釘釘小程序web-view內(nèi)嵌H5頁面并實(shí)現(xiàn)通信
前言
今天探討個不太常見的需求,公司需要在釘釘小程序內(nèi)進(jìn)行文件的上傳,意圖打造一個小型云盤,考慮到釘釘?shù)纳蟼鞔笮〉扔兄T多限制,以及數(shù)據(jù)的安全性,決定在釘釘內(nèi)使用web-view嵌套H5上傳頁面,并且在完成上傳動作后,需要返回小程序,并執(zhí)行其他操作。
1.引入頁面
在管理端新建頁面,同時在釘釘頁面使用web-view引入,需要后端配合傳入合適的token。
- 釘釘頁面引入
<template>
<view class="">
<web-view id="web-view-1" :src="`http://10.10.5.231:9529/myNetwork?x-token=${token}`"></web-view>
</view>
</template>2.在H5頁面向釘釘發(fā)送消息
H5頁面使用dd.postMessage()進(jìn)行消息發(fā)送,并使用 dd.navigateTo()進(jìn)行頁面的跳轉(zhuǎn)。
<template>
<div>
<el-button @click="handleToDT" >返回并發(fā)送消息</el-button>
</div>
</template>
<script>
export default {
data() {
return {
}
},
created() {
var userAgent = navigator.userAgent
if (userAgent.indexOf('AlipayClient') > -1) {
// 支付寶小程序的 JS-SDK 防止 404 需要動態(tài)加載,如果不需要兼容支付寶小程序,則無需引用此 JS 文件。
document.writeln('<script src="https://appx/web-view.min.js"' + '>' + '<' + '/' + 'script>')
},
methods: {
handleToDT() {
// 網(wǎng)頁向小程序 postMessage 消息
dd.postMessage({ name: '測試web-view' })
setTimeout(()=>{
dd.navigateTo({ url: '/pages/index/myNetwork/index' })
},500)
},
}
},
</script>釘釘頁面使用@message進(jìn)行消息的接受,但很坑的是,文檔上接收方法為onMessage,但uniapp中需要改為@message才能接收到消息。

<template>
<view class="">
<web-view id="web-view-1" :src="`http://10.10.5.231:9529/myNetwork?x-token=${token}`" @message="test"></web-view>
</view>
</template>
<script>
export default {
data() {
return {
webViewContext: '',
token: uni.getStorageSync('x-token')
}
},
onLoad(e){
},
methods: {
test(e){
console.log(e)
}
},
}
</script>
<style>
</style>3.在釘釘頁面向H5發(fā)送消息,繼而實(shí)現(xiàn)雙向通信
釘釘頁面創(chuàng)建實(shí)例,并調(diào)用this.webViewContext.postMessage()方法發(fā)送消息
<template>
<view class="">
<web-view id="web-view-1" :src="`http://10.10.5.231:9529/myNetwork?x-token=${token}`" @message="test"></web-view>
</view>
</template>
<script>
export default {
data() {
return {
webViewContext: '',
token: uni.getStorageSync('x-token')
}
},
onLoad(e){
this.webViewContext = dd.createWebViewContext('web-view-1');
},
methods: {
test(e){
this.webViewContext.postMessage({'sendToWebView': '1'});
}
},
}
</script>
<style>
</style>H5頁面在mounted中使用dd.onMessage接收消息
mounted() {
// 接收來自小程序的消息。
dd.onMessage = function(e) {
console.log(e); //{'sendToWebView': '1'}
}
},4.注意 內(nèi)容調(diào)試方式
釘釘頁面在控制臺查看數(shù)據(jù)即可 H5數(shù)據(jù)調(diào)試控制臺開啟方式


5.附雙向通信全部代碼
釘釘頁面(即uniapp編寫頁面)
<template>
<view class="">
<web-view id="web-view-1" :src="`http://10.10.5.231:9529/myNetwork?x-token=${token}`" @message="test"></web-view>
</view>
</template>
<script>
export default {
data() {
return {
webViewContext: '',
token: uni.getStorageSync('x-token')
}
},
onLoad(e){
this.webViewContext = dd.createWebViewContext('web-view-1');
},
methods: {
test(e){
console.log(e)
this.webViewContext.postMessage({'sendToWebView': '1'});
}
},
}
</script>
<style>
</style>H5頁面(即掛載到管理端頁面)
<template>
<div>
<el-button @click="handleToDT" >返回并發(fā)送消息</el-button>
</div>
</template>
<script>
export default {
data() {
return {
}
},
created() {
var userAgent = navigator.userAgent
if (userAgent.indexOf('AlipayClient') > -1) {
// 支付寶小程序的 JS-SDK 防止 404 需要動態(tài)加載,如果不需要兼容支付寶小程序,則無需引用此 JS 文件。
document.writeln('<script src="https://appx/web-view.min.js"' + '>' + '<' + '/' + 'script>')
}
},
mounted() {
// 接收來自小程序的消息。
dd.onMessage = function(e) {
console.log(e); //{'sendToWebView': '1'}
}
},
methods: {
handleToDT() {
// javascript
// 網(wǎng)頁向小程序 postMessage 消息
dd.postMessage({ name: '測試web-view' })
setTimeout(()=>{
dd.navigateTo({ url: '/pages/index/myNetwork/index' })
},500)
},
}
}
</script>
<style lang="scss" scoped>
</style>釘釘小程序文檔 https://open.dingtalk.com/document/isvapp-client/web-view
到此這篇關(guān)于釘釘小程序web-view內(nèi)嵌H5頁面并實(shí)現(xiàn)通信的文章就介紹到這了,更多相關(guān)web-view內(nèi)嵌H5內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS在onclientclick里如何控制onclick的執(zhí)行
這篇文章主要介紹了JS在onclientclick里如何控制onclick的執(zhí)行的相關(guān)資料,需要的朋友可以參考下2016-05-05
javascript阻止事件冒泡和瀏覽器的默認(rèn)行為
本篇文章主要介紹了javascript阻止事件冒泡和瀏覽器的默認(rèn)行為。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
JS中實(shí)現(xiàn)隱藏部分姓名或者電話號碼的代碼
最近做了小項(xiàng)目,項(xiàng)目需要只顯示用戶的姓名和手機(jī)號開頭跟結(jié)尾,其他部分用*號代替,下面小編給大家分享一段簡單的代碼,需要的朋友跟隨腳本之家小編一起看看吧2018-07-07
詳解js實(shí)現(xiàn)線段交點(diǎn)的三種算法
下面小編就最近學(xué)會的一些”求線段交點(diǎn)”的算法說一說, 希望對大家有所幫助。“求線段交點(diǎn)”是一種非?;A(chǔ)的幾何計(jì)算, 在很多游戲中都會被使用到。有需要的可以參考學(xué)習(xí)2016-08-08
js 獲取經(jīng)緯度的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨s 獲取經(jīng)緯度的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
JavaScript Event學(xué)習(xí)第二章 Event瀏覽器兼容性
在這一章我將對重要的事件做一個概述,包括一些流行的瀏覽器的兼容性問題。2010-02-02

