前端微信H5公眾號實現(xiàn)授權(quán)登錄的方法總結(jié)
一,公眾號
首先我們需要有一個微信服務(wù)公眾號
首先賬號是需要時公眾號下的開發(fā)者
還需要在網(wǎng)頁授權(quán)域名中配置 相應(yīng)的H5正式域名 正式上線是需要用到這個的
二,重定向code說明
首先我們這為了方便調(diào)試 會將線上和本地調(diào)試 分開進行調(diào)試
這樣就需要我們在前端執(zhí)行中 判斷好
我們可以先看這個 微信網(wǎng)頁授權(quán)的開發(fā)文檔 這個 重定向登錄 說白了就是 一個微信授權(quán)
需要跳轉(zhuǎn)這個鏈接
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)回來哪個頁面 我們就填哪個頁面就好了
還是需要編碼的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;
看以上的代碼
我們可以看到有兩個html 文件 一個是本地的地址 一個是線上的地址
這個就是重定向地址
<!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>
這個html 里的內(nèi)容就是一段js
跳轉(zhuǎn)的是
http://192.168.110.71:5173/#/pages/login/index/${location.search}
這個頁面 就是說微信會重定向到這個頁面里 ·location.search· 這個 / 后面的參數(shù) 這個必填 是用來接收查詢參數(shù)的 會有code 參數(shù)在這個查詢參數(shù)里面
這個鏈接 是我們本地的調(diào)試 鏈接
如果正式上線 我們需要 把本地IP變成 線上正式地址 但是我們?yōu)榱苏{(diào)試方便 我們可以將這個兩個都寫上
三,開發(fā)使用
首先我們寫一個函數(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 { } };
我們需要做一個判斷 根據(jù)緩存判斷 微信公眾號 就是依據(jù) 微信的緩存進行判斷開發(fā) 為了避免有太多的 重定向跳轉(zhuǎn) 影響用戶體驗
這個需要做好判斷 否則會造成 重定向一直進行
總結(jié)
到此這篇關(guān)于前端微信H5公眾號實現(xiàn)授權(quán)登錄的文章就介紹到這了,更多相關(guān)前端微信H5公眾號授權(quán)登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js使用循環(huán)清空某個div中的input標(biāo)簽值
清空div中input標(biāo)簽值的方法有很多,下面為大家介紹下使用循環(huán)清空某個div中的input標(biāo)簽值的具體實現(xiàn)2014-09-09用javascript替換URL中的參數(shù)值示例代碼
本篇文章主要是對用javascript替換URL中的參數(shù)值示例代碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01