fix(partner): 修正微信选图 JSSDK 签名 URL 含 query 参数

录店页 ?step= 变化时重新 wx.config,微信内上传仅走 chooseImage 接口。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-12 16:24:42 +08:00
parent 656e906254
commit 076455944e
4 changed files with 54 additions and 68 deletions
+1
View File
@@ -71,6 +71,7 @@ async function reportChooseImageEvent(
sourceType: payload.sourceType,
stage: payload.stage,
pageUrl: typeof window !== 'undefined' ? normalizeJssdkPageUrl(window.location.href) : undefined,
signUrl: typeof window !== 'undefined' ? getJssdkSignUrl() : undefined,
}),
});
} catch {
+31 -29
View File
@@ -1,64 +1,66 @@
import type { WechatJssdkConfig } from '@dukang/shared-types';
import { isIosDevice, isWechatBrowser, isWechatDevTools } from './env';
import { isWechatBrowser, isWechatDevTools } from './env';
import { DEFAULT_JS_API_LIST } from './types';
const JSSDK_URL = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js';
const IOS_ENTRY_URL_KEY = 'dukang_wx_ios_entry_url';
const SIGN_URL_CACHE_KEY = 'dukang_wx_sign_url_v2';
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 步骤参数不参与签名) */
/** 参与 JSSDK 签名的页面 URL:与微信文档一致,取 location.href 去掉 # 后的部分;剔除 OAuth 回调参数 */
export function normalizeJssdkPageUrl(rawUrl: string): string {
try {
const url = new URL(rawUrl);
return `${url.origin}${url.pathname}`;
url.hash = '';
url.searchParams.delete('code');
url.searchParams.delete('state');
const query = url.searchParams.toString();
return `${url.origin}${url.pathname}${query ? `?${query}` : ''}`;
} catch {
return rawUrl.split('#')[0].split('?')[0];
const noHash = rawUrl.split('#')[0];
try {
const url = new URL(noHash, typeof window !== 'undefined' ? window.location.origin : 'https://localhost');
url.searchParams.delete('code');
url.searchParams.delete('state');
const query = url.searchParams.toString();
return `${url.origin}${url.pathname}${query ? `?${query}` : ''}`;
} catch {
return noHash;
}
}
}
/** iOS 微信内 SPA:同 pathname 内复用入口 URL;跨路由时更新入口并失效旧签名 */
function signUrlChanged(prev: string | null, current: string): boolean {
return prev !== current;
}
/** 记录最近一次签名 URL;SPA 路由或 ?step= 变化时须重新 wx.config */
export function captureIosJssdkEntryUrl(): void {
if (typeof window === 'undefined') return;
if (!isIosDevice() || !isWechatBrowser() || isWechatDevTools()) return;
if (!isWechatBrowser() || isWechatDevTools()) return;
const current = normalizeJssdkPageUrl(window.location.href);
const existing = sessionStorage.getItem(IOS_ENTRY_URL_KEY);
const existing = sessionStorage.getItem(SIGN_URL_CACHE_KEY);
if (!existing) {
sessionStorage.setItem(IOS_ENTRY_URL_KEY, current);
sessionStorage.setItem(SIGN_URL_CACHE_KEY, current);
return;
}
if (pathnameOf(existing) !== pathnameOf(current)) {
sessionStorage.setItem(IOS_ENTRY_URL_KEY, current);
if (signUrlChanged(existing, current)) {
sessionStorage.setItem(SIGN_URL_CACHE_KEY, current);
resetJssdkConfig();
}
}
/** 获取参与 JSSDK 签名的 URL */
/** 获取参与 JSSDK 签名的 URL(始终为当前页完整 URL,含 query) */
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 && pathnameOf(entry) === pathnameOf(current)) {
return entry;
}
}
return current;
return normalizeJssdkPageUrl(rawUrl ?? (typeof window !== 'undefined' ? window.location.href : ''));
}
function isJssdkDebugEnabled(): boolean {
@@ -163,7 +165,7 @@ export async function ensureJssdkReady(options: {
}): Promise<void> {
captureIosJssdkEntryUrl();
const pageUrl = options.url ?? getJssdkSignUrl();
if (configuredUrl && pathnameOf(configuredUrl) !== pathnameOf(pageUrl)) {
if (configuredUrl && signUrlChanged(configuredUrl, pageUrl)) {
resetJssdkConfig();
}
if (!isJssdkReady()) {