Files
Genarrative/src/services/wechatMiniProgramSubscribe.ts
T
kdletters 4790f1dca5 补齐小程序壳路由一致性门禁
根级原生壳验收新增小程序页面路由一致性检查

订阅授权页面常量纳入微信协议反查

小程序 H5 页面常量、分享入口、来源 query 和 HTTPS 域名格式纳入门禁

同步宿主壳文档和共享决策记录
2026-06-19 06:51:34 +08:00

132 lines
3.8 KiB
TypeScript

import {
getHostRuntime,
navigateHostNativePage,
} from './host-bridge/hostBridge';
const SUBSCRIBE_RESULT_HASH_KEY = 'wx_subscribe_result';
const MINI_PROGRAM_SUBSCRIBE_MESSAGE_PAGE_URL = '/pages/subscribe-message/index';
const SUBSCRIBE_RESULT_TIMEOUT_MS = 2_500;
const SUBSCRIBE_RESULT_RETURN_FALLBACK_MS = 800;
function clearSubscribeResultHash() {
const rawHash = window.location.hash.replace(/^#/, '');
if (!rawHash.includes(`${SUBSCRIBE_RESULT_HASH_KEY}=`)) {
return;
}
const params = new URLSearchParams(rawHash);
params.delete(SUBSCRIBE_RESULT_HASH_KEY);
const nextHash = params.toString();
window.history.replaceState(
null,
'',
`${window.location.pathname}${window.location.search}${nextHash ? `#${nextHash}` : ''}`,
);
}
function readSubscribeResultFromHash() {
const value = new URLSearchParams(window.location.hash.replace(/^#/, '')).get(
SUBSCRIBE_RESULT_HASH_KEY,
);
if (!value) {
return null;
}
clearSubscribeResultHash();
return value;
}
function waitSubscribeResultFromHash(timeoutMs = SUBSCRIBE_RESULT_TIMEOUT_MS) {
const immediateResult = readSubscribeResultFromHash();
if (immediateResult) {
return Promise.resolve(immediateResult);
}
return new Promise<string | null>((resolve) => {
let timer: number | null = null;
let resumeFallbackTimer: number | null = null;
const cleanup = () => {
window.removeEventListener('hashchange', handleHashChange);
window.removeEventListener('focus', handleResume);
window.removeEventListener('pageshow', handleResume);
document.removeEventListener('visibilitychange', handleResume);
if (timer !== null) {
window.clearTimeout(timer);
}
if (resumeFallbackTimer !== null) {
window.clearTimeout(resumeFallbackTimer);
}
};
const finish = (result: string | null) => {
cleanup();
resolve(result);
};
const consume = () => {
const result = readSubscribeResultFromHash();
if (result) {
finish(result);
return true;
}
return false;
};
const handleHashChange = () => {
consume();
};
const handleResume = () => {
if (
typeof document !== 'undefined' &&
document.visibilityState === 'hidden'
) {
return;
}
if (consume()) {
return;
}
// 中文注释:订阅授权只影响后续通知,不应阻断生成;原生页返回但没有 hash
// 回灌时,按已返回处理,让原本的生成提交流程继续执行。
if (resumeFallbackTimer === null) {
resumeFallbackTimer = window.setTimeout(
() => finish('returned'),
SUBSCRIBE_RESULT_RETURN_FALLBACK_MS,
);
}
};
window.addEventListener('hashchange', handleHashChange);
window.addEventListener('focus', handleResume);
window.addEventListener('pageshow', handleResume);
document.addEventListener('visibilitychange', handleResume);
timer = window.setTimeout(() => finish(null), timeoutMs);
});
}
export async function requestGenerationResultSubscribePermission() {
if (
getHostRuntime().kind !== 'wechat_mini_program' ||
typeof window === 'undefined'
) {
return false;
}
const requestId = `subscribe_generation_result_${Date.now()}`;
let resultPromise: Promise<string | null> | null = null;
try {
const navigated = await navigateHostNativePage(
`${MINI_PROGRAM_SUBSCRIBE_MESSAGE_PAGE_URL}?requestId=${encodeURIComponent(requestId)}&scene=generation-result`,
{
errorMessage: 'wechat_js_sdk_unavailable',
beforeNavigate() {
resultPromise = waitSubscribeResultFromHash();
},
},
);
if (!navigated) {
return false;
}
} catch {
return false;
}
const result = await (resultPromise ?? Promise.resolve(null));
return Boolean(result);
}