您當前位置>首頁 » 新聞資訊 » 公衆号相(xiàng)關 >
uniapp中h5網頁微(wēi)信公衆号授權
發表時(shí)間(jiān):2020-10-19
發布人(rén):葵宇科(kē)技(jì)
浏覽次數(shù):303
uniapp微(wēi)信網頁授權
- uniapp中h5網頁微(wēi)信公衆号授權
- 主要(yào)代碼
- 獲取code返回的(de)code截取代碼
uniapp中h5網頁微(wēi)信公衆号授權
微(wēi)信官方文(wén)檔–>網頁授權
uniapp中h5網頁微(wēi)信公衆号授權步驟:
1.采用(yòng)用(yòng)戶授權獲取code
2.把code傳給後端後端獲取openid 以及是(shì)否關注公衆号判斷
3.沒有(yǒu)關注跳(tiào)轉至關注公衆号頁面
主要(yào)代碼
//判斷用(yòng)戶是(shì)否是(shì)微(wēi)↑α信環境
if (isWechat()) {
let code = getUrlParam("code"); //是(shì)否存在code 截取code代碼 授權會(huì)返回coΩ">de需要(yào)截取鏈接中code
let local = window.location.href;
if (code == null || code === "") {
//不(bù)存在就(jiù)打開(kāi)上(shàng)✔∏÷面的(de)地(dì)址進行(xíng)授權
window.location.href =
`https://open.weixin.qq.com/connect/oa®♣uth2/authorize?appid=appid&redirec§¶÷§t_uri=url&response_type≈♣☆1;code&scope=snsapi_userinfo&φ¥$§state=STATE&connect_redirect=1#wechat₩∏§_redirect`;
//appid填寫你(nǐ)的(de)appid redirect_uri填寫請(qǐng)求成™功後回調地(dì)址
} else {
that.code = code;
//把code傳給後端判斷用(yòng)戶是(shì)否關注相(xiàng)對 (duì)應的(de)公衆号
uni.request({
url: 'url',
header: {
'content-type': 'application/x-www-form-url¶✘encoded'
},
data: {
code: that.code
},
method: 'GET',
success: (res) => {
//201沒有(yǒu)關注公衆号 跳(tiào)轉關注頁面
if (res.data == 201) {
window.location.href =
`https://mp.weixin.qq.₩♦com/mp/profile_ext?action&"≥#61;home&__biz=wechatbiz#wechat_redirect&£α↔#96;;
//_biz的(de)獲取通(tōng)過登錄微(wēi)信公衆平台 在頭像那(÷☆♥λnà)裡(lǐ)右擊查看(kàn)源碼 找到(dào) uin: αβπ4;658565",uin_base64: •£"",_biz的(de)值等于uλ✘πin_base64就(jiù)可(kě)以了(le)
} else { //關注了(le)可(kě)以進行(xíng)下(xià)一(yī)步
uni.request({
url: 'url',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: 'GET',
success: (ti) => {
}
})
}
},
})
}
}else{
uni.showModal({
title:'請(qǐng)在微(wēi)信打開(kāi σ)',
content:'請(qǐng)在微(wēi)信打開(kāi)本網頁'
})
}
獲取code返回的(de)code截取代碼
// 判斷公衆号截取code
const getUrlParam = (name) => {
let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"✘α;);
let r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}