FEAT => 本次更新项目为:

基础配置传递过去了
This commit is contained in:
超级老白兔
2025-08-02 19:58:49 +08:00
parent 947f5c0f6b
commit 65f80c55b0
3 changed files with 46 additions and 18 deletions

View File

@@ -43,7 +43,7 @@
CONNECT: 0, CONNECT: 0,
DATA: 1, DATA: 1,
FUNCTION: 2, FUNCTION: 2,
CONFIG: 3,
} }
export default { export default {
data() { data() {
@@ -56,10 +56,7 @@
} }
}, },
onLoad() { onLoad() {
this.buildIframeUrl(); this.sendBaseConfig()
setTimeout(() => {
this.sendMessageToIframeConnect()
}, 1000);
}, },
methods: { methods: {
// 构建 iframe URL包含参数 // 构建 iframe URL包含参数
@@ -96,13 +93,14 @@
console.log('[App]SendMessage=>\n' + JSON.stringify(message)); console.log('[App]SendMessage=>\n' + JSON.stringify(message));
}, },
// 发送消息到 iframe通过URL传参 // 发送消息到 iframe通过URL传参
async sendMessageToIframeConnect() { async sendBaseConfig() {
const message = { const message = {
type: TYPE_EMUE.CONNECT, type: TYPE_EMUE.CONFIG,
data: { data: {
action: 'ping', paddingTop: await getTopSafeAreaHeightAsync(),
content: '联通测试通过', appId: '1234567890',
timestamp: Date.now() appName: '存客宝',
appVersion: '1.0.0',
} }
}; };

View File

@@ -39,10 +39,16 @@
<script> <script>
import { getTopSafeAreaHeightAsync } from '@utils/common'; import { getTopSafeAreaHeightAsync } from '@utils/common';
const TYPE_EMUE = {
CONNECT: 0,
DATA: 1,
FUNCTION: 2,
}
export default { export default {
data() { data() {
return { return {
baseUrl: 'https://kr-op.quwanzhi.com/iframe', baseUrl: 'https://kr-op.quwanzhi.com/init',
iframeUrl: '', // 动态构建的 URL iframeUrl: '', // 动态构建的 URL
receivedMessages: [], receivedMessages: [],
messageId: 0, messageId: 0,
@@ -51,11 +57,13 @@
}, },
onLoad() { onLoad() {
this.buildIframeUrl(); this.buildIframeUrl();
setTimeout(() => {
this.sendMessageToIframeConnect()
}, 1000);
}, },
methods: { methods: {
// 构建 iframe URL包含参数 // 构建 iframe URL包含参数
buildIframeUrl() { buildIframeUrl() {
const params = []; const params = [];
Object.keys(this.urlParams).forEach(key => { Object.keys(this.urlParams).forEach(key => {
const value = this.urlParams[key]; const value = this.urlParams[key];
@@ -65,7 +73,6 @@
params.push(`${encodedKey}=${encodedValue}`); params.push(`${encodedKey}=${encodedValue}`);
} }
}); });
const queryString = params.join('&'); const queryString = params.join('&');
this.iframeUrl = queryString ? `${this.baseUrl}?${queryString}` : this.baseUrl; this.iframeUrl = queryString ? `${this.baseUrl}?${queryString}` : this.baseUrl;
}, },
@@ -74,7 +81,7 @@
const paddingTop = await getTopSafeAreaHeightAsync(); const paddingTop = await getTopSafeAreaHeightAsync();
this.messageId++; this.messageId++;
const message = { const message = {
type: 0, // 数据类型0数据交互 1App功能调用 type: TYPE_EMUE.DATA, // 数据类型0数据交互 1App功能调用
data: { data: {
id: this.messageId, id: this.messageId,
content: `Hello我是 App 发送的消息 ${this.messageId}`, content: `Hello我是 App 发送的消息 ${this.messageId}`,
@@ -88,14 +95,36 @@
this.buildIframeUrl(); this.buildIframeUrl();
console.log('[App]SendMessage=>\n' + JSON.stringify(message)); console.log('[App]SendMessage=>\n' + JSON.stringify(message));
}, },
// 发送消息到 iframe通过URL传参
async sendMessageToIframeConnect() {
const message = {
type: TYPE_EMUE.CONNECT,
data: {
action: 'ping',
content: '联通测试通过',
timestamp: Date.now()
}
};
// 将消息添加到URL参数中
this.urlParams.message = encodeURIComponent(JSON.stringify(message));
this.buildIframeUrl();
console.log('[App]SendMessage=>\n' + JSON.stringify(message));
},
// 接收 web-view 发送的消息 // 接收 web-view 发送的消息
handleMessage(event) { handleMessage(event) {
console.log("event", event);
const [ResDetail] = event.detail.data; const [ResDetail] = event.detail.data;
this.receivedMessages.push(`[${new Date().toLocaleTimeString()}] ${JSON.stringify(ResDetail)}`); this.receivedMessages.push(`[${new Date().toLocaleTimeString()}] ${JSON.stringify(ResDetail)}`);
if (ResDetail.type === 0) {
console.log('[App]ReceiveMessage=>\n' + JSON.stringify(ResDetail.data)); switch (ResDetail.type) {
} else if (ResDetail.type === 1) { case TYPE_EMUE.DATA:
console.log('[App]ReceiveMessage=>\n' + JSON.stringify(ResDetail.data)); console.log('[App]ReceiveMessage=>\n' + JSON.stringify(ResDetail.data));
break;
case TYPE_EMUE.FUNCTION:
console.log('[App]ReceiveMessage=>\n' + JSON.stringify(ResDetail.data));
break;
} }
} }
} }

View File

@@ -20,6 +20,7 @@ const TYPE_EMUE = {
CONNECT: 0, CONNECT: 0,
DATA: 1, DATA: 1,
FUNCTION: 2, FUNCTION: 2,
CONFIG: 3,
}; };
const IframeDebugPage: React.FC = () => { const IframeDebugPage: React.FC = () => {
const [receivedMessages, setReceivedMessages] = useState<string[]>([]); const [receivedMessages, setReceivedMessages] = useState<string[]>([]);