5.30
This commit is contained in:
parent
328e470c27
commit
8cc5a7bcdd
@ -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>
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
18
src/main.js
18
src/main.js
@ -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
|
||||||
|
|
||||||
|
@ -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时)
|
||||||
|
@ -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));
|
||||||
};
|
};
|
||||||
// 下拉刷新事件处理函数
|
// 下拉刷新事件处理函数
|
||||||
|
Loading…
x
Reference in New Issue
Block a user