Files
Genarrative/miniprogram/pages/web-view/index.js

57 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const { WEB_VIEW_ENTRY_URL, WEB_VIEW_SOURCE_QUERY } = require('../../config');
function isConfiguredEntryUrl(value) {
const trimmed = String(value || '').trim();
return /^https:\/\/[^/]+/i.test(trimmed);
}
function appendQuery(url, query) {
const pairs = Object.keys(query)
.filter((key) => query[key])
.map(
(key) =>
`${encodeURIComponent(key)}=${encodeURIComponent(String(query[key]))}`,
);
if (pairs.length === 0) {
return url;
}
return `${url}${url.includes('?') ? '&' : '?'}${pairs.join('&')}`;
}
function resolveWebViewUrl() {
const entryUrl = String(WEB_VIEW_ENTRY_URL || '').trim();
if (!isConfiguredEntryUrl(entryUrl)) {
return '';
}
return appendQuery(entryUrl, WEB_VIEW_SOURCE_QUERY);
}
Page({
data: {
webViewUrl: '',
},
onLoad() {
// 中文注释web-view 只能打开已配置业务域名;未配置时展示本地提示,避免空白页误判。
this.setData({
webViewUrl: resolveWebViewUrl(),
});
},
handleWebViewLoad(event) {
console.info('[web-view] loaded', event.detail);
},
handleWebViewError(event) {
console.error('[web-view] load failed', event.detail);
},
handleWebViewMessage(event) {
// 中文注释H5 如需和小程序壳通信,可通过 wx.miniProgram.postMessage 发送轻量消息。
console.info('[web-view] message', event.detail);
},
});