} * * @throws ValidationException 账号或密码不对 */ public function signIn(string $login, string $password): array { $user = UserInfo::where('username', $login) ->orWhereRaw('lower(email) = ?', [mb_strtolower($login)]) ->first(); if (! $user || ! $this->hasher->check($password, (string) $user->password)) { throw ValidationException::withMessages(['login' => __('auth.failed')]); } $token = AuthService::getUserToken($user->userid); if (! $token) { // getUserToken 只在查无此人时返回 null,而上面刚查到——走到这里是数据不一致 abort(500); } return [ 'token' => $token, 'user' => $this->profile($user), ]; } /** * 当前登录用户的资料。token 有效但账号已不存在(被删)时 401。 * * @return array */ public function me(string $userUid): array { $user = UserInfo::where('userid', $userUid)->first(); if (! $user) { abort(401); } return $this->profile($user); } /** * 公开摘要(与 v3 其他资源里的 user 同形)+ 本人才需要的补充: * roles 恒为数组(没有就是 []),root 管理员固定是 ['root']——与 v2 auth/current 一致。 * * @return array */ private function profile(UserInfo $user): array { $profile = $this->users->profile($user); $profile['roles'] = $user->userid === config('mint.admin.root_uuid') ? ['root'] : (is_array($profile['roles'] ?? null) ? $profile['roles'] : []); $profile['avatar'] ??= null; $profile['email'] = $user->email; return $profile; } }