login.controller.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { Body, Config, Controller, Inject, Param, Post, Get } from '@midwayjs/core';
  2. import { ApiTags } from '@midwayjs/swagger';
  3. import { LoginDTO, LoginType, LoginVO, UPwdDTO } from '../interface/login.interface';
  4. import { LoginService } from '../service/login.service';
  5. import { JwtService } from '@midwayjs/jwt';
  6. import { Context } from '@midwayjs/koa';
  7. const assert = require('assert');
  8. import get = require('lodash/get');
  9. import { UtilService } from '../service/util.service';
  10. @ApiTags(['登录服务'])
  11. @Controller('/login')
  12. export class LoginController {
  13. @Inject()
  14. loginService: LoginService;
  15. @Inject()
  16. jwtService: JwtService;
  17. @Inject()
  18. utilService: UtilService;
  19. @Config('jwt.secret')
  20. jwtSecret;
  21. @Config('jwt.expiresIn')
  22. jwtExpiresIn;
  23. @Inject()
  24. ctx: Context;
  25. /**
  26. * 账密登录
  27. * @param data 用户名和密码
  28. * @param type 用户类型
  29. */
  30. @Post('/:type', { routerName: '登录' })
  31. async toLogin(@Body() data: LoginDTO, @Param('type') type: string) {
  32. const user = await this.loginService.loginByAccount(data, LoginType[type]);
  33. // 超级管理员不需要设置角色,部门,直接给
  34. if (type === 'Admin' && user.is_super === '0') user.role = [type];
  35. let vo = new LoginVO(user);
  36. vo = JSON.parse(JSON.stringify(vo));
  37. vo.login_code = await this.loginService.onePointLogin(vo);
  38. const token = await this.jwtService.sign(vo, this.jwtSecret);
  39. return token;
  40. }
  41. /**
  42. * TODO:微信小程序登录
  43. * @param openid 微信小程序openid
  44. */
  45. // @Post('/wxapp/:openid')
  46. // async wxAppLogin(@Param('openid') openid: string) {
  47. // if (!openid) throw new ServiceError('缺少微信凭证!', FrameworkErrorEnum.BAD_PARAMS);
  48. // const user = await this.loginService.wxAppLogin(openid);
  49. // let vo = new LoginVO(user);
  50. // vo = JSON.parse(JSON.stringify(vo));
  51. // const token = await this.jwtService.sign(vo, this.jwtSecret, {
  52. // expiresIn: this.jwtExpiresIn,
  53. // });
  54. // return token;
  55. // }
  56. /**
  57. * 修改密码
  58. * @param data 修改密码所需数据
  59. * @param type 账户类型
  60. */
  61. @Post('/updatePwd/:type', { routerName: '修改密码' })
  62. async updatePwd(@Body() data: UPwdDTO, @Param('type') type: string) {
  63. await this.loginService.updatePwd(data, LoginType[type]);
  64. return 'ok';
  65. }
  66. @Post('/resetPwd/:type', { routerName: '重置密码' })
  67. async resetPwd(@Body('id') id: number, @Param('type') type: string) {
  68. // 随机密码
  69. const data = new UPwdDTO();
  70. data.id = id;
  71. data.password = this.utilService.randomStr();
  72. await this.loginService.updatePwd(data, LoginType[type]);
  73. return data.password;
  74. }
  75. @Get('/analysis')
  76. async analysisToken() {
  77. const token = get(this.ctx, 'request.header.token');
  78. assert(token, '缺少token信息');
  79. const result = await this.jwtService.decodeSync(token);
  80. return result;
  81. }
  82. }