fix(partner): resolve WeChat image picker auth denied on store create
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -51,12 +51,17 @@ export async function chooseWechatImages(
|
||||
const sourceType = options.sourceType ?? ['album', 'camera'];
|
||||
|
||||
if (platform === 'wechat-h5') {
|
||||
const pageUrl = typeof window !== 'undefined' ? window.location.href.split('#')[0] : '';
|
||||
await ensureJssdkReady({
|
||||
apiBase: config.apiBase ?? '/api/v1',
|
||||
clientApp: config.clientApp,
|
||||
getAccessToken: config.getAccessToken,
|
||||
url: pageUrl,
|
||||
jsApiList: ['chooseImage', 'getLocalImgData'],
|
||||
});
|
||||
if (!window.wx?.chooseImage) return null;
|
||||
if (!window.wx?.chooseImage) {
|
||||
throw new Error('微信选图接口不可用,请刷新页面后重试');
|
||||
}
|
||||
|
||||
const localIds = await new Promise<string[]>((resolve, reject) => {
|
||||
window.wx!.chooseImage!({
|
||||
|
||||
@@ -5,6 +5,11 @@ const JSSDK_URL = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js';
|
||||
|
||||
let scriptPromise: Promise<void> | null = null;
|
||||
let configured = false;
|
||||
let configuredUrl: string | null = null;
|
||||
|
||||
function currentPageUrl() {
|
||||
return typeof window !== 'undefined' ? window.location.href.split('#')[0] : '';
|
||||
}
|
||||
|
||||
function loadScript(): Promise<void> {
|
||||
if (typeof document === 'undefined') return Promise.reject(new Error('非浏览器环境'));
|
||||
@@ -48,13 +53,16 @@ export async function initWechatJssdk(options: {
|
||||
jsApiList?: string[];
|
||||
}): Promise<void> {
|
||||
const { apiBase, clientApp, getAccessToken } = options;
|
||||
const pageUrl = options.url ?? (typeof window !== 'undefined' ? window.location.href.split('#')[0] : '');
|
||||
const pageUrl = options.url ?? currentPageUrl();
|
||||
await loadScript();
|
||||
if (!window.wx) throw new Error('微信 JSSDK 不可用');
|
||||
|
||||
const config = await fetchJssdkConfig(apiBase, clientApp, pageUrl, getAccessToken?.());
|
||||
const jsApiList = options.jsApiList ?? [...DEFAULT_JS_API_LIST];
|
||||
|
||||
configured = false;
|
||||
configuredUrl = null;
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
window.wx!.config({
|
||||
...config,
|
||||
@@ -63,6 +71,7 @@ export async function initWechatJssdk(options: {
|
||||
});
|
||||
window.wx!.ready(() => {
|
||||
configured = true;
|
||||
configuredUrl = pageUrl;
|
||||
resolve();
|
||||
});
|
||||
window.wx!.error((err) => reject(new Error(err.errMsg || 'wx.config 失败')));
|
||||
@@ -70,15 +79,18 @@ export async function initWechatJssdk(options: {
|
||||
}
|
||||
|
||||
export function isJssdkReady(): boolean {
|
||||
return configured && !!window.wx;
|
||||
return configured && !!window.wx && configuredUrl === currentPageUrl();
|
||||
}
|
||||
|
||||
export async function ensureJssdkReady(options: {
|
||||
apiBase: string;
|
||||
clientApp: string;
|
||||
url?: string;
|
||||
getAccessToken?: () => string | null;
|
||||
jsApiList?: string[];
|
||||
}): Promise<void> {
|
||||
const pageUrl = options.url ?? currentPageUrl();
|
||||
if (!isJssdkReady()) {
|
||||
await initWechatJssdk(options);
|
||||
await initWechatJssdk({ ...options, url: pageUrl });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user