| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2024-06-17 19:19:20 |
| 4 | * @LastEditTime: 2025-05-06 15:50:54 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: 用户路由 |
| 7 | */ |
| 8 | import { Body, Controller, Delete, Get, Put } from '@nestjs/common'; |
| 9 | import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; |
| 10 | import { TokenInfo } from '../auth/interfaces/auth.interfaces'; |
| 11 | import { UpdateUserInfoDto, UserResDto, LoginByPhoneDto } from './dto/user.dto'; |
| 12 | import { UserService } from './user.service'; |
| 13 | import { ErrHttpBack } from '../filters/http-exception.back-code'; |
| 14 | import { AppHttpException } from '../filters/http-exception.filter'; |
| 15 | import { LoginService, LoginTypeCacheKey } from './login.service'; |
| 16 | import { GetToken } from '../auth/auth.guard'; |
| 17 | import { ParamsValidationPipe } from '../validation.pipe'; |
| 18 | import { RedisService } from 'src/lib/redis/redis.service'; |
| 19 | |
| 20 | @ApiTags('user - 用户') |
| 21 | @Controller('user') |
| 22 | export class UserController { |
| 23 | constructor( |
| 24 | private readonly userService: UserService, |
| 25 | private readonly loginService: LoginService, |
| 26 | private readonly redisService: RedisService, |
| 27 | ) {} |
| 28 | |
| 29 | @ApiOperation({ |
| 30 | description: '获取自己的用户信息', |
| 31 | summary: '获取自己的用户信息', |
| 32 | }) |
| 33 | @ApiResponse({ status: 200, type: UserResDto }) |
| 34 | @Get('mine') |
| 35 | getUserInfoById(@GetToken() token: TokenInfo) { |
| 36 | return this.userService.getUserInfoById(token.id); |
| 37 | } |
| 38 | |
| 39 | @ApiOperation({ description: '更新用户信息', summary: '更新用户信息' }) |
| 40 | @ApiResponse({ status: 200, type: UserResDto }) |
| 41 | @Put('info/update') |
| 42 | async updateInfo( |
| 43 | @GetToken() token: TokenInfo, |
| 44 | @Body(new ParamsValidationPipe()) body: UpdateUserInfoDto, |
| 45 | ) { |
| 46 | const userInfo = await this.userService.getUserInfoById(token.id); |
| 47 | |
| 48 | for (const key in body) { |
| 49 | if (Object.prototype.hasOwnProperty.call(body, key)) { |
| 50 | userInfo[key] = body[key]; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | this.userService.updateUser(token.id, userInfo); |
| 55 | return userInfo; |
| 56 | } |
| 57 | |
| 58 | @ApiOperation({ description: '用户注销', summary: '用户注销' }) |
| 59 | @ApiResponse({ status: 200, type: UserResDto }) |
| 60 | @Delete('del') |
| 61 | async del(@GetToken() token: TokenInfo) { |
| 62 | const userInfo = await this.userService.getUserInfoById(token.id); |
| 63 | if (!userInfo) throw new AppHttpException(ErrHttpBack.err_user_no_had); |
| 64 | |
| 65 | const res = await this.userService.deleteUser(userInfo); |
| 66 | return res; |
| 67 | } |
| 68 | |
| 69 | @ApiOperation({ |
| 70 | summary: '用户更新自己的电话号码', |
| 71 | description: '和手机验证码登录使用同一个验证码', |
| 72 | }) |
| 73 | @Put('updatePhone') |
| 74 | async updatePhone( |
| 75 | @GetToken() token: TokenInfo, |
| 76 | @Body() body: LoginByPhoneDto, |
| 77 | ) { |
| 78 | const { phone, code } = body; |
| 79 | |
| 80 | const res = await this.loginService.verifyPhoneCode( |
| 81 | phone, |
| 82 | code, |
| 83 | LoginTypeCacheKey.Code, |
| 84 | ); |
| 85 | if (!res) throw new AppHttpException(ErrHttpBack.err_user_code_nohad); |
| 86 | return await this.userService.updateUserPhone(token.id, phone); |
| 87 | } |
| 88 | } |
| 89 |