99 lines
2.5 KiB
Vue
99 lines
2.5 KiB
Vue
<template>
|
||
<web-view
|
||
ref="webviewRef"
|
||
:src="iframeUrl"
|
||
@message="handleMessage"
|
||
:fullscreen="true"
|
||
></web-view>
|
||
</template>
|
||
|
||
<script>
|
||
import { getTopSafeAreaHeightAsync } from '@utils/common';
|
||
const TYPE_EMUE = {
|
||
CONNECT: 0,
|
||
DATA: 1,
|
||
FUNCTION: 2,
|
||
CONFIG: 3,
|
||
}
|
||
export default {
|
||
data() {
|
||
return {
|
||
baseUrl: 'https://kr-op.quwanzhi.com/init',
|
||
iframeUrl: '', // 动态构建的 URL
|
||
receivedMessages: [],
|
||
messageId: 0,
|
||
urlParams: {}
|
||
}
|
||
},
|
||
onLoad() {
|
||
this.sendBaseConfig()
|
||
},
|
||
methods: {
|
||
// 构建 iframe URL,包含参数
|
||
buildIframeUrl() {
|
||
const params = [];
|
||
Object.keys(this.urlParams).forEach(key => {
|
||
const value = this.urlParams[key];
|
||
if (value !== null && value !== undefined) {
|
||
const encodedKey = encodeURIComponent(key);
|
||
const encodedValue = encodeURIComponent(String(value));
|
||
params.push(`${encodedKey}=${encodedValue}`);
|
||
}
|
||
});
|
||
const queryString = params.join('&');
|
||
this.iframeUrl = queryString ? `${this.baseUrl}?${queryString}` : this.baseUrl;
|
||
},
|
||
// 发送消息到 iframe(通过URL传参)
|
||
async sendBaseConfig() {
|
||
const message = {
|
||
type: TYPE_EMUE.CONFIG,
|
||
data: {
|
||
paddingTop: await getTopSafeAreaHeightAsync(),
|
||
appId: '1234567890',
|
||
appName: '存客宝',
|
||
appVersion: '1.0.0',
|
||
isAppMode:true
|
||
}
|
||
};
|
||
|
||
// 将消息添加到URL参数中
|
||
this.urlParams.message = encodeURIComponent(JSON.stringify(message));
|
||
this.buildIframeUrl();
|
||
console.log('[App]SendMessage=>\n' + JSON.stringify(message));
|
||
},
|
||
// 接收 web-view 发送的消息
|
||
handleMessage(event) {
|
||
console.log("event", event);
|
||
const [ResDetail] = event.detail.data;
|
||
this.receivedMessages.push(`[${new Date().toLocaleTimeString()}] ${JSON.stringify(ResDetail)}`);
|
||
|
||
switch (ResDetail.type) {
|
||
case TYPE_EMUE.DATA:
|
||
console.log('[App]ReceiveMessage=>\n' + JSON.stringify(ResDetail.data));
|
||
break;
|
||
case TYPE_EMUE.FUNCTION:
|
||
console.log('[App]ReceiveMessage=>\n' + JSON.stringify(ResDetail.data));
|
||
if (ResDetail.data.action === 'clearCache') {
|
||
this.clearCache();
|
||
}
|
||
break;
|
||
|
||
}
|
||
},
|
||
clearCache() {
|
||
// 清除 webview 缓存
|
||
if (this.$refs.webviewRef) {
|
||
// 重新加载 webview
|
||
this.$refs.webviewRef.reload();
|
||
}
|
||
// 清除 webview 缓存数据
|
||
uni.clearStorage({
|
||
success: () => {
|
||
console.log('Webview 缓存已清除');
|
||
}
|
||
});
|
||
|
||
}
|
||
}
|
||
}
|
||
</script> |