继前面登录之后,有做手机号码授权。获取用户手机号码。
注意: 微信文档所示
在回调中调用 wx.login 登录,可能会刷新登录态。此时服务器使用 code 换取的 sessionKey 不是加密时使用的 sessionKey,导致解密失败。建议开发者提前进行 login;或者在回调中先使用 checkSession 进行登录态检查,避免 login 刷新登录态。
也就是在请求这个接口之前需要,先请求前面的userId接口//获取用户手机号码授权 public function userMobile() { $encryptedData = $this->input->post('encryptedData'); if (empty($encryptedData)){ return $this->fail('','encryptedData不能为空'); } $iv = $this->input->post('iv'); if (empty($iv)){ return $this->fail('','iv不能为空'); } $uid = $this->input->post('uid'); if (empty(intval($uid))){ return $this->fail('','uid不能为空'); } $appid = "自己的appid" ; //取登录时的sessionKey $mc = &load_cache('redis'); $sessionKey= $mc->get('session_key'); include_once APPPATH."/third_party/WeChat/wxBizDataCrypt.php"; $userifo = new WXBizDataCrypt($appid, $sessionKey); $errCode = $userifo->decryptData($encryptedData, $iv, $data ); if ($errCode == 0) { $data = json_decode($data,true); $userData = [ 'mobile' =>$data['phoneNumber'], ]; $where['id'] = $uid; $result = $this->AppUserModel->update($userData,$where); if ($result){ return $this->success('','手机号码已授权'); }else{ return $this->fail('','手机号码授权失败'); } }else{ return $this->fail($errCode,'获取用户手机号码失败'); } }