valid([ "code" => "required", ]); $sessionData = app(WechatService::class)->codeToSession($params['code']); $token = app(AuthService::class)->authByOpenid($sessionData['openid']); return $this->ok([ "token" => $token, "openid" => $sessionData['openid'], "sessionKey" => $sessionData['session_key'], ]); } public function decryptUserInfo() { $params = $this->valid([ "iv" => "required", "encrypted_data" => "required", "session_key" => "required", ]); app(WechatService::class)->decryptUserInfo($params); /** @var User $user */ $user = \Auth::user(); return $this->ok($user->format()); } public function updateUserInfo() { $params = $this->valid([ "avatar" => "string", "name" => "string", "email" => "string", "captcha" => "string", "password" => "string", ]); if ($captcha = Arr::get($params, 'captcha')) { if (!app(CaptchaService::class)->checkCaptcha($params['email'], $captcha)) { throw new ClientException("验证码错误"); } } $user = app(AuthService::class)->updateUserInfo(Auth::user(), $params); return $this->ok($user->format()); } public function decryptPhone() { $params = $this->valid([ "iv" => "required", "encrypted_data" => "required", "session_key" => "required", ]); app(WechatService::class)->decryptPhone($params); /** @var User $user */ $user = \Auth::user(); return $this->ok($user->format()); } public function profile() { /** @var User $user */ $user = Auth::user(); return $this->ok([ "id" => $user->id, "token" => app(AuthService::class)->encryptToken($user->id), "name" => $user->name, "avatar" => $user->avatar, "company" => [ "id" => optional($user->company)->id ?? 0, "name" => optional($user->company)->name ?? "", ], "email" => $user->email ?? "", "phone" => $user->phone ?? "", "cart_total" => Cart::where("user_id", $user->id)->count(), "fav_total" => UserFav::where("user_id", $user->id)->count(), ]); } public function test() { $id = request()->input("id"); return $this->ok(app(AuthService::class)->encryptToken($id)); } public function emailCaptcha() { $params = $this->valid([ "email" => "required|email", "source" => "required", ]); return $this->ok(app(EmailService::class)->emailCaptcha($params['email'], $params['source'])); } public function register() { $params = $this->valid([ "email" => "required|email", "phone" => new Phone(), "password" => "required", "captcha" => "required", ]); if (!app(CaptchaService::class)->checkCaptcha($params['email'], $params['captcha'])) { throw new ClientException("验证码错误"); } $user = app(AuthService::class)->register($params); return $this->ok($user->format()); } public function login() { $params = $this->valid([ "userinfo" => "required", "password" => "required", ]); $user = app(AuthService::class)->login($params); return $this->ok($user->format()); } public function resetPassword() { $params = $this->valid([ "email" => "required|email", "password" => "required|min:6", "captcha" => "required", ]); if (!app(CaptchaService::class)->checkCaptcha($params['email'], $params['captcha'])) { throw new ClientException("验证码错误"); } $user = app(AuthService::class)->resetPassword($params); return $this->ok($user->format()); } }