When the html page hosted by the uniapp applet using web-view is uniapp H5, H5 communicates with the applet

Recently, web-view was used in a small program project, and the H5 carried by the web-view needed to communicate with the small program. There was a big pitfall, so write about the implementation process and how to avoid the pitfall.

1. The applet transfers parameters to the H5 hosted by the web-view, just add the parameters directly after the url ( xxxxx.com )

2. H5 sends a message to the applet
  1. Create a template html in the root directory of the uniapp project

2. Then introduce the WeChat SDK and uni SDK into the template html; note that there is a trap, the official SDK link provided by uniapp is wrong (it’s been a long time for me) , use this: https://js.cdn.aliyun.dcloud .net.cn/dev/uni-app/uni.webview.1.5.2.js , the template html code is as follows


<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>
            <%= htmlWebpackPlugin.options.title %>
        </title>
        <!-- Open Graph data -->
        <!-- <meta property="og:title" content="Title Here" /> -->
        <!-- <meta property="og:url" content="http://www.example.com/" /> -->
        <!-- <meta property="og:image" content="http://example.com/image.jpg" /> -->
        <!-- <meta property="og:description" content="Description Here" /> -->
        <script>
            var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)'))
            document.write('<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (coverSupport ? ', viewport-fit=cover' : '') + '" />')
        </script>
        <link rel="stylesheet" href="<%= BASE_URL %>static/index.<%= VUE_APP_INDEX_CSS_HASH %>.css" />
    </head>
    <body>
        <noscript>
            <strong>Please enable JavaScript to continue.</strong>
        </noscript>
        <div id="app"></div>
        <!-- built files will be auto injected -->
    </body>
    
    <!-- 可更换,两个SDK都可使用最近更新的版本 -->
    <!-- 微信 JS-SDK 如果不需要兼容小程序,可以不引用 JS 文件。 两个文件同时引入时,微信的需要在前-->
    <script type="text/javascript" src="//res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>  
    <!-- uni 的 SDK,必须引用。 -->  
    <script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script>
    
    <script>
        //执行UniAppJSBridgeReady,通过后才可调用uni的api
        document.addEventListener('UniAppJSBridgeReady', function() {
            console.log(uni)
            console.log('uni.webView', uni.webView)
            uni.webView.getEnv(function(res) {
                console.log('当前环境:' + JSON.stringify(res));
            });
        });
    </script>
</html>

3. Introduce the template html you created in manifest.json

4. Then you can use it on your page


<template>
    <view class="layout">
        <view class="content">
            <view class="footer-box">
                <view class="confirm" @click="confirm">确认</view>
            </view>
        </view>
    </view>
</template>
<!-- <script src="./index.js"></script> -->

<script>
    export default {
        data() {
            return {
                
            }
        },
        onLoad() {
            
        },

        methods: {
            confirm() {
                //调用 uni.webView.postMessage 方法,向uniapp发送消息 postMessage ,
                //uniapp会在特定时机(后退、组件销毁、分享)触发并收到消息

                //先调用 uni.webView.navigateBack ,接着再调用 uni.webView.postMessage 
                //就会触发并接收到消息(调用顺讯不要反了,不然会触发不了发送消息)
                uni.webView.navigateBack()
                uni.webView.postMessage({
                    data: {
                        action: 'message' //发送的消息
                    }
                })
            },
        }
    }
</script>

5. Finally, just receive the message in the web-view


<template>
    <view>
        <web-view :webview-styles="webviewStyles" :src="webViewUrl" @message="getMssage"></web-view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                webviewStyles: {
                    progress: {
                        color: ' rgba(0, 126, 255, 0.99)'
                    }
                },
                webViewUrl: null
            }
        },
        
        onLoad() {
            //小程序仅支持加载网络网页,不支持本地html
            //因为是小程序所以需要进行 decodeURIComponent
            this.webViewUrl = decodeURIComponent('https://baidu.com?params=aaa')
        },

        methods: {
            // 在uniap中以 @message 触发并收到消息
            getMssage(e) {
                //接收到的消息 e.detail.data 数据格式为数组
                console.log('收到网页发来的消息', e.detail.data[0].action)
            },
        },
    }
</script>

Guess you like

Origin blog.csdn.net/Samuel_OY/article/details/129281263