This commit is contained in:
JayJiaJun 2025-05-30 16:25:17 +08:00
parent 328e470c27
commit 8cc5a7bcdd
5 changed files with 57 additions and 11 deletions

View File

@ -7,6 +7,7 @@
<link rel="icon" href="<%= BASE_URL %>favicon.ico"> <link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title><%= htmlWebpackPlugin.options.title %></title> <title><%= htmlWebpackPlugin.options.title %></title>
<script src="./web/js/webView.js"></script> <script src="./web/js/webView.js"></script>
<script src="./uni.webview.1.5.6.js"></script>
</head> </head>
<body> <body>
<noscript> <noscript>

View File

@ -69,7 +69,7 @@ function MQTT_json_send(string, protocol) {
} else { } else {
//这里需要将string发送出去 //这里需要将string发送出去
window.parent.postMessage({ window.parent.postMessage({
str: send_string data: send_string
}, "*"); // 替换为Vue应用的URL }, "*"); // 替换为Vue应用的URL
} }
} }

View File

@ -4,6 +4,7 @@ import App from './App.vue';
import router from './router'; import router from './router';
import store from './store'; import store from './store';
import '@vant/touch-emulator'; import '@vant/touch-emulator';
import '../public/uni.webview.1.5.6.js';
// import '../../set-vue/src/assets/icon/iconfont.js'; // import '../../set-vue/src/assets/icon/iconfont.js';
import '../src/assets/icon/iconfont' import '../src/assets/icon/iconfont'
// const VConsole = require('vconsole'); // const VConsole = require('vconsole');
@ -74,6 +75,23 @@ app.use(store).use(router).mount('#app');
window.VueRouter = router; // 这里立即暴露 router window.VueRouter = router; // 这里立即暴露 router
console.log("VueRouter 已暴露到全局"); console.log("VueRouter 已暴露到全局");
// main.js
if (window.uni && uni.postMessage) {
console.log('WebView SDK 已加载');
}
// 示例:监听 UniApp 原生事件
document.addEventListener('UniAppJSBridgeReady', () => {
uni.postMessage({
data: '子页面已准备好'
});
});
if (uni.getEnv().plus) {
console.log('运行在 UniApp WebView 中');
}
// qiankun // qiankun

View File

@ -67,6 +67,7 @@ export default {
// //
onMounted(() => { onMounted(() => {
const messageHandler = (event) => { const messageHandler = (event) => {
console.log(' 收到父页面消息:', event);
console.log(' 收到父页面消息:', event.data); console.log(' 收到父页面消息:', event.data);
// XXXXXXXX/XXX // XXXXXXXX/XXX

View File

@ -112,19 +112,45 @@ export default {
// //
const isLocal = window.location.hostname === '192.168.4.1'; const isLocal = window.location.hostname === '192.168.4.1';
// const MQTT_send = (send_string) => {
// console.log(" :", JSON.stringify(send_string));
// // 使 postMessage
// window.parent.postMessage(
// {
// data: send_string
// },
// '*' // origin
// );
// };
const MQTT_send = (send_string) => { const MQTT_send = (send_string) => {
console.log(" 向父页面发送消息:", JSON.stringify(send_string)); console.log("向父页面发送消息:", JSON.stringify(send_string));
// 使 postMessage
window.parent.postMessage( //
{ if (typeof uni !== 'undefined' && uni.postMessage) {
data: send_string // /APP使uni.postMessage
}, uni.postMessage({
'*' // origin data: {
); type: 'mqtt_data',
payload: send_string,
timestamp: Date.now()
}
});
} else {
// H5使window.parent.postMessage
window.parent.postMessage(
{
data: send_string
},
'*' // origin
);
}
}; };
const handleMQTTMessage = (string) => { const handleMQTTMessage = (string) => {
console.log("MQTT 接收:" + string); // console.log("MQTT :" + string);
const data = JSON.parse(string); const data = JSON.parse(string);
// console.log("MQTT :", JSON.stringify(data, null, 2)); // JSON // console.log("MQTT :", JSON.stringify(data, null, 2)); // JSON
@ -250,7 +276,7 @@ export default {
const parsedString = JSON.parse(string); const parsedString = JSON.parse(string);
// //
const data = typeof parsedString === 'string' ? JSON.parse(parsedString) : parsedString; const data = typeof parsedString === 'string' ? JSON.parse(parsedString) : parsedString;
console.log("MQTTRECV 接收的json:" + JSON.stringify(data)); // console.log("MQTTRECV json:" + JSON.stringify(data));
handleMQTTMessage(JSON.stringify(data)); handleMQTTMessage(JSON.stringify(data));
}; };
// //