user.controller.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayjs/core';
  2. import { BaseController } from '../../frame/BaseController';
  3. import { ApiQuery, ApiResponse, ApiTags } from '@midwayjs/swagger';
  4. import { Validate } from '@midwayjs/validate';
  5. import { omit, pick } from 'lodash';
  6. import { ErrorCode, ServiceError } from '../../error/service.error';
  7. import { UserService } from '../../service/system/user.service';
  8. import { CVO_user, FVO_user, QVO_user, UVAO_user } from '../../interface/system/user.interface';
  9. import { RoleService } from '../../service/system/role.service';
  10. const namePrefix = '平台用户';
  11. @ApiTags(['平台用户'])
  12. @Controller('/user', { tagName: namePrefix })
  13. export class UserController implements BaseController {
  14. controllerCode = 'user_user';
  15. @Inject()
  16. service: UserService;
  17. @Inject()
  18. roleService: RoleService;
  19. @Get('/')
  20. @ApiTags('列表查询')
  21. @ApiQuery({ name: 'query' })
  22. @ApiResponse({ type: QVO_user })
  23. async index(@Query() query: object) {
  24. const qobj = omit(query, ['skip', 'limit']);
  25. const others = pick(query, ['skip', 'limit']);
  26. const result = await this.service.query(qobj, others);
  27. return result;
  28. }
  29. @Get('/list')
  30. async list(@Query() query: object) {
  31. const qobj = omit(query, ['skip', 'limit']);
  32. const others = pick(query, ['skip', 'limit']);
  33. const { data, total } = await this.service.query(qobj, others);
  34. for (let i of data) {
  35. i = await this.roleService.Isaudit(i);
  36. }
  37. return { data, total };
  38. }
  39. @Get('/:id')
  40. @ApiTags('单查询')
  41. @ApiResponse({ type: FVO_user })
  42. async fetch(@Param('id') id: number) {
  43. const data = await this.service.fetch({ id });
  44. const result = new FVO_user(data);
  45. return result;
  46. }
  47. @Get('/detail/:id')
  48. @ApiResponse({ type: FVO_user })
  49. async detail(@Param('id') id: string) {
  50. const data = await this.service.detail(id);
  51. const result = new FVO_user(data);
  52. return result;
  53. }
  54. @Post('/', { routerName: `创建${namePrefix}` })
  55. @ApiTags('创建数据')
  56. @Validate()
  57. @ApiResponse({ type: CVO_user })
  58. async create(@Body() data: object) {
  59. await this.service.createExamine(data);
  60. await this.service.checkPhone(data);
  61. await this.service.checkEmail(data);
  62. const dbData = await this.service.create(data);
  63. const result = new CVO_user(dbData);
  64. return result;
  65. }
  66. @Post('/:id', { routerName: `修改${namePrefix}` })
  67. @ApiTags('修改数据')
  68. @Validate()
  69. @ApiResponse({ type: UVAO_user })
  70. async update(@Param('id') id: number, @Body() data: object) {
  71. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  72. await this.service.checkPhone(data)
  73. await this.service.checkEmail(data)
  74. const result = await this.service.update({ id }, data);
  75. return result;
  76. }
  77. @Del('/:id', { routerName: `删除${namePrefix}` })
  78. @ApiTags('删除数据')
  79. @Validate()
  80. async delete(@Param('id') id: number) {
  81. if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
  82. const result = await this.service.delete({ id });
  83. return result;
  84. }
  85. }