開始之前你需要做的準(zhǔn)備:
- 有一個(gè)支付寶小程序!!?。ㄒ呀?jīng)發(fā)布的那種)
- 小程序關(guān)聯(lián)一個(gè)二維碼:關(guān)聯(lián)普通二維碼
注意:這里面也有坑,試著自己解決吧,這里我就不詳細(xì)說明了,可能會(huì)出相關(guān)的文章,到時(shí)候會(huì)把鏈接給大家。
- 前提:瀏覽器掃小程序二維碼會(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>