支付寶小程序踩坑系列:瀏覽器掃碼跳轉(zhuǎn)支付寶小程序

開始之前你需要做的準(zhǔn)備:

  1. 有一個(gè)支付寶小程序!!?。ㄒ呀?jīng)發(fā)布的那種)
  2. 小程序關(guān)聯(lián)一個(gè)二維碼:關(guān)聯(lián)普通二維碼

    注意:這里面也有坑,試著自己解決吧,這里我就不詳細(xì)說明了,可能會(huì)出相關(guān)的文章,到時(shí)候會(huì)把鏈接給大家。

  3. 前提:瀏覽器掃小程序二維碼會(huì)得到一個(gè)404空頁面,所以我們需要?jiǎng)?chuàng)建一個(gè)H5頁面來跳轉(zhuǎn)到支付寶APP再進(jìn)入到小程序里面

開始正題

一、創(chuàng)建一個(gè)html頁面(名稱隨便),在script里面添加
 //用window的onload事件,窗體加載完畢的時(shí)候執(zhí)行
window.onload = function () {
   window.location.href = 'alipays://platformapi/startapp?appId=2021002102670825&page=pages/my-alipay/my-alipay&query=https://jkt.ikanke.cn/category/dD13bHJ5JnJjPXVuZGVmaW5lZCZpZD1jMWVkNGFlZTZhMjY0ZTVkOTA5MDAxMmJlMTgxMWQxMw=='
}
  • alipays://platformapi/startapp? 不變,跳轉(zhuǎn)到支付寶app;
  • appId= 你需要跳轉(zhuǎn)的小程序appId;
  • pages= 你需要跳轉(zhuǎn)到的小程序頁面;
  • query= 你需要傳遞的信息(如無需要可以去掉)。

PS:appId在你的支付寶小程序后臺(tái)里面去找,如下圖:

image.png

二、到這里你可以使用你的瀏覽器掃描二維碼,Safari瀏覽器可以直接跳轉(zhuǎn)到支付寶小程序,但是Android的瀏覽器就不可以,所以我們需要一個(gè)手動(dòng)點(diǎn)擊跳轉(zhuǎn)的按鈕,這里我使用a標(biāo)簽:
<a id="myId" href="alipays://platformapi/startapp?appId=2021002102670825&page=pages/my-alipay/my-alipay">點(diǎn)擊跳轉(zhuǎn)</a>

一個(gè)題外話:如果你的page或者query中含有特殊文字,我們需要進(jìn)行encodeURIComponent()編碼,具體操作可以查看本篇文章!

下面是我的完整代碼,可做參考:
<!DOCTYPE html>
<html lang="zh">
    <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>點(diǎn)擊跳轉(zhuǎn)</title>
        <style>
            html,
            body {
                padding: 0;
                margin: 0;
                width: 100%;
                height: 100%;
            }
            .Hjump {
                height: 100%;
                display: flex;
                flex-direction: column;
                /* justify-content: center; */
                align-items: center;
            }
            .myImg {
                width: 100%;
            }
            .name {
                font-size: 18px;
                margin: 80px 0 40px;
            }
            #myId {
                width: 200px;
                line-height: 46px;
                border-radius: 4px;
                background-color: #14b9c7;
                color: #fff;
                text-align: center;
            }
            #myId:link,
            #myId:visited,
            #myId:active {
                text-decoration: none;
            }
            .tips {
                display: flex;
                align-items: center;
                margin-top: 10px;
            }
            .myIcon {
                width: 16px;
                margin-right: 6px;
            }
            .iconText {
                font-size: 12px;
                color: #a9a9a9;
            }
        </style>
    </head>
    <body>
        <div class="Hjump">
            <img class="myImg" src="./img_banner.png" alt="img" />
            <div class="name">正在跳轉(zhuǎn)支付寶小程序中...</div>
            <a id="myId" href="">點(diǎn)擊跳轉(zhuǎn)</a>
            <div class="tips">
                <img class="myIcon" src="./icon_notice.png" alt="img" />
                <span class="iconText">如不能跳轉(zhuǎn),請(qǐng)點(diǎn)擊上方按鈕前往</span>
            </div>
        </div>
        <script>
            console.log(123, window.location.href)
            var encode = encodeURIComponent('pages/my-alipay/my-alipay')
            var myCode =
                'alipays://platformapi/startapp?appId=2021002102670825&page=' +
                encode +
                '&query=' +
                window.location.href
            document.getElementById('myId').href = myCode
            //用window的onload事件,窗體加載完畢的時(shí)候
            window.onload = function () {
                window.location.href = myCode
            }
        </script>
    </body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容