jwt = $jwt; $this->userRepository = $userRepository; } /** * 小程序认证 * @param WeappAuthorizationRequest $request * User: Mead */ public function weappLogin(WeappAuthorizationRequest $request, ConfigRepository $configRepository, InviteNewUserRepository $inviteNewUserRepository) { try { $code = $request->code; $appid = $request->appid; $invite_user_id = $request->get('invite_user_id') ?? 0; $miniProgram = app('wechat.mini_program'); $data = $miniProgram->auth->session($code); if (isset($data['errcode'])) { $this->response->errorUnauthorized('code 不正确'); return ''; } $auth = Auth::where('identifier', $appid)->where('credential', $data['openid'])->first(); $is_auth = true; if (!$auth) { //注册用户 $userInfo = $this->userRepository->getRandomUserInfo(); $userInfo['register_source'] = User::REGISTER_SOURCE_WEAPP; $user = User::create($userInfo); //添加邀请注册活动 if ((int)$invite_user_id != 0) { $inviteNewUserRepository->create($invite_user_id, $user->id); } //注册认证信息 $attributes['type'] = Auth::TYPE_WEAPP; $attributes['identifier'] = $appid; $attributes['credential'] = $data['openid']; $attributes['user_id'] = $user->id; $attributes['is_verified'] = Auth::VERIFIED_OK; Auth::create($attributes); $is_auth = false; $auth = Auth::where('identifier', $appid)->where('credential', $data['openid'])->first(); } else { $user = $this->userRepository->byIdGetModel($auth->user_id); } $phone_detail = $request->get('phone_detail') ?? ''; if (!empty($phone_detail)) { $detail = json_decode($phone_detail); if (!empty($detail)) { UserPhoneDetail::Log($detail, $user->id); } } $token = JWTAuth::fromUser($user); // 单机登录限制 app()->redis->hset(CacheMap::SINGLE_LOGIN_API, $user->id, $token); Cache::put('login:user:session_key:auth:' . $user->id, $data['session_key'], 1440); return $this->response->array([ 'is_auth' => $is_auth, 'token' => 'Bearer ' . $token, 'session_key' => '', 'exp' => Carbon::now()->addMinute(JWTAuth::factory()->getTTL())->getTimestamp(), 'auth_id' => $auth->id, 'user' => $is_auth ? $user : [], 'android_mini_version' => $configRepository->getMiniAndroidVersion(), 'ios_mini_version' => $configRepository->getMiniIosVersion(), ]); } catch (\Exception $exception) { return $this->errorNoValidation($exception->getMessage()); } } /** * mobileLogin 手机号登录 * * @param MobileLoginRequest $request * @param ConfigRepository $configRepository * @return void * @author Fx * */ public function mobileLogin(MobileLoginRequest $request, ConfigRepository $configRepository) { try { $mobile = $request->get('mobile'); $code = $request->get('code'); $v_code = Cache::get("verification_code_{$mobile}", ''); if (empty($v_code)) { return $this->errorNoValidation('验证码已过期'); } if ((string)$v_code !== (string)$code) { return $this->errorNoValidation('验证码错误'); } $user = User::query()->where('mobile', $mobile)->first(); $is_auth = true; if (empty($user)) { return $this->errorNoValidation('找不到该用户,或用户没有绑定此手机号'); } //注册认证信息 $auth = Auth::query()->where('user_id', $user->id)->first(); if (!$auth) { $is_auth = false; } $phone_detail = $request->get('phone_detail') ?? ''; if (!empty($phone_detail)) { $detail = json_decode($phone_detail); if (!empty($detail)) { UserPhoneDetail::Log($detail, $user->id); } } $token = JWTAuth::fromUser($user); // 单机登录限制 app()->redis->hset(CacheMap::SINGLE_LOGIN_API, $user->id, $token); return $this->response->array([ 'token' => 'Bearer ' . $token, 'exp' => Carbon::now()->addMinute(JWTAuth::factory()->getTTL())->getTimestamp(), 'auth_id' => $auth->id ?? '', 'user' => $is_auth ? $user : [], 'android_mini_version' => $configRepository->getMiniAndroidVersion(), 'ios_mini_version' => $configRepository->getMiniIosVersion(), ]); } catch (\Exception $exception) { return $this->errorNoValidation($exception->getMessage()); } } /** * 更新用户基本信息 * User: Mead */ public function weappUserInfoSync(WeappUserinfoRequest $request) { try { $this->user->fill([ 'nickname' => $request->get('nickName'), 'gender' => $request->get('gender', 0), 'country' => $request->get('country', null), 'province' => $request->get('province', null), 'city' => $request->get('city', null), 'avatar' => $request->get('avatarUrl', null) ?? "http://resource.weilaigo.l4j.cn/avatarUrlDefault.jpeg", 'language' => $request->get('language', 'zh_CN'), 'register_area_id' => $request->get('area_id', 0), 'is_register' => User::REGISTER_OK ]); $this->user->save(); return $this->response->item($this->user, UserTransformer::class); } catch (\Exception $exception) { $this->response->error($exception->getMessage(), 401); } } /** * 获取当前登录用户 * @return \Dingo\Api\Http\Response * User: Mead */ public function loginUser() { try { return $this->response->item($this->user, UserTransformer::class); } catch (\Exception $exception) { return $this->errorNoValidation($exception->getMessage()); } } }