Add WeChat miniprogram web-view shell

This commit is contained in:
2026-05-03 16:29:42 +08:00
parent 49aad7311c
commit 9baa515a75
13 changed files with 280 additions and 0 deletions

10
miniprogram/app.js Normal file
View File

@@ -0,0 +1,10 @@
App({
globalData: {
launchOptions: null,
},
onLaunch(options) {
// 中文注释:保留启动参数,后续如果要把分享路径映射到 H5 深链,可以从这里统一读取。
this.globalData.launchOptions = options;
},
});

20
miniprogram/app.json Normal file
View File

@@ -0,0 +1,20 @@
{
"pages": [
"pages/web-view/index"
],
"window": {
"navigationBarTitleText": "百梦",
"navigationBarBackgroundColor": "#0b0f14",
"navigationBarTextStyle": "white",
"backgroundColor": "#0b0f14",
"backgroundTextStyle": "light"
},
"networkTimeout": {
"request": 60000,
"connectSocket": 60000,
"uploadFile": 60000,
"downloadFile": 60000
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}

5
miniprogram/app.wxss Normal file
View File

@@ -0,0 +1,5 @@
page {
min-height: 100vh;
background: #0b0f14;
color: #f5f7fb;
}

15
miniprogram/config.js Normal file
View File

@@ -0,0 +1,15 @@
// 中文注释:这里填写已经在“小程序后台-开发-开发设置-业务域名”配置过的 H5 入口。
// 示例https://game.example.com/
// 注意:必须是 https 域名,不能是 localhost、IP 地址或未备案域名。
const WEB_VIEW_ENTRY_URL = 'https://dev.genarrative.world/';
// 中文注释:给 H5 加一个来源标记,便于后续前端或后端识别这是微信小程序 web-view 宿主。
const WEB_VIEW_SOURCE_QUERY = {
clientType: 'mini_program',
clientRuntime: 'wechat_mini_program',
};
module.exports = {
WEB_VIEW_ENTRY_URL,
WEB_VIEW_SOURCE_QUERY,
};

View File

@@ -0,0 +1,56 @@
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);
},
});

View File

@@ -0,0 +1,3 @@
{
"usingComponents": {}
}

View File

@@ -0,0 +1,15 @@
<block wx:if="{{webViewUrl}}">
<web-view
src="{{webViewUrl}}"
bindload="handleWebViewLoad"
binderror="handleWebViewError"
bindmessage="handleWebViewMessage"
/>
</block>
<view wx:else class="setup-screen">
<view class="setup-card">
<view class="setup-title">需要配置 H5 入口</view>
<view class="setup-text">请先在 miniprogram/config.js 填写 WEB_VIEW_ENTRY_URL。</view>
</view>
</view>

View File

@@ -0,0 +1,33 @@
.setup-screen {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 48rpx;
background: #0b0f14;
box-sizing: border-box;
}
.setup-card {
width: 100%;
max-width: 560rpx;
padding: 36rpx;
border: 1rpx solid rgba(255, 255, 255, 0.14);
border-radius: 12rpx;
background: rgba(255, 255, 255, 0.06);
box-sizing: border-box;
}
.setup-title {
font-size: 34rpx;
font-weight: 600;
line-height: 1.35;
color: #f5f7fb;
}
.setup-text {
margin-top: 16rpx;
font-size: 26rpx;
line-height: 1.55;
color: rgba(245, 247, 251, 0.72);
}

8
miniprogram/sitemap.json Normal file
View File

@@ -0,0 +1,8 @@
{
"rules": [
{
"action": "allow",
"page": "*"
}
]
}