fix: align miniprogram web-view auth and share
This commit is contained in:
@@ -16,6 +16,33 @@ const PAY_RESULT_STORAGE_KEY = 'genarrative:wechat-pay-result';
|
||||
const AUTH_RESULT_STORAGE_KEY = 'genarrative:mini-program-auth-result';
|
||||
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';
|
||||
|
||||
function showWebViewShareMenu() {
|
||||
if (typeof wx.showShareMenu !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
wx.showShareMenu({
|
||||
withShareTicket: true,
|
||||
menus: ['shareAppMessage', 'shareTimeline'],
|
||||
});
|
||||
}
|
||||
|
||||
function buildWebViewShareAppMessage() {
|
||||
return {
|
||||
title: WEB_VIEW_SHARE_TITLE,
|
||||
path: WEB_VIEW_SHARE_PATH,
|
||||
};
|
||||
}
|
||||
|
||||
function buildWebViewShareTimeline() {
|
||||
return {
|
||||
title: WEB_VIEW_SHARE_TITLE,
|
||||
query: '',
|
||||
};
|
||||
}
|
||||
|
||||
function isConfiguredEntryUrl(value) {
|
||||
const trimmed = String(value || '').trim();
|
||||
@@ -271,18 +298,6 @@ async function resolveAuthResult() {
|
||||
};
|
||||
}
|
||||
|
||||
async function refreshMiniProgramSessionSilently() {
|
||||
if (!isConfiguredApiBaseUrl(API_BASE_URL)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return await resolveAuthResult();
|
||||
} catch (error) {
|
||||
console.warn('[web-view] silent mini program login refresh failed', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
authResult: null,
|
||||
@@ -296,6 +311,7 @@ Page({
|
||||
|
||||
async onLoad(query = {}) {
|
||||
this._lastLaunchQuery = query;
|
||||
showWebViewShareMenu();
|
||||
// 中文注释:web-view 只能打开已配置业务域名;未配置时展示本地提示,避免空白页误判。
|
||||
if (!isConfiguredEntryUrl(WEB_VIEW_ENTRY_URL)) {
|
||||
this.setData({
|
||||
@@ -309,14 +325,13 @@ Page({
|
||||
const forcedPhoneBinding = parseBooleanQueryFlag(query.phoneBindingRequired);
|
||||
const returnToPreviousPage = shouldReturnToPreviousPage(query);
|
||||
if (!shouldStartAuthFromQuery(query) && !forcedPhoneBinding) {
|
||||
const authResult = await refreshMiniProgramSessionSilently();
|
||||
this.setData({
|
||||
authResult,
|
||||
authResult: null,
|
||||
errorMessage: '',
|
||||
loading: false,
|
||||
phoneBindingRequired: false,
|
||||
returnToPreviousPage: false,
|
||||
webViewUrl: resolveWebViewUrl(authResult),
|
||||
webViewUrl: resolveWebViewUrl(null),
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -490,4 +505,12 @@ Page({
|
||||
// 中文注释:支付由独立 native 页面承接,web-view 消息只保留调试输出。
|
||||
console.info('[web-view] message', event.detail);
|
||||
},
|
||||
|
||||
onShareAppMessage() {
|
||||
return buildWebViewShareAppMessage();
|
||||
},
|
||||
|
||||
onShareTimeline() {
|
||||
return buildWebViewShareTimeline();
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user