| 1 | import { Body, Controller, Delete, Get, Post, Put } from '@nestjs/common'; |
| 2 | import { ApiOperation, ApiTags } from '@nestjs/swagger'; |
| 3 | import { ManagerService } from './manager.service'; |
| 4 | import { |
| 5 | CreateManagerDto, |
| 6 | LoginDto, |
| 7 | UpdateManagerDto, |
| 8 | } from './dto/manager.dto'; |
| 9 | import { AuthService } from '../auth/auth.service'; |
| 10 | import { Manager } from '../auth/manager.guard'; |
| 11 | import { GetToken, Public } from '../auth/auth.guard'; |
| 12 | import { TokenInfo } from '../auth/interfaces/auth.interfaces'; |
| 13 | import { validatePassWord } from '../util/password.util'; |
| 14 | import { AppHttpException } from '../filters/http-exception.filter'; |
| 15 | import { ErrHttpBack } from '../filters/http-exception.back-code'; |
| 16 | |
| 17 | @ApiTags('manager - 管理员') |
| 18 | @Controller('manager') |
| 19 | export class ManagerController { |
| 20 | constructor( |
| 21 | private readonly managerService: ManagerService, |
| 22 | private readonly authService: AuthService, |
| 23 | ) {} |
| 24 | |
| 25 | @ApiOperation({ summary: '管理员登录' }) |
| 26 | @Public() |
| 27 | @Post('login') |
| 28 | async login(@Body() loginDto: LoginDto) { |
| 29 | const manager = await this.managerService.findByAccount(loginDto.account); |
| 30 | if (!manager) { |
| 31 | throw new AppHttpException(ErrHttpBack.err_user_no_had); |
| 32 | } |
| 33 | |
| 34 | const isValid = validatePassWord( |
| 35 | manager.password, |
| 36 | manager.salt, |
| 37 | loginDto.password, |
| 38 | ); |
| 39 | if (!isValid) { |
| 40 | throw new AppHttpException(ErrHttpBack.err_no_power_login); |
| 41 | } |
| 42 | |
| 43 | const token = await this.authService.generateToken({ |
| 44 | id: manager.id, |
| 45 | phone: manager.phone || '', |
| 46 | name: manager.name, |
| 47 | isManager: true, |
| 48 | }); |
| 49 | |
| 50 | return { |
| 51 | token, |
| 52 | managerInfo: manager, |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | @ApiOperation({ summary: '获取管理员信息' }) |
| 57 | @Manager() |
| 58 | @Get('info') |
| 59 | async getInfo(@GetToken() token: TokenInfo) { |
| 60 | return this.managerService.findById(token.id); |
| 61 | } |
| 62 | |
| 63 | @ApiOperation({ summary: '创建管理员' }) |
| 64 | @Manager() |
| 65 | @Post() |
| 66 | async create(@Body() createManagerDto: CreateManagerDto) { |
| 67 | const existManager = await this.managerService.findByAccount( |
| 68 | createManagerDto.account, |
| 69 | ); |
| 70 | if (existManager) { |
| 71 | throw new AppHttpException(ErrHttpBack.err_user_had); |
| 72 | } |
| 73 | return this.managerService.create(createManagerDto); |
| 74 | } |
| 75 | |
| 76 | @ApiOperation({ summary: '更新管理员信息' }) |
| 77 | @Manager() |
| 78 | @Put() |
| 79 | async update( |
| 80 | @GetToken() token: TokenInfo, |
| 81 | @Body() updateManagerDto: UpdateManagerDto, |
| 82 | ) { |
| 83 | return this.managerService.update(token.id, updateManagerDto); |
| 84 | } |
| 85 | |
| 86 | @ApiOperation({ summary: '删除管理员' }) |
| 87 | @Manager() |
| 88 | @Delete() |
| 89 | async delete(@GetToken() token: TokenInfo) { |
| 90 | return this.managerService.delete(token.id); |
| 91 | } |
| 92 | } |
| 93 |