這篇文章主要介紹微信小程序如何授權(quán)獲取用戶詳細(xì)信息openid,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
小程序獲取用戶的頭像昵稱openid之類
第一種使用wx.getUserInfo直接獲取微信頭像,昵稱
wx.getUserInfo({ success: function (res) { that.setData({ nickName: res.userInfo.nickName, avatarUrl: res.userInfo.avatarUrl, }) }, })
第二種
我們?cè)谑褂眯〕绦騱x.login API進(jìn)行登錄的時(shí)候,直接使用wx.getUserInfo是不能獲取更多的信息的,如微信用戶的openid。
官方提示,需要發(fā)送獲取到的code進(jìn)行請(qǐng)求到微信的后端API,進(jìn)行用戶解密之類的操作才可以獲取,
根據(jù)文檔,只需要進(jìn)行一個(gè)get請(qǐng)求到如下地址即可:
https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code appid和secret在微信小程序后臺(tái)可以看到,js_code為使用wx.login登錄時(shí)獲取到的code參數(shù)數(shù)據(jù),grant_type這個(gè)不用改動(dòng)。
js文件
var openId = (wx.getStorageSync('openId')) if (openId) { wx.getUserInfo({ success: function (res) { that.setData({ nickName: res.userInfo.nickName, avatarUrl: res.userInfo.avatarUrl, }) }, fail: function () { // fail console.log("獲取失??!") }, complete: function () { // complete console.log("獲取用戶信息完成!") } }) } else { wx.login({ success: function (res) { console.log(res.code) if (res.code) { wx.getUserInfo({ withCredentials: true, success: function (res_user) { wx.request({ //后臺(tái)接口地址 url: 'https://....com/wx/login', data: { code: res.code, encryptedData: res_user.encryptedData, iv: res_user.iv }, method: 'GET', header: { 'content-type': 'application/json' }, success: function (res) { // this.globalData.userInfo = JSON.parse(res.data); that.setData({ nickName: res.data.nickName, avatarUrl: res.data.avatarUrl, }) wx.setStorageSync('openId', res.data.openId); } }) }, fail: function () { wx.showModal({ title: '警告通知', content: '您點(diǎn)擊了拒絕授權(quán),將無(wú)法正常顯示個(gè)人信息,點(diǎn)擊確定重新獲取授權(quán)。', success: function (res) { if (res.confirm) { wx.openSetting({ success: (res) => { if (res.authSetting["scope.userInfo"]) {////如果用戶重新同意了授權(quán)登錄 wx.login({ success: function (res_login) { if (res_login.code) { wx.getUserInfo({ withCredentials: true, success: function (res_user) { wx.request({ url: 'https://....com/wx/login', data: { code: res_login.code, encryptedData: res_user.encryptedData, iv: res_user.iv }, method: 'GET', header: { 'content-type': 'application/json' }, success: function (res) { that.setData({ nickName: res.data.nickName, avatarUrl: res.data.avatarUrl, }) wx.setStorageSync('openId', res.data.openId); } }) } }) } } }); } }, fail: function (res) { } }) } } }) }, complete: function (res) { } }) } } }) } }, globalData: { userInfo: null }
后臺(tái)是php 框架是laravel5.4版本
官方文檔:
https://mp.weixin.qq.com/debug/wxadoc/dev/api/signature.html
微信官方提供了多種編程語(yǔ)言的示例代碼(點(diǎn)擊下載)。每種語(yǔ)言類型的接口名字均一致。調(diào)用方式可以參照示例。
下載之后在php文件中引入:
<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Models\User; use App\Models\Wechatuser; include_once app_path('/Http/Controllers/Admin/PHP/wxBizDataCrypt.php'); // 獲取微信用戶信息 public function getWxLogin(Request $request) { // require_once ROOTPATH . "./PHP/wxBizDataCrypt.php"; $code = $request->get('code'); $encryptedData = $request->get('encryptedData'); $iv = $request->get('iv'); $appid = "***" ; $secret = "***"; $URL = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code"; $apiData=file_get_contents($URL); // var_dump($code,'wwwwwwww',$apiData['errscode']); // $ch = curl_init(); // curl_setopt($ch, CURLOPT_URL, $URL); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // curl_setopt($ch, CURLOPT_HEADER, 0); // $output = curl_exec($ch); // curl_close($ch) if(!isset($apiData['errcode'])){ $sessionKey = json_decode($apiData)->session_key; $userifo = new \WXBizDataCrypt($appid, $sessionKey); $errCode = $userifo->decryptData($encryptedData, $iv, $data ); if ($errCode == 0) { return ($data . "\n"); } else { return false; } } }
官方文檔的登錄流程圖,整個(gè)登錄流程基本如下圖所示:
以上是“微信小程序如何授權(quán)獲取用戶詳細(xì)信息openid”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
本文標(biāo)題:微信小程序如何授權(quán)獲取用戶詳細(xì)信息openid-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://jinyejixie.com/article20/digpjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、移動(dòng)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容