code2session($this->code); if($res['code'] != 0) return $res; $session_key = $res['data']['session_key']; $res = $this->decrypted_data($session_key); if($res['code'] != 0){ return $res; } $res['data'] = json_decode($res['data'],true); if(!isset($res['data']['purePhoneNumber'])){ return [ 'code' => 1, 'msg' => '数据解析失败', 'data' => $res['data'] ]; } $mobile = $res['data']['purePhoneNumber'];//不带区号的手机号 $mobile_prefix = $res['data']['countryCode']; $data = [ 'code' => 0, 'msg' => 'ok', 'data' => [ 'mobile' => $mobile, 'mobile_prefix' => $mobile_prefix ] ]; return $data; } /*** * 用户数据解密 */ private function decrypted_data($session_key){ $pc = new WxBizDataCrypt($this->wechat_mp->appId, $session_key); $errCode = $pc->decryptData($this->encrypted_data, $this->iv, $data ); if ($errCode == 0) { return [ 'code' => 0, 'msg' => 'success', 'data' => $data ]; } else { return [ 'code' => 1, 'msg' => $errCode, ]; } } private function code2session($code) { $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wechat_mp->appId}&secret={$this->wechat_mp->appSecret}&js_code={$code}&grant_type=authorization_code"; $this->wechat_mp->curl->get($api); if($this->wechat_mp->curl->error_code != 0){ return [ 'code' => 1, 'msg' => "err_code:{$this->wechat_mp->curl->error_code}err_msg:{$this->wechat_mp->curl->error_msg}" ]; } $resp = $this->wechat_mp->curl->response; $res = json_decode($resp, true); if(!isset($res['openid'])){ return [ 'code' => 1, 'msg' => isset($res['errmsg']) ? $res['errmsg'] : 'error' ]; } return [ 'code' => 0, 'msg' => 'ok', 'data' => $res ]; } /** * @ 获取js-sdk前端所需参数 */ public function actionGetJssdk(){ $redis_name = "api:{$this->wechat_mp->appId}:actionGetJssdk_1"; $get = \Yii::$app->redis->get($redis_name); if(empty($get)){ $accessToken = $this->wechat_mp->getAccessToken(); // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$accessToken; $this->wechat_mp->curl->get($url); $res = json_decode($this->wechat_mp->curl->response); $get = $res->ticket; \Yii::$app->redis->setex($redis_name,6800,$get); } $uniq = uniqid(); $time = time(); $this->url = urldecode($this->url); $str = "jsapi_ticket={$get}&noncestr={$uniq}×tamp={$time}&url={$this->url}"; $sign = sha1($str); $return = [ 'appId' => $this->wechat_mp->appId, 'timestamp' => $time, 'nonceStr' => $uniq, 'signature' => $sign, 'jsApiList' => [ 'scanQRCode', ], 'url' => $this->url, ]; return $this->apiReturnSuccess('ok',$return); } }