validate()){ return $this->getModelError(); } $res = FlashStorage::getCache("m{$this->cx_mch_id}_c{$this->code}"); if($this->code_expires == 1 || $res === false){ $res = $this->code2session($this->code); if($res['code'] != 0) return $res; FlashStorage::setCache("m{$this->cx_mch_id}_c{$this->code}",$res,864000); } $session_key = $res['data']['session_key']; $openid = $res['data']['openid']; $res = $this->decrypted_data($session_key); if($res['code'] != 0){ return $res; } $decrypted_info = json_decode($res['data'], true); $openid = $openid ? $openid : $decrypted_info['openId']; $nickname = $decrypted_info['nickName']; $avatar_url = empty($decrypted_info['avatarUrl']) ? User::DEFAULT_AVATAR_URL : $decrypted_info['avatarUrl']; $gender = $decrypted_info['gender']; $unionid = isset($decrypted_info['unionId']) ? $decrypted_info['unionId'] : '0'; //用户是否存在 $user_oauth = UserOauth::findOne([ 'cx_mch_id' => $this->cx_mch_id, 'is_delete' => 0, 'type' => SysConst::$cxOauthProviderWxmp, 'openid' => $openid ]); if($user_oauth != null){ /*if($user_oauth->user_id == $this->user_id){ return $this->apiReturnSuccess("绑定成功"); } $mobile = $user_oauth->user && !empty($user_oauth->user->mobile_phone) ? EncryptHelper::decryptMobilePhone($user_oauth->user->mobile_phone) : null; $account_name = $mobile ? $mobile : ($user_oauth->user ? $user_oauth->user->username : ""); if(strlen($account_name) != 0) $account_name = Utils::stringDesensitization ($account_name,2,-2); return $this->apiReturnError("绑定失败,此微信已绑定{$account_name}账号");*/ $user_oauth->user_id = $this->user_id; $user_oauth->unionid = $unionid; $user_oauth->created_at = time(); $user_oauth->nickname = $nickname; $user_oauth->avatar_url = $avatar_url; }else{ $user_oauth = new UserOauth(); $user_oauth->cx_mch_id = $this->cx_mch_id; $user_oauth->type = SysConst::$cxOauthProviderWxmp; $user_oauth->user_id = $this->user_id; $user_oauth->openid = $openid; $user_oauth->unionid = $unionid; $user_oauth->is_delete = 0; $user_oauth->created_at = time(); $user_oauth->nickname = $nickname; $user_oauth->avatar_url = $avatar_url; } // 删除用户信息缓存 $obj = new WindowsApiForm(); $cache = $obj->getUserCacheName($this->user_id); foreach ($cache as $key=>$val){ FlashStorage::deleteCache($val); } if(!$user_oauth->save()){ return $this->getModelError($user_oauth); } return $this->apiReturnSuccess("绑定成功"); } /*** * 用户数据解密 */ 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 ]; } }