時(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)
/** * 微信掃描二維碼進(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),思路
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。