feat: send puzzle result subscribe messages
This commit is contained in:
@@ -15,6 +15,10 @@ const MINI_PROGRAM_APP_ID = 'wx3da23ea14ca66b65';
|
||||
// 中文注释:仅作为运行时环境识别失败时的兜底;正常情况下由 wx.getAccountInfoSync 自动判断。
|
||||
const MINI_PROGRAM_ENV = 'release';
|
||||
|
||||
// 中文注释:AI 创作生成结果订阅消息模板,需与微信公众平台后台的模板 ID 保持一致。
|
||||
const GENERATION_RESULT_SUBSCRIBE_TEMPLATE_ID =
|
||||
'm5z7BkkBhJGbcH0cdDeHaeRU2tViDEguP38XdrRRCdU';
|
||||
|
||||
// 中文注释:给 H5 加一个来源标记,便于后续前端或后端识别这是微信小程序 web-view 宿主。
|
||||
const WEB_VIEW_SOURCE_QUERY = {
|
||||
clientType: 'mini_program',
|
||||
@@ -25,6 +29,7 @@ module.exports = {
|
||||
API_BASE_URL,
|
||||
DEV_API_BASE_URL,
|
||||
DEV_WEB_VIEW_ENTRY_URL,
|
||||
GENERATION_RESULT_SUBSCRIBE_TEMPLATE_ID,
|
||||
MINI_PROGRAM_APP_ID,
|
||||
MINI_PROGRAM_ENV,
|
||||
WEB_VIEW_ENTRY_URL,
|
||||
|
||||
@@ -5,6 +5,7 @@ const {
|
||||
API_BASE_URL,
|
||||
DEV_API_BASE_URL,
|
||||
DEV_WEB_VIEW_ENTRY_URL,
|
||||
GENERATION_RESULT_SUBSCRIBE_TEMPLATE_ID,
|
||||
MINI_PROGRAM_APP_ID,
|
||||
MINI_PROGRAM_ENV,
|
||||
WEB_VIEW_ENTRY_URL,
|
||||
@@ -20,6 +21,8 @@ const AUTH_ACTION_LOGIN = 'login';
|
||||
const PAY_RESULT_RECHECK_DELAY_MS = 120;
|
||||
const WEB_VIEW_SHARE_TITLE = '陶泥儿';
|
||||
const WEB_VIEW_SHARE_PATH = '/pages/web-view/index';
|
||||
const SUBSCRIBE_MESSAGE_TYPE = 'genarrative:request-subscribe-message';
|
||||
const GENERATION_RESULT_SUBSCRIBE_SCENE = 'generation-result';
|
||||
|
||||
function showWebViewShareMenu() {
|
||||
if (typeof wx.showShareMenu !== 'function') {
|
||||
@@ -415,6 +418,36 @@ function requestMiniProgramBindPhone(authToken, wechatPhoneCode, displayName) {
|
||||
});
|
||||
}
|
||||
|
||||
function requestGenerationResultSubscribeMessage() {
|
||||
return new Promise((resolve) => {
|
||||
if (!GENERATION_RESULT_SUBSCRIBE_TEMPLATE_ID) {
|
||||
resolve({ ok: false, reason: 'missing_template_id' });
|
||||
return;
|
||||
}
|
||||
if (typeof wx.requestSubscribeMessage !== 'function') {
|
||||
resolve({ ok: false, reason: 'unsupported' });
|
||||
return;
|
||||
}
|
||||
|
||||
wx.requestSubscribeMessage({
|
||||
tmplIds: [GENERATION_RESULT_SUBSCRIBE_TEMPLATE_ID],
|
||||
success(result) {
|
||||
resolve({
|
||||
ok: result[GENERATION_RESULT_SUBSCRIBE_TEMPLATE_ID] === 'accept',
|
||||
result,
|
||||
});
|
||||
},
|
||||
fail(error) {
|
||||
console.warn('[web-view] request subscribe message failed', error);
|
||||
resolve({
|
||||
ok: false,
|
||||
reason: error && error.errMsg ? error.errMsg : 'failed',
|
||||
});
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function resolveAuthResult(displayName) {
|
||||
const code = await wxLogin();
|
||||
const response = await requestMiniProgramLogin(code, displayName);
|
||||
@@ -712,7 +745,23 @@ Page({
|
||||
},
|
||||
|
||||
handleWebViewMessage(event) {
|
||||
// 中文注释:支付由独立 native 页面承接,web-view 消息只保留调试输出。
|
||||
const messages =
|
||||
event && event.detail && Array.isArray(event.detail.data)
|
||||
? event.detail.data
|
||||
: [];
|
||||
const shouldRequestSubscribe = messages.some((message) => {
|
||||
const payload = message && typeof message === 'object' ? message : {};
|
||||
return (
|
||||
payload.type === SUBSCRIBE_MESSAGE_TYPE &&
|
||||
payload.scene === GENERATION_RESULT_SUBSCRIBE_SCENE
|
||||
);
|
||||
});
|
||||
if (shouldRequestSubscribe) {
|
||||
void requestGenerationResultSubscribeMessage();
|
||||
return;
|
||||
}
|
||||
|
||||
// 中文注释:支付由独立 native 页面承接,其他 web-view 消息只保留调试输出。
|
||||
console.info('[web-view] message', event.detail);
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user