fix(h5-partner): allow reopening WeChat image picker after cancel
Avoid resetting JSSDK on each pick and unlock cancel paths so camera/album can be opened again. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -17,10 +17,14 @@ function delay(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => window.setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
function isChooseImageCancelMessage(msg: string): boolean {
|
||||
return /cancel|取消/i.test(msg.trim());
|
||||
}
|
||||
|
||||
/** 将微信 chooseImage / getLocalImgData fail 的 errMsg 转为用户可读文案 */
|
||||
export function formatChooseImageFailMessage(errMsg: string): string {
|
||||
const msg = errMsg.trim() || '无法打开相册';
|
||||
if (/cancel/i.test(msg)) return '';
|
||||
if (isChooseImageCancelMessage(msg)) return '';
|
||||
|
||||
if (/offline verifying|permission value is offline/i.test(msg)) {
|
||||
return '微信权限验证中,请稍候再试或刷新页面后重新选择图片';
|
||||
@@ -54,7 +58,7 @@ async function reportChooseImageEvent(
|
||||
stage: 'jssdk' | 'choose' | 'read' | 'empty';
|
||||
},
|
||||
) {
|
||||
if (/cancel/i.test(payload.errMsg)) return;
|
||||
if (isChooseImageCancelMessage(payload.errMsg)) return;
|
||||
try {
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -159,19 +163,38 @@ function invokeChooseImage(
|
||||
sourceType: Array<'album' | 'camera'>,
|
||||
): Promise<string[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let settled = false;
|
||||
const finish = (fn: () => void) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
window.clearTimeout(hangTimer);
|
||||
fn();
|
||||
};
|
||||
|
||||
// 极端机型取消后无回调:最长等待后按取消解锁,避免业务侧 uploading 永久卡住
|
||||
const hangTimer = window.setTimeout(() => {
|
||||
finish(() => resolve([]));
|
||||
}, 180_000);
|
||||
|
||||
window.wx!.chooseImage!({
|
||||
count,
|
||||
sizeType: ['compressed'],
|
||||
sourceType,
|
||||
success: (res) => resolve(res.localIds ?? []),
|
||||
success: (res) => finish(() => resolve(res.localIds ?? [])),
|
||||
fail: (err) => {
|
||||
const raw = err.errMsg || '无法打开相册';
|
||||
if (/cancel/i.test(raw)) {
|
||||
resolve([]);
|
||||
if (isChooseImageCancelMessage(raw)) {
|
||||
finish(() => resolve([]));
|
||||
return;
|
||||
}
|
||||
const formatted = formatChooseImageFailMessage(raw);
|
||||
reject(new Error(formatted || raw));
|
||||
finish(() => reject(new Error(formatted || raw)));
|
||||
},
|
||||
// 部分微信版本取消只走 complete
|
||||
complete: () => {
|
||||
window.setTimeout(() => {
|
||||
finish(() => resolve([]));
|
||||
}, 300);
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -51,6 +51,7 @@ export type WxApi = {
|
||||
sourceType?: Array<'album' | 'camera'>;
|
||||
success?: (res: { localIds: string[] }) => void;
|
||||
fail?: (res: { errMsg: string }) => void;
|
||||
complete?: (res?: { errMsg?: string }) => void;
|
||||
}) => void;
|
||||
getLocalImgData: (options: {
|
||||
localId: string;
|
||||
|
||||
Reference in New Issue
Block a user