一.h5傳值給app
1,在h5項(xiàng)目的mian.js中引入
// #ifdef H5
import '@/common/webview_sdk.js'
// #endif
//此處需要注意的是如果項(xiàng)目本身要打包成app,記得使用條件編譯,否則在app運(yùn)行時(shí)本次引入會(huì)報(bào)錯(cuò)
2.h5頁面給app傳值部分
otherAppMethod(e){
?? ??? ??? ??? ?uni.webView.postMessage({
?? ??? ??? ??? ??? ?data:{
?? ??? ??? ??? ??? ??? ?action:e
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?})
?? ??? ??? ?},
3.app頁面接收
html
<web-view :src="webUrl" ref="webview" @message="message"></web-view>
js由于我做的是掃碼
message(event) {
console.log(event, '這樣還不行嗎')
let _this=this
//(`getParams(${JSON.stringify(res)})`)
uni.scanCode({
onlyFromCamera: true, // 是否只能從相機(jī)掃碼,不允許從相冊(cè)選擇圖片 如果想用相冊(cè)選擇圖片掃碼 需要注釋掉
// scanType: ["barCode"], // 調(diào)起條碼掃描
success(CodeRes) {
let codeResult = "";
codeResult = CodeRes.result.split(":");
if (codeResult[0] === "RU" && codeResult[1] === "INSPECT") {
uni.showToast({
title: "獲取位置信息中...",
icon: "none",
duration: 5000,
});
uni.getLocation({
type: "wgs84",
geocode: true, // 設(shè)置該參數(shù)為true可直接獲取經(jīng)緯度及城市信息
isHighAccuracy: true,
success: (pointSuccess) => {
uni.showToast({
title: "加載數(shù)據(jù)中...",
icon: "none",
duration: 5000,
});
outOfRange({
riskUnitId: codeResult[2],
longitude: pointSuccess
.longitude,
latitude: pointSuccess
.latitude,
}).then((res) => {
uni.hideToast()
if (res) {
if (res.data.flag) {
// uni.navigateTo({
// url: "/pages/dangerQuick/viewSaoInvestigation/index?inspectId=" +codeResult[2] +"&source=index",
// });
// this.webUrl =
// 'http://192.168.1.77:8081/views/login/index?action=' +
// 2
console.log(codeResult[2],'掃出來的啥')
_this.sanweb(codeResult[2])
} else {
uni.showToast({
title: res
.data
.message ||
"您不在規(guī)定范圍內(nèi),請(qǐng)前往掃描",
icon: "none",
});
}
}
});
},
fail: (pointFail) => {
uni.showToast({
title: "獲取地址失敗,請(qǐng)打開位置信息。并重新登錄!",
duration: 5000,
icon: "none",
});
},
});
} else {
// _this.sanweb(codeResult[2])
uni.showToast({
title: "請(qǐng)掃描正確二維碼",
icon: "none",
});
}
},
});
// 對(duì)從web-view中接收到的消息進(jìn)行處理
// console.log('收到來自web-view的消息:', event.detail.data)
// // 將消息發(fā)送到uni-app中的事件總線
// EventBus.$emit('toOption', event.detail.data)
},
sanweb(e){
this.currentWebview = this.$scope.$getAppWebview().children()[0]
console.log(this.$scope.$getAppWebview().children()[0],'兩邊與偶啥')
// this.currentWebview.evalJS("uniEvent()");
let res={
action:1,
code:e
}
this.currentWebview.evalJS(`uniEvent(${JSON.stringify(res)})`)
}
二.掃碼過后app向h5傳值并跳轉(zhuǎn)
1.app發(fā)起傳值
在“一“的app接收h5傳值部分我也寫了
sanweb(e){
this.currentWebview = this.$scope.$getAppWebview().children()[0]
console.log(this.$scope.$getAppWebview().children()[0],'兩邊與偶啥')
// this.currentWebview.evalJS("uniEvent()");
let res={
action:1,
code:e
}
this.currentWebview.evalJS(`uniEvent(${JSON.stringify(res)})`)
}
2.h5接收app傳過來的值文章來源:http://www.zghlxwxcb.cn/news/detail-785360.html
onShow() {
// #ifdef H5
let dianziHetong = null;
window.uniEvent = function(data) {
console.log('app發(fā)來的數(shù)據(jù)2', data.action)
if(data.action==1){
uni.navigateTo({
url: "/pages/dangerQuick/viewSaoInvestigation/index?inspectId=" +data.code +"&source=index",
});
}
}
// #endif
}
至此互相通訊完畢文章來源地址http://www.zghlxwxcb.cn/news/detail-785360.html
到了這里,關(guān)于關(guān)于uniapp的app和uniapp的H5互相通訊實(shí)現(xiàn)H5調(diào)取app掃碼再傳入H5(app內(nèi)嵌H5,webView)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!