這篇文章主要為大家展示了“Thinkphp5微信小程序如何實(shí)現(xiàn)獲取用戶信息接口”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Thinkphp5微信小程序如何實(shí)現(xiàn)獲取用戶信息接口”這篇文章吧。
在吳川等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營(yíng)銷網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),營(yíng)銷型網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站制作,吳川網(wǎng)站建設(shè)費(fèi)用合理。
Thinkphp5微信小程序獲取用戶信息接口的實(shí)例詳解
首先在官網(wǎng)下載示例代碼, 選php的,
官方的php文件,編碼是UTF-8+的, 所以要把文件改為UTF-8
然后在Thinkphp5 extend文件夾下建立Wxxcx命名空間,把官方的幾個(gè)類文件放進(jìn)去(這里要注意文件夾名, 命名空間名, 類名的, 大小寫,一定要一樣,官方的文件名和類名大小寫不一樣)
然后是自己的thinkphp接口代碼:
<?php /** * Created by PhpStorm. * User: leeoo * Date: 2017/9/14 0014 * Time: 10:43 */ namespace app\api\controller\v1; use think\Loader; use think\Request; use Workerman\Protocols\Http; use Wxxcx\WXBizDataCrypt; use first\second\Foo; class Index { public function index($id) { return json(['msg' => $id]); } public function dologin() { $code = Request::instance()->param('code'); $encryptedData = Request::instance()->param('encryptedData'); $iv = Request::instance()->param('iv'); $appid = "你的小程序appid"; $secret = "你的小程序secret"; //appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code $param = array( 'appid' => $appid, 'secret' => $secret, 'js_code' => $code, 'grant_type' => 'authorization_code' ); //http函數(shù)為封裝的請(qǐng)求函數(shù) $res = http("https://api.weixin.qq.com/sns/jscode2session", $param, 'post'); $arr = json_decode($res, true); $result = $this->wxdecode($encryptedData, $iv, $arr['session_key'], $appid); //return json($result); if ($result) { return json(['code' => 1]); } else { return json(['code' => -1]); } } public function wxdecode($encryptedData, $iv, $sessionKey, $appid) { //Loader::import('Wxxcx\WXBizDataCrypt', EXTEND_PATH); $pc = new WXBizDataCrypt($appid, $sessionKey); $data = null; $errCode = $pc->decryptData($encryptedData, $iv, $data); //echo $data; //return json(['data'=>$data]); $data = json_decode($data); if ($errCode == 0) { //print($data . "\n"); //dump($data); return $data; } else { //print($errCode . "\n"); //dump($errCode); return $errCode; } } }
http封裝函數(shù):
/** * 發(fā)送HTTP請(qǐng)求方法 * @param string $url 請(qǐng)求URL * @param array $params 請(qǐng)求參數(shù) * @param string $method 請(qǐng)求方法GET/POST * @return array $data 響應(yīng)數(shù)據(jù) */ function http($url, $params, $method = 'GET', $header = array(), $multi = false){ $opts = array( CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HTTPHEADER => $header ); /* 根據(jù)請(qǐng)求類型設(shè)置特定參數(shù) */ switch(strtoupper($method)){ case 'GET': $opts[CURLOPT_URL] = $url . '?' . http_build_query($params); break; case 'POST': //判斷是否傳輸文件 $params = $multi ? $params : http_build_query($params); $opts[CURLOPT_URL] = $url; $opts[CURLOPT_POST] = 1; $opts[CURLOPT_POSTFIELDS] = $params; break; default: throw new Exception('不支持的請(qǐng)求方式!'); } /* 初始化并執(zhí)行curl請(qǐng)求 */ $ch = curl_init(); curl_setopt_array($ch, $opts); $data = curl_exec($ch); $error = curl_error($ch); curl_close($ch); if($error) throw new Exception('請(qǐng)求發(fā)生錯(cuò)誤:' . $error); return $data; }
然后是小程序的代碼:
// 獲取用戶信息 wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { // 已經(jīng)授權(quán),可以直接調(diào)用 getUserInfo 獲取頭像昵稱,不會(huì)彈框 wx.getUserInfo({ success: res => { console.log(res); var encryptedData = res.encryptedData var iv = res.iv wx.request({ url: "https://你的服務(wù)器地址/dologin",//dologin是訪問后端的方法 method: "post", data: { code: code, encryptedData: encryptedData, iv: iv }, success: function (ret) { console.log(ret); } }) // 可以將 res 發(fā)送給后臺(tái)解碼出 unionId this.globalData.userInfo = res.userInfo // 由于 getUserInfo 是網(wǎng)絡(luò)請(qǐng)求,可能會(huì)在 Page.onLoad 之后才返回 // 所以此處加入 callback 以防止這種情況 if (this.userInfoReadyCallback) { this.userInfoReadyCallback(res) } } }) } } }) },
以上是“Thinkphp5微信小程序如何實(shí)現(xiàn)獲取用戶信息接口”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前文章:Thinkphp5微信小程序如何實(shí)現(xiàn)獲取用戶信息接口
當(dāng)前網(wǎng)址:http://www.2m8n56k.cn/article16/gsejdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、微信小程序、標(biāo)簽優(yōu)化、建站公司、靜態(tài)網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(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;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)