Login.controller.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { Controller, Post, Body, Param, Inject, Config } from '@midwayjs/core';
  2. import { LoginService } from '../../service/frame/Login.service';
  3. import { LoginType, LoginVO, UPwdDTO } from '../../frame/Options';
  4. import { JwtService } from '@midwayjs/jwt';
  5. import { LoginRecordService } from '../../service/frame/LoginRecord.service';
  6. import { RF } from '../../response/CustomerResponse';
  7. @Controller('/login')
  8. export class LoginController {
  9. @Inject()
  10. loginService: LoginService;
  11. @Inject()
  12. loginRecordService: LoginRecordService;
  13. @Config('jwt.secret')
  14. jwtSecret;
  15. @Inject()
  16. jwtService: JwtService;
  17. /**
  18. * 账密登录
  19. * @param data 用户名和密码
  20. * @param type 用户类型
  21. */
  22. @Post('/:type')
  23. async toLogin(@Body() data, @Param('type') type: string) {
  24. const user = await this.loginService.loginByAccount(data, LoginType[type]);
  25. if (user) user.role = type;
  26. let vo = new LoginVO(user);
  27. vo = JSON.parse(JSON.stringify(vo));
  28. // user数据写成
  29. const token = await this.jwtService.sign(vo, this.jwtSecret);
  30. // 创建/更新登录信息
  31. await this.loginRecordService.create(token);
  32. return RF.success(token);
  33. }
  34. /**
  35. * 修改密码
  36. * @param data 修改密码所需数据
  37. * @param type 账户类型
  38. */
  39. @Post('/updatePwd/:type')
  40. async updatePwd(@Body() data: UPwdDTO, @Param('type') type: string) {
  41. await this.loginService.updatePwd(data, LoginType[type]);
  42. return RF.success()
  43. }
  44. /**
  45. * 重置密码
  46. * @param _id 用户id
  47. * @param type 用户类型
  48. */
  49. @Post('/resetPwd/:type')
  50. async resetPwd(@Body('_id') _id: string, @Param('type') type: string) {
  51. // 随机密码,不需要写密码字段,函数内会给
  52. const data = new UPwdDTO();
  53. data._id = _id;
  54. const password = await this.loginService.updatePwd(data, LoginType[type]);
  55. return RF.success(password);
  56. }
  57. }