getOpenPlatform(); $officialAccount = $app->officialAccount($this->storeInfo['g_authorizer_appid'], $this->storeInfo['g_authorizer_refresh_token']); $userObj = $officialAccount->oauth->userFromCode($code); // 获取 OAuth 授权结果用户信息 $userArr = $userObj->toArray(); $wx_unionid = $userArr['raw']['unionid'] ?? ''; $wx_h5_openid = $userArr['raw']['openid']; //根据openid判断该用户是否已经存在,如果存在则登录跳转 $flag = false; $materialService = new MaterialService(new EavSetService($this->memberSetAlias)); $memberInfo = $materialService->elasticClientService()->queryOne(['store_id' => $this->storeId, 'wx_h5_openid' => $wx_h5_openid]); if ($memberInfo) { $flag = true; } else { if ($wx_unionid) { $memberInfo = $materialService->elasticClientService()->queryOne(['store_id' => $this->storeId, 'wx_unionid' => $wx_unionid]); if ($memberInfo) { $flag = true; } } } $toUrl = 'https://' . $state . '.tiaotiaoyu168.com'; if ($flag) { // 更新信息 $materialService->update($memberInfo['id'], ['wx_unionid' => $wx_unionid, 'wx_h5_openid' => $wx_h5_openid, 'wx_info' => json_encode($userArr)]); $loginService = new LoginService($this->siteAliasName); $encryptArr = $loginService->setLoginCookie($memberInfo); //微信登录的时候,保存的微信接口返回的微信用户信息 $key = 'wechat:userInfo:' . $encryptArr['token']; Cache::put($key, $userArr, 7200); // 随机值保存,前端通过该值获取登录凭证,防止在url显示登录凭证 $code = md5(microtime() . mt_rand()); Cache::put('WX_CODE:' . $code, $encryptArr, 7200); $url = toRoute($toUrl . '/pages/public/login_empty?&code=' . $code); } else { //该用户不存在,要求用户先绑定手机号 $key = 'wechat:userInfo:' . $code; Cache::put($key, $userArr, 7200); $url = toRoute($toUrl . '/pages/public/login_empty?&code=' . $code . '&is_need=1'); } Cache::forget($wechat_cache_key); return redirect($url); } /** * 微信公账号登录,客户绑定手机号 */ public function bindIn() { $requestData = Request::all(); $rule = [ 'key' => 'required', 'mobile' => 'required', 'sms_code' => 'required', ]; $msg = [ 'key.required' => '参数错误!', 'mobile.required' => '请填写手机号!', 'sms_code.required' => '请填写6位短信验证码!', 'sms_code.size' => '短信验证码必须是6个字符!', ]; $validator = Validator::make($requestData, $rule, $msg); if ($validator->fails()) { $errorMessage = $validator->errors()->all(); return response()->json(['result' => false, 'code' => 2001, 'msg' => $errorMessage[0] ?? '']); } //判断验证短信验证码是否正确 $result_bool = $this->smsService->checkSmsCode($requestData['mobile'], $requestData['sms_code']); if (!$result_bool) { return $this->responseMessage(2002, '短信验证码错误,请确认后再试!'); } //获取微信的信息 $key2 = 'wechat:userInfo:' . $requestData['key']; $weInfo = Cache::get($key2); if (empty($weInfo)) { return $this->responseMessage(2003, '微信登录失败,请重试!'); } $wx_h5_openid = $weInfo['raw']['openid']; $token = $this->saveBindAppMember($weInfo, $requestData['mobile'], '', $wx_h5_openid); return $this->responseMessage(1001, 'success', $token); } }