微信小程式獲取使用者手機號
阿新 • • 發佈:2018-12-20
官方文件地址如下
實現思路比較簡單,以下是我的js程式碼
getPhoneNumber: function (e) { var that=this; if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { wx.showModal({ title: '提示', showCancel: false, content: '未授權,請手動填寫', success: function (res) { } }) } else { wx.request({ url: app.globalData.url + '/routine/ydj/get_phone?uid=' + app.globalData.uid, method: 'POST', data: { encryptedData: e.detail.encryptedData, iv: e.detail.iv, }, success: function (res) { if (res.data.code == 200) { that.setData({ userphone : res.data.data.phoneNumber }) } } }) } }
介面程式碼
public function get_phone($uid,$encryptedData,$iv) { $sessionKey=Db::table('ydj_wechat_user')->where('uid',$uid)->value('session_key'); if (strlen($sessionKey) != 24) { return JsonService::fail('sessionKey錯誤!'); } $aesKey=base64_decode($sessionKey); if (strlen($iv) != 24) { return JsonService::fail('iv錯誤!'); } $aesIV=base64_decode($iv); $aesCipher=base64_decode($encryptedData); $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); $dataObj=json_decode( $result,true); Db::table('ydj_user')->where('uid',$uid)->update(array('phone'=>$dataObj['phoneNumber'])); return JsonService::successful('請求成功',$dataObj); }