前端微信H5公眾號實(shí)現(xiàn)授權(quán)登錄的方法總結(jié)
一,公眾號
首先我們需要有一個(gè)微信服務(wù)公眾號


首先賬號是需要時(shí)公眾號下的開發(fā)者
還需要在網(wǎng)頁授權(quán)域名中配置 相應(yīng)的H5正式域名 正式上線是需要用到這個(gè)的
二,重定向code說明
首先我們這為了方便調(diào)試 會將線上和本地調(diào)試 分開進(jìn)行調(diào)試
這樣就需要我們在前端執(zhí)行中 判斷好
我們可以先看這個(gè) 微信網(wǎng)頁授權(quán)的開發(fā)文檔 這個(gè) 重定向登錄 說白了就是 一個(gè)微信授權(quán)
需要跳轉(zhuǎn)這個(gè)鏈接
https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId.value}&redirect_uri=${local}&response_type=code&connect_redirect=1&scope=snsapi_userinfo&state=STATE#wechat_redirect其中的lOCAL 就是微信會幫我們跳轉(zhuǎn)的地址 所以我們需要微信跳轉(zhuǎn)回來哪個(gè)頁面 我們就填哪個(gè)頁面就好了
還是需要編碼的local
let local = `http://h5.liangpiao.net.cn/cdx2.html`; // 獲取頁面url
if (window.location.origin.indexOf("192.168.110.71") !== -1) {
local = `http://h5.liangpiao.net.cn/cdx1.html`; // 獲取頁面url
// 地址轉(zhuǎn)碼
local = encodeURIComponent(local);
// 獲取 code 地址
let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId.value}&redirect_uri=${local}&response_type=code&connect_redirect=1&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
window.location.href = url;看以上的代碼
我們可以看到有兩個(gè)html 文件 一個(gè)是本地的地址 一個(gè)是線上的地址
這個(gè)就是重定向地址
<!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>Document</title>
</head>
<body></body>
</html>
<script>
window.onload = function() {
window.location.href = `http://192.168.110.71:5173/#/pages/login/index/${location.search}`;
};
</script>這個(gè)html 里的內(nèi)容就是一段js
跳轉(zhuǎn)的是
http://192.168.110.71:5173/#/pages/login/index/${location.search}
這個(gè)頁面 就是說微信會重定向到這個(gè)頁面里 ·location.search· 這個(gè) / 后面的參數(shù) 這個(gè)必填 是用來接收查詢參數(shù)的 會有code 參數(shù)在這個(gè)查詢參數(shù)里面
這個(gè)鏈接 是我們本地的調(diào)試 鏈接
如果正式上線 我們需要 把本地IP變成 線上正式地址 但是我們?yōu)榱苏{(diào)試方便 我們可以將這個(gè)兩個(gè)都寫上
三,開發(fā)使用
首先我們寫一個(gè)函數(shù)
const getCode = () => {
let local = `http://xxxxx/cdx2.html`; // 獲取頁面url 線上
if (window.location.origin.indexOf("192.168.110.71") !== -1) {
local = `http://xxxxx/cdx1.html`; // 獲取頁面url 本地
}
// 地址轉(zhuǎn)碼
local = encodeURIComponent(local);
// 獲取 code 地址
let url =
`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId.value}&redirect_uri=${local}&response_type=code&connect_redirect=1&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
window.location.href = url;
// Taro.hideLoading();
};
//用戶重定向登錄
const getUserInfo = async (option) => {
let code = option.code;
let userInfo = uni.getStorageSync("userInfo") ?
uni.getStorageSync("userInfo") :
null;
if (userInfo) {
userInfo = await userApi
.userInfo({
userId: userInfo.id
})
.then((res : any) => res.data);
// if (!userInfo) {
// Taro.removeStorageSync("userInfo");
// }
} else if (code) {
const openId = await userApi
.EmitCodeByH5({
code
})
.then((res : any) => res.data);
openid.value = openId;
uni.setStorageSync("openid", openId);
const UserInfoRes = await userApi
.Login({
userName: openId
})
.then((res : any) => res);
if (UserInfoRes.state === 200) {
userInfo = UserInfoRes.data;
uni.setStorageSync("userInfo", UserInfoRes.data);
userInfo.value = UserInfoRes.data;
userStore.setUserinfo(UserInfoRes.data);
userStore.setToken(UserInfoRes["token"]);
} else if (UserInfoRes.state === 202) {
return
}
}
// 如果 userInfo 為空 代表 本地 沒有 緩存數(shù)據(jù) 且 code 不存在 獲取失效 重新獲取code
if (!userInfo) {
getCode();
return;
} else {
}
};
我們需要做一個(gè)判斷 根據(jù)緩存判斷 微信公眾號 就是依據(jù) 微信的緩存進(jìn)行判斷開發(fā) 為了避免有太多的 重定向跳轉(zhuǎn) 影響用戶體驗(yàn)
這個(gè)需要做好判斷 否則會造成 重定向一直進(jìn)行
總結(jié)
到此這篇關(guān)于前端微信H5公眾號實(shí)現(xiàn)授權(quán)登錄的文章就介紹到這了,更多相關(guān)前端微信H5公眾號授權(quán)登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript 讀取xml,寫入xml 實(shí)現(xiàn)代碼
javascript xml讀取,寫入xml 實(shí)現(xiàn)代碼2009-07-07
JS+DIV+CSS實(shí)現(xiàn)的經(jīng)典標(biāo)簽切換效果代碼
這篇文章主要介紹了JS+DIV+CSS實(shí)現(xiàn)的經(jīng)典標(biāo)簽切換效果代碼,涉及JavaScript基于鼠標(biāo)事件針對頁面元素動(dòng)態(tài)變換的實(shí)現(xiàn)技巧,頁面美觀實(shí)用,需要的朋友可以參考下2015-09-09
JavaScript和Vue分別實(shí)現(xiàn)逐字彈出(打字機(jī))效果
這篇文章主要為大家詳細(xì)介紹了如何通過CSS、JavaScript和Vue分別實(shí)現(xiàn)逐字彈出(打字機(jī))效果,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2024-01-01
移動(dòng)端左右滑動(dòng)切換頁面效果完整代碼(純JavaScript)
這篇文章主要介紹了如何通過原生JavaScript實(shí)現(xiàn)移動(dòng)端左右滑動(dòng)效果,并結(jié)合tab切換卡功能,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
layui 監(jiān)聽彈窗關(guān)閉并刷新父級table的場景分析
這篇文章主要介紹了layui 監(jiān)聽彈窗關(guān)閉并刷新父級table的場景分析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07
js動(dòng)態(tài)修改input輸入框的type屬性(實(shí)現(xiàn)方法解析)
本文是對js動(dòng)態(tài)修改input輸入框的type屬性的實(shí)現(xiàn)方法。進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11
JS驗(yàn)證 只能輸入小數(shù)點(diǎn),數(shù)字,負(fù)數(shù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狫S驗(yàn)證 只能輸入小數(shù)點(diǎn),數(shù)字,負(fù)數(shù)的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
js控制瀏覽器前進(jìn)、后退、頁面跳轉(zhuǎn)詳細(xì)方法和示例
這篇文章主要介紹了js控制瀏覽器前進(jìn)、后退、頁面跳轉(zhuǎn)詳細(xì)方法和示例,包括location.href跳轉(zhuǎn)、history.back/forward前進(jìn)后退、pushState修改歷史記錄,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-07-07

