合并分享链路重构到主分支
合入通用作品分享卡片与小程序直达路径 合入推荐页当前作品系统分享参数同步 合入小程序九宫切图与相关测试 # Conflicts: # .hermes/shared-memory/decision-log.md # docs/【开发运维】本地开发验证与生产运维-2026-05-15.md # docs/【玩法创作】平台入口与玩法链路-2026-05-15.md # src/components/custom-world-home/CustomWorldCreationHub.tsx # src/components/platform-entry/PlatformEntryFlowShellImpl.test.ts # src/components/platform-entry/PlatformEntryFlowShellImpl.tsx # src/components/rpg-entry/RpgEntryHomeView.tsx
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { isWechatMiniProgramWebViewRuntime } from './authService';
|
||||
|
||||
const MESSAGE_TYPE = 'genarrative:share-target';
|
||||
|
||||
export type WechatMiniProgramShareTarget = {
|
||||
targetPath: '/works/detail';
|
||||
work: string;
|
||||
title?: string | null;
|
||||
};
|
||||
|
||||
function normalizeShareTarget(
|
||||
target: WechatMiniProgramShareTarget | null | undefined,
|
||||
) {
|
||||
const work = target?.work?.trim() ?? '';
|
||||
if (!work) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
targetPath: '/works/detail' as const,
|
||||
work,
|
||||
title: target?.title?.trim() || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildWechatMiniProgramShareTargetMessage(
|
||||
target: WechatMiniProgramShareTarget | null | undefined,
|
||||
) {
|
||||
const normalizedTarget = normalizeShareTarget(target);
|
||||
return normalizedTarget
|
||||
? {
|
||||
type: MESSAGE_TYPE,
|
||||
payload: normalizedTarget,
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
export function postWechatMiniProgramShareTarget(
|
||||
target: WechatMiniProgramShareTarget | null | undefined,
|
||||
) {
|
||||
if (
|
||||
typeof window === 'undefined' ||
|
||||
!isWechatMiniProgramWebViewRuntime() ||
|
||||
typeof window.wx?.miniProgram?.postMessage !== 'function'
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const message = buildWechatMiniProgramShareTargetMessage(target);
|
||||
if (!message) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 中文注释:微信 web-view 会在分享等时机把 postMessage 数据交给原生页,
|
||||
// 小程序页据此把右上角系统分享指向当前推荐作品。
|
||||
window.wx.miniProgram.postMessage({
|
||||
data: message,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user