微信公众号开发

微信公众号h5实现点击按钮跳转到小程序

本文主要是介绍微信公众号h5实现点击按钮跳转到小程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

微信公众号h5实现点击按钮跳转到小程序,先决条件是小程序和公众号已进行关联。
1、登录微信公众平台-设置-关联设置 进行关联
2、登录微信公众平台-设置-基本设置-账号信息-原始ID 复制
3、代码如下

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试页</title>
    <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            border: 0;
        }

        body {
            width: 100%;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            box-sizing: border-box;
            padding: 0 28px;
        }

        .app {
            text-align: center;
        }

        .app-info {
            margin-bottom: 93px;
            font-size: 24px;
        }

        .main {
            color: #0072FF;
            line-height: 45px;
            letter-spacing: 2px;
        }

        .sub {
            color: #263047;
            letter-spacing: 1px;
        }

        .app-img {
            width: 100%;
            object-fit: contain;
        }

        .app-btn {
            width: 247px;
            line-height: 45px;
            text-align: center;
            background: #0072FF;
            border-radius: 23px;
            color: #FFFFFF;
            font-size: 15px;
            font-weight: bold;
            margin: 130px auto 0;
            user-select: none;
        }

        .hidden {
            display: none;
        }
    </style>
</head>

<body>
    <div id="app" class="app">
        <div id="wechat-container">
            
        </div>
    </div>
    <script>
        function docReady(fn) {
            if (document.readyState === 'complete' || document.readyState === 'interactive') {
                fn()
            } else {
                document.addEventListener('DOMContentLoaded', fn);
            }
        }
        docReady(async function () {
            const ua = navigator.userAgent.toLowerCase(); // 浏览器ua识别类型
            const isWXWork = ua.match(/wxwork/i) == 'wxwork'; // 判断是否有微信线程
            const isWeixin = !isWXWork && ua.match(/micromessenger/i) == 'micromessenger'; // 是否是微信浏览器
            // 判断是否是桌面浏览器
            let isDesktop = false
            if (!navigator.userAgent.match(
                    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|IEMobile)/i)) {
                isDesktop = true
            }
            if(isDesktop){
                var cu = document.getElementById('wechat-container');
                cu.innerHTML = '<div class="app-btn">请在手机微信里扫码访问</div>';
                // alert("请用手机打开")
            }else if(isWeixin){
                getSignature();
            } else {
                var cu = document.getElementById('wechat-container');
                cu.innerHTML = '<div class="app-btn">请在手机微信里扫码访问</div>';
                // cu.innerText = '请在手机微信里扫码访问';
            }
        });
        // 获取签名,必备步骤与后端配和
        function getSignature() {
            const url = 'XXX';
            const xhr = new XMLHttpRequest();
            xhr.open('GET', url, true);
            xhr.send();
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    const result = JSON.parse(xhr.responseText) || {};
                    if (result.code === 1) {
                        wx.config({
                            debug: false,
                            appId: , // 必填 公众号appid
                            timestamp: , // 必填,生成签名的时间戳
                            nonceStr: '', // 必填,生成签名的随机串
                            signature: , // 必填,签名
                            jsApiList: ['chooseImage'],
                            openTagList: ['wx-open-launch-weapp'],
                        })
                        wx.ready(()=>{
                            var cu = document.getElementById('wechat-container'); 
                            let id = getQueryVariable('id');
                            // 动态创建跳转按钮   username传入第二步复制的小程序原始ID   path为需要跳转的小程序页面路径    scene为页面带的参数可在小程序页面进行获取
                            cu.innerHTML= `<wx-open-launch-weapp id="launch-btn" username="" path="/pages/index/index?scene=${id}"> 
                                            <script type="text/wxtag-template">
                                              <div 
                                                class="app-btn" 
                                                style="width: 247px;
                                                line-height: 45px;
                                                text-align: center;
                                                background: #0072FF;
                                                border-radius: 23px;
                                                color: #FFFFFF;
                                                font-size: 15px;
                                                font-weight: bold;
                                                margin: 130px auto 0;
                                                user-select: none;">立即体验</div>
                                            <\/script>
                                          </wx-open-launch-weapp>`;
                        })
                        wx.error((error)=>{
                            // alert(JSON.stringify(error))
                        })
                    }
                }
            }
        }
    </script>
</body>

</html>

这篇关于微信公众号h5实现点击按钮跳转到小程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!