From 06eb67ad5535a24b573f015fb44a7a4da98e19d9 Mon Sep 17 00:00:00 2001 From: juetan Date: Mon, 7 Aug 2023 19:09:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E5=A4=8D=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/response/response.ts | 6 +++--- src/modules/auth/auth.service.ts | 9 +++------ src/modules/auth/vo/logined-user.vo.ts | 6 +++++- src/modules/user/entities/user.entity.ts | 7 +++++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/common/response/response.ts b/src/common/response/response.ts index 8e233e9..6775c5e 100644 --- a/src/common/response/response.ts +++ b/src/common/response/response.ts @@ -16,7 +16,7 @@ export class Response { * 响应消息 * @example '请求成功' */ - @ApiProperty({ type: 'string', example: '请求成功' }) + @ApiProperty({ type: 'string', example: '操作成功' }) message?: string; /** @@ -41,14 +41,14 @@ export class Response { /** * 创建成功响应结果 */ - static success({ code = ResponseCode.SUCESS, message = '请求成功', ...rest }: Response = {} as any) { + static success({ code = ResponseCode.SUCESS, message = '操作成功', ...rest }: Response = {} as any) { return this.create({ code, message, ...rest }); } /** * 创建失败响应结果 */ - static error(data = null, message = '请求失败') { + static error(data = null, message = '操作失败') { return this.create({ code: ResponseCode.ERROR, message, data }); } diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index abbd665..a83c445 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -9,17 +9,14 @@ export class AuthService { constructor(private userService: UserService, private jwtService: JwtService) {} async signIn(authUserDto: AuthUserDto) { - const user = await this.userService.findByUsername(authUserDto.username); + const { password, ...user } = await this.userService.findByUsername(authUserDto.username); if (!user) { throw new UnauthorizedException('用户名不存在'); } - if (user.password !== authUserDto.password) { + if (password !== authUserDto.password) { throw new UnauthorizedException('密码错误'); } - const loginedUser = new LoginedUserVo(); - for (const key in loginedUser) { - loginedUser[key] = user[key]; - } + const loginedUser = Object.assign(new LoginedUserVo(), user); loginedUser.token = await this.jwtService.signAsync({ id: user.id, username: user.username }); return loginedUser; } diff --git a/src/modules/auth/vo/logined-user.vo.ts b/src/modules/auth/vo/logined-user.vo.ts index 48fd3c8..fea86ae 100644 --- a/src/modules/auth/vo/logined-user.vo.ts +++ b/src/modules/auth/vo/logined-user.vo.ts @@ -1,7 +1,11 @@ import { User } from '@/modules/user'; import { OmitType } from '@nestjs/swagger'; -export class LoginedUserVo extends OmitType(User, ['password'] as const) { +export class LoginedUserVo extends OmitType(User, ['password', 'id'] as const) { + /** + * 用户ID + */ + id: number; /** * 访问令牌 * @example 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MjIsInVzZXJuYW1lIjoianVldGFuIiwiaWF0IjoxNjkxMTM5MjI3LCJleHAiOjE2OTExOTkyMjd9.6z7f-xfsHABbsyg401o2boKeqNQ1epPDYfEdavIcfYc' diff --git a/src/modules/user/entities/user.entity.ts b/src/modules/user/entities/user.entity.ts index bcb9fa7..6e02f7a 100644 --- a/src/modules/user/entities/user.entity.ts +++ b/src/modules/user/entities/user.entity.ts @@ -1,8 +1,8 @@ -import { ApiHideProperty } from '@nestjs/swagger'; -import { Exclude } from 'class-transformer'; import { BaseEntity } from '@/database'; import { Post } from '@/modules/post'; import { Role } from '@/modules/role'; +import { ApiHideProperty } from '@nestjs/swagger'; +import { Exclude } from 'class-transformer'; import { Column, Entity, JoinTable, ManyToMany, RelationId } from 'typeorm'; @Entity({ orderBy: { id: 'DESC' } }) @@ -66,6 +66,9 @@ export class User extends BaseEntity { @JoinTable() roles: Role[]; + /** + * 用户角色ID + */ @RelationId('roles') roleIds: number[]; }