国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營(yíng)銷資訊 > 網(wǎng)站運(yùn)營(yíng) > 微信掃碼登錄PC網(wǎng)站實(shí)現(xiàn)思路

微信掃碼登錄PC網(wǎng)站實(shí)現(xiàn)思路

時(shí)間:2022-08-06 21:06:02 | 來源:網(wǎng)站運(yùn)營(yíng)

時(shí)間:2022-08-06 21:06:02 來源:網(wǎng)站運(yùn)營(yíng)

思路整理

登錄頁面,當(dāng)點(diǎn)擊微信登錄后,根據(jù)地址url生成一個(gè)二維碼,讓用戶掃描。 在生成二維碼的同時(shí),我在數(shù)據(jù)庫插入一條記錄,然后將ID放在這個(gè)二維碼中。 然后用戶拿著微信去掃描這個(gè)二維碼(地址),后面的就是公眾號(hào)通過網(wǎng)頁獲取用戶信息了.. 獲取到用戶信息后,根據(jù)地址中的ID,查找數(shù)據(jù)庫,更新狀態(tài)和用戶信息 與此同時(shí),在PC端二維碼生成后,一直輪詢向后臺(tái)請(qǐng)求狀態(tài),根據(jù)狀態(tài)獲取對(duì)應(yīng)的結(jié)果 大體思路就是這樣,真實(shí)現(xiàn)起來.. 感覺也就微信公眾號(hào)獲取用戶信息這段稍微麻煩點(diǎn)..畢竟得翻著API才能寫.

需要感受完整功能的可以在 我的億萬店 (百度搜索“源代碼設(shè)計(jì)筆記”)隨便找個(gè)商品,點(diǎn)擊購買的時(shí)候,選擇普通登錄,就會(huì)跳轉(zhuǎn)到我的第三方登錄系統(tǒng)啦

代碼是用nodejs實(shí)現(xiàn)的,后端的接口:

創(chuàng)建二維碼 輪詢狀態(tài) 跳轉(zhuǎn)到第三方地址(這個(gè)大家用的話,可能就是登錄成功后跳轉(zhuǎn)的地址了) 微信公眾號(hào)跳轉(zhuǎn)地址,用于獲取code 微信公眾號(hào)重定向地址,用戶獲取用戶信息,并展示對(duì)應(yīng)的信息 簡(jiǎn)單寫下關(guān)于微信公眾號(hào)獲取用戶信息的代碼吧。

公眾號(hào)獲取用戶信息代碼部分

nodejs - thinkjs - controller/wechat.js

/***

module.exports = class extends Base{

/** * 微信掃描二維碼進(jìn)入該地址,附帶地址信息。 */async indexAction(){ let code = this.query('code'); let redirectURI = this.config('site').domain.value+'/oauth/wechat/redirect'; let appid = this.config('site').wechatappid.value; let scope = 'snsapi_userinfo'; await this.model('lxxx').where({id : code}).update({ status : 1//已掃描 }); let codeUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirectURI}&response_type=code&scope=${scope}&state=${code}#wechat_redirect`; return this.redirect(codeUrl);}/** * 重定向后的地址,換取access_token */async redirectAction(){ let code = this.query('code'); let state = this.query('state'); let appid = this.config('site').wechatappid.value; let secret = this.config('site').wechatappsecret.value; let tokenUrl = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${secret}&code=${code}&grant_type=authorization_code`; let rs = await axios.get(tokenUrl).then(rs=>rs.data); console.log(rs); let access_token = rs.access_token; let openId = rs.openid; let userUrl = `https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openId}&lang=zh_CN` let userInfo = await axios.get(userUrl).then(rs=>rs.data); console.log(userInfo); let {nickname,headimgurl} = userInfo; //查找state let record = await this.model('xxx').where({id : state}).find(); console.log(record); if(!think.isEmpty(record) && openId && userInfo && !userInfo.errmsg){ //獲取用戶信息成功 console.log('登錄成功,更新信息') await this.model('xxx').where({id : state}).update({ json : JSON.stringify(userInfo), code : think.uuid().replace(/-/g,''), name : nickname, openid : openId, avatar : headimgurl, status : 2 }); this.assign('suc',true); }else{ console.log('登錄失敗') await this.model('xxx').where({id : state}).update({status : 4}); this.assign('suc',false); } //此處需要將獲得的state ,然后查找對(duì)應(yīng)的記錄,進(jìn)行數(shù)據(jù)更新。并提示關(guān)閉當(dāng)前頁面。 return this.display('wechat/logintip');}/** * 掃描成功后,進(jìn)入該地址,根據(jù)session進(jìn)行頁面地址跳轉(zhuǎn) */async wechatAction(){ let id = this.query('id'); let record = await this.model('xxx').where({id : id}).find(); //獲取對(duì)應(yīng)的appid let loginInfo = await this.session('loginInfo'); let clientCode = record.code; return this.redirect(loginInfo.redirect_uri+'?code='+clientCode+'&state='+loginInfo.state);}}

關(guān)鍵詞:實(shí)現(xiàn),思路

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉