您好,登錄后才能下訂單哦!
小編給大家分享一下小程序獲取手機(jī)號(hào)的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
1、直接上代碼php
namespace Home\Controller; use Think\Controller; class ApiController extends Controller { /** * error code 說(shuō)明. * <ul> * <li>-41001: encodingAesKey 非法</li> * <li>-41003: aes 解密失敗</li> * <li>-41004: 解密后得到的buffer非法</li> * <li>-41005: base64加密失敗</li> * <li>-41016: base64解密失敗</li> * </ul> */ public static $OK = 0; public static $IllegalAesKey = -41001; public static $IllegalIv = -41002; public static $IllegalBuffer = -41003; public static $DecodeBase64Error = -41004; // 小程序 public static $appid = 'XXX'; //小程序appid public static $secret = 'XXX'; //小程序秘鑰 public $sessionKey =''; // 獲取openId session-key 等 public function getopenId($value='') { $code = I('post.code'); $appid = self::$appid; $secret = self::$secret; $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='. $appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code'; $result = httpGet($url); $res = json_decode($result); // session(['sessionKey'=>$res,'expire'=>7200]); $this->ajaxReturn($res); } // 獲取小程序手機(jī)號(hào)api 接口,對(duì)應(yīng)下面小程序 js public function getPhoneNumber($value='') { $encryptedData = I('get.encryptedData'); $iv = I('get.iv'); $this->sessionKey=I('get.session_key'); $res = $this->decryptData($encryptedData, $iv); // $res = json_decode($res); if($res->phoneNumber){ // $res->phoneNumbe 就是手機(jī)號(hào)可以 寫(xiě)入數(shù)據(jù)庫(kù)或者做其他操作 } $this->ajaxReturn(['msg'=>$res,'status'=>'1']); //把手機(jī)號(hào)返回 } // 小程序解密 public function decryptData($encryptedData, $iv) { if (strlen($this->sessionKey) != 24) { return self::$IllegalAesKey; } $aesKey=base64_decode($this->sessionKey); if (strlen($iv) != 24) { return self::$IllegalIv; } $aesIV=base64_decode($iv); $aesCipher=base64_decode($encryptedData); $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); $dataObj=json_decode( $result ); if( $dataObj == NULL ) { return self::$IllegalBuffer; } if( $dataObj->watermark->appid != self::$appid ) { return self::$IllegalBuffer; } return $dataObj; // return self::$OK; } function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); // 為保證第三方服務(wù)器與微信服務(wù)器之間數(shù)據(jù)傳輸?shù)陌踩?,所有微信接口采用https方式調(diào)用,必須使用下面2行代碼打開(kāi)ssl安全校驗(yàn)。 // 如果在部署過(guò)程中代碼在此處驗(yàn)證失敗,請(qǐng)到 http://curl.haxx.se/ca/cacert.pem 下載新的證書(shū)判別文件。 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } }
//2、小程序
2.1在app.js 啟動(dòng)頁(yè)面里先login
// 登錄 // if (!wx.getStorageSync('session_key') || wx.getStorageSync('time') < Date.parse(new Date())){ // 判斷session_key是不是存在獲者過(guò)期 wx.login({ success: res => { console.log(res) // 發(fā)送 res.code 到后臺(tái)換取 openId, sessionKey, unionId wx.request({ url: 'https://www.zhixiaobing.com/index.php?m=&c=api&a=getopenId', header: { "Content-Type": "application/x-www-form-urlencoded" }, method: 'post', data: { code: res.code }, success: function (res) { console.log(res.data); wx.setStorageSync('openid', res.data.openid) wx.setStorageSync('session_key', res.data.session_key) wx.setStorageSync('time', parseInt(Date.parse(new Date())) + 7200) } }) } })
//2.2 在小程序模板里寫(xiě)組件
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" >//這是官方的組件點(diǎn)擊會(huì)彈出授權(quán)頁(yè)面
在js里寫(xiě)下面的函數(shù)
getPhoneNumber: function (e) { var that =this; var session_key = wx.getStorageSync('session_key') if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { wx.showModal({ title: '提示', showCancel: false, content: '未授權(quán)', success: function (res) { } }) } else {//確認(rèn)授權(quán) wx.request({ url: 'https://www.showoow.com/index.php?m=mini&c=api&a=getPhoneNumber&openid=' + wx.getStorageSync('openid'), //openid是app.js 已經(jīng)存的 header: {"Content-Type": "application/x-www-form-urlencoded" }, method: "get", data: { encryptedData: e.detail.encryptedData, iv: e.detail.iv, session_key:session_key }, success:function(res){ if (res.data.msg.phoneNumber){ console.log(res); wx.showModal({ title: '提示', showCancel: false, content: '授權(quán)成功', success: function () { wx.setStorageSync('phoneNumber', res.data.msg.phoneNumber); var time = Date.parse(new Date()) + 60 * 60 * 24 * 2 wx.setStorageSync('exp', time ); } }) setTimeout(function(){ wx.navigateTo({ url: '/pages/form/form', }) },1500); that.setData({ show:'show', hiden:'' }) }else{ wx.showToast({ title: '授權(quán)失敗', icon:'loading' }) } }, fail:function(){ wx.showToast({ title: '授權(quán)失敗', icon: 'loading' }) } }) } },
看完了這篇文章,相信你對(duì)小程序獲取手機(jī)號(hào)的方法有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。