微信sdk更新,修正上传门头照照片错误

This commit is contained in:
2026-07-12 16:10:11 +08:00
parent e8823a0640
commit 656e906254
6 changed files with 126 additions and 27 deletions
+31 -4
View File
@@ -9,6 +9,20 @@ let scriptPromise: Promise<void> | null = null;
let configured = false;
let configuredUrl: string | null = null;
function pathnameOf(url: string): string {
try {
return new URL(url).pathname;
} catch {
return url.split('?')[0];
}
}
/** 清除 JSSDK 配置缓存(路由切换后须重新 wx.config) */
export function resetJssdkConfig(): void {
configured = false;
configuredUrl = null;
}
/** 参与 JSSDK 签名的页面 URL(去掉 hash、query;SPA 步骤参数不参与签名) */
export function normalizeJssdkPageUrl(rawUrl: string): string {
try {
@@ -19,12 +33,20 @@ export function normalizeJssdkPageUrl(rawUrl: string): string {
}
}
/** iOS 微信内 SPA签名 URL 须用首次进入页面的入口 URL,而非路由跳转后的当前 URL */
/** iOS 微信内 SPA同 pathname 内复用入口 URL;跨路由时更新入口并失效旧签名 */
export function captureIosJssdkEntryUrl(): void {
if (typeof window === 'undefined') return;
if (!isIosDevice() || !isWechatBrowser() || isWechatDevTools()) return;
if (sessionStorage.getItem(IOS_ENTRY_URL_KEY)) return;
sessionStorage.setItem(IOS_ENTRY_URL_KEY, normalizeJssdkPageUrl(window.location.href));
const current = normalizeJssdkPageUrl(window.location.href);
const existing = sessionStorage.getItem(IOS_ENTRY_URL_KEY);
if (!existing) {
sessionStorage.setItem(IOS_ENTRY_URL_KEY, current);
return;
}
if (pathnameOf(existing) !== pathnameOf(current)) {
sessionStorage.setItem(IOS_ENTRY_URL_KEY, current);
resetJssdkConfig();
}
}
/** 获取参与 JSSDK 签名的 URL */
@@ -32,7 +54,9 @@ export function getJssdkSignUrl(rawUrl?: string): string {
const current = normalizeJssdkPageUrl(rawUrl ?? (typeof window !== 'undefined' ? window.location.href : ''));
if (typeof window !== 'undefined' && isIosDevice() && isWechatBrowser() && !isWechatDevTools()) {
const entry = sessionStorage.getItem(IOS_ENTRY_URL_KEY);
if (entry) return entry;
if (entry && pathnameOf(entry) === pathnameOf(current)) {
return entry;
}
}
return current;
}
@@ -139,6 +163,9 @@ export async function ensureJssdkReady(options: {
}): Promise<void> {
captureIosJssdkEntryUrl();
const pageUrl = options.url ?? getJssdkSignUrl();
if (configuredUrl && pathnameOf(configuredUrl) !== pathnameOf(pageUrl)) {
resetJssdkConfig();
}
if (!isJssdkReady()) {
await initWechatJssdk({ ...options, url: pageUrl });
}