FEAT => 本次更新项目为:

初始化状态链接功能
This commit is contained in:
超级老白兔
2025-08-02 17:10:41 +08:00
parent 56c9c24937
commit ae4cbd46dc
8 changed files with 474 additions and 43 deletions

View File

@@ -42,7 +42,7 @@
export default {
data() {
return {
baseUrl: 'https://kr-op.quwanzhi.com/iframe',
baseUrl: 'https://kr-op.quwanzhi.com/init',
iframeUrl: '', // 动态构建的 URL
receivedMessages: [],
messageId: 0,
@@ -55,7 +55,6 @@
methods: {
// 构建 iframe URL包含参数
buildIframeUrl() {
const params = [];
Object.keys(this.urlParams).forEach(key => {
const value = this.urlParams[key];
@@ -65,7 +64,6 @@
params.push(`${encodedKey}=${encodedValue}`);
}
});
const queryString = params.join('&');
this.iframeUrl = queryString ? `${this.baseUrl}?${queryString}` : this.baseUrl;
},

View File

@@ -33,12 +33,12 @@
</view>
</view>
</view>
</view>
</template>
<script>
import { getTopSafeAreaHeightAsync } from '@utils/common';
export default {
data() {
return {
@@ -46,7 +46,6 @@
iframeUrl: '', // 动态构建的 URL
receivedMessages: [],
messageId: 0,
// URL 参数配置
urlParams: {}
}
},
@@ -56,8 +55,8 @@
methods: {
// 构建 iframe URL包含参数
buildIframeUrl() {
const params = [];
const params = [];
Object.keys(this.urlParams).forEach(key => {
const value = this.urlParams[key];
if (value !== null && value !== undefined) {
@@ -69,49 +68,34 @@
const queryString = params.join('&');
this.iframeUrl = queryString ? `${this.baseUrl}?${queryString}` : this.baseUrl;
// console.log('构建的 iframe URL:', this.iframeUrl);
},
// 发送消息到 iframe通过URL传参
sendMessageToIframe() {
async sendMessageToIframe() {
const paddingTop = await getTopSafeAreaHeightAsync();
this.messageId++;
const message = {
type: 0, // 数据类型0数据交互 1App功能调用
data: {
id: this.messageId,
content: `Hello我是 App 发送的消息 ${this.messageId}`,
timestamp: Date.now()
timestamp: Date.now(),
paddingTop: paddingTop
}
};
// 将消息添加到URL参数中
this.urlParams.message = encodeURIComponent(JSON.stringify(message));
this.buildIframeUrl();
console.log('App 发送消息:', message);
console.log('[App]SendMessage=>\n' + JSON.stringify(message));
},
// 接收 web-view 发送的消息
handleMessage(event) {
const webData = event.detail.data[0];
console.log('App 收到消息:', webData);
this.receivedMessages.push(`[${new Date().toLocaleTimeString()}] ${JSON.stringify(webData)}`);
if (webData.type === 0) {
// 数据交互
console.log('收到数据交互消息:', webData.data);
uni.showToast({
title: `收到数据:${webData.data.content || '无内容'}`,
icon: 'none'
});
} else if (webData.type === 1) {
// App功能调用
console.log('收到App功能调用:', webData.data);
uni.showToast({
title: `收到功能调用:${webData.data.action || '未知功能'}`,
icon: 'none'
});
const [ResDetail] = event.detail.data;
this.receivedMessages.push(`[${new Date().toLocaleTimeString()}] ${JSON.stringify(ResDetail)}`);
if (ResDetail.type === 0) {
console.log('[App]ReceiveMessage=>\n' + JSON.stringify(ResDetail.data));
} else if (ResDetail.type === 1) {
console.log('[App]ReceiveMessage=>\n' + JSON.stringify(ResDetail.data));
}
}
}