| 1 | import { Body, Controller, Get, Patch, Put } from '@nestjs/common' |
| 2 | import { ApiTags } from '@nestjs/swagger' |
| 3 | import { GetToken, TokenInfo } from '@yikart/aitoearn-auth' |
| 4 | import { ApiDoc, AppException, ResponseCode } from '@yikart/common' |
| 5 | import { |
| 6 | ReportLocationDto, |
| 7 | ReportLocationDtoSchema, |
| 8 | SetAiConfigDto, |
| 9 | SetAiConfigItemDto, |
| 10 | SwitchUserTypeDto, |
| 11 | SwitchUserTypeDtoSchema, |
| 12 | UpdateLocaleDto, |
| 13 | UpdateLocaleDtoSchema, |
| 14 | UpdateUserInfoDto, |
| 15 | } from './user.dto' |
| 16 | import { UserService } from './user.service' |
| 17 | import { UserInfoVO } from './user.vo' |
| 18 | |
| 19 | @ApiTags('User/User') |
| 20 | @Controller('user') |
| 21 | export class UserController { |
| 22 | constructor( |
| 23 | private readonly userService: UserService, |
| 24 | ) { } |
| 25 | |
| 26 | @ApiDoc({ |
| 27 | summary: 'Get Current User Information', |
| 28 | description: 'Retrieve the profile of the authenticated user.', |
| 29 | response: UserInfoVO, |
| 30 | }) |
| 31 | @Get('mine') |
| 32 | getUserInfoById(@GetToken() token: TokenInfo) { |
| 33 | return this.userService.getUserInfoById(token.id) |
| 34 | } |
| 35 | |
| 36 | @ApiDoc({ |
| 37 | summary: 'Update User Information', |
| 38 | description: 'Update the profile of the authenticated user.', |
| 39 | body: UpdateUserInfoDto.schema, |
| 40 | }) |
| 41 | @Put('info/update') |
| 42 | async updateInfo( |
| 43 | @GetToken() token: TokenInfo, |
| 44 | @Body() body: UpdateUserInfoDto, |
| 45 | ) { |
| 46 | const userInfo = await this.userService.getUserInfoById(token.id) |
| 47 | if (!userInfo) |
| 48 | throw new AppException(ResponseCode.UserNotFound, 'User not found') |
| 49 | |
| 50 | const res = await this.userService.updateUserInfo(token.id, body) |
| 51 | return res |
| 52 | } |
| 53 | |
| 54 | @ApiDoc({ |
| 55 | summary: 'Set AI Configuration', |
| 56 | body: SetAiConfigDto.schema, |
| 57 | }) |
| 58 | @Put('ai/config/info') |
| 59 | async setAiConfig( |
| 60 | @GetToken() token: TokenInfo, |
| 61 | @Body() body: SetAiConfigDto, |
| 62 | ) { |
| 63 | return this.userService.setAiConfig(token.id, body) |
| 64 | } |
| 65 | |
| 66 | @ApiDoc({ |
| 67 | summary: 'Set AI Configuration Item', |
| 68 | body: SetAiConfigItemDto.schema, |
| 69 | }) |
| 70 | @Put('ai/config/item') |
| 71 | async setAiConfigItem( |
| 72 | @GetToken() token: TokenInfo, |
| 73 | @Body() body: SetAiConfigItemDto, |
| 74 | ) { |
| 75 | return this.userService.setAiConfigItem(token.id, body.type, body.value) |
| 76 | } |
| 77 | |
| 78 | @ApiDoc({ |
| 79 | summary: '上报地理位置', |
| 80 | description: '上报当前用户的地理位置信息', |
| 81 | body: ReportLocationDtoSchema, |
| 82 | }) |
| 83 | @Put('/location') |
| 84 | async reportLocation( |
| 85 | @GetToken() token: TokenInfo, |
| 86 | @Body() body: ReportLocationDto, |
| 87 | ) { |
| 88 | await this.userService.updateLocation(token.id, body) |
| 89 | } |
| 90 | |
| 91 | @ApiDoc({ |
| 92 | summary: '设置语言偏好', |
| 93 | description: '设置当前用户的语言偏好(en-US 或 zh-CN)', |
| 94 | body: UpdateLocaleDtoSchema, |
| 95 | }) |
| 96 | @Patch('/locale') |
| 97 | async updateLocale( |
| 98 | @GetToken() token: TokenInfo, |
| 99 | @Body() body: UpdateLocaleDto, |
| 100 | ) { |
| 101 | await this.userService.updateLocale(token.id, body.locale) |
| 102 | } |
| 103 | |
| 104 | @ApiDoc({ |
| 105 | summary: '切换用户身份', |
| 106 | description: '在创作者和商家身份之间切换', |
| 107 | body: SwitchUserTypeDtoSchema, |
| 108 | }) |
| 109 | @Put('type/switch') |
| 110 | async switchUserType( |
| 111 | @GetToken() token: TokenInfo, |
| 112 | @Body() body: SwitchUserTypeDto, |
| 113 | ) { |
| 114 | const userInfo = await this.userService.getUserInfoById(token.id) |
| 115 | if (!userInfo) { |
| 116 | throw new AppException(ResponseCode.UserNotFound) |
| 117 | } |
| 118 | |
| 119 | if (userInfo.userType === body.userType) { |
| 120 | return { userType: body.userType } |
| 121 | } |
| 122 | |
| 123 | const success = await this.userService.switchUserType(token.id, body.userType) |
| 124 | if (!success) { |
| 125 | throw new AppException(ResponseCode.UserNotFound) |
| 126 | } |
| 127 | |
| 128 | return { userType: body.userType } |
| 129 | } |
| 130 | } |
| 131 |