1
0

login.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import BaseController from '../../base';
  2. import { AdminRoute } from '../../../decorator/router_register';
  3. import { LoginInfoDto, UpdatePersonInfoDto } from '../../../dto/admin/verify';
  4. /**
  5. * 通用功能控制器
  6. */
  7. export default class CommController extends BaseController {
  8. /**
  9. * @api {get} /admin/captcha/img 获取图片验证码
  10. * @apiGroup 登陆验证类
  11. * @apiParam {Number} [width=100] 图片宽度
  12. * @apiParam {Number} [height=50] 图片高度
  13. * @apiUse BaseRes
  14. * @apiUse Auth
  15. * @apiSuccess {String} data.img base64格式的验证码图片字符串
  16. * @apiSuccess {String} data.id 验证码对应ID
  17. */
  18. @AdminRoute('/captcha/img', 'get')
  19. async captchaByImg() {
  20. const result = await this.service.admin.comm.verify.getImgCaptcha(this.getQuery());
  21. this.res({
  22. data: result,
  23. });
  24. }
  25. /**
  26. * @api {post} /admin/login 管理员登陆
  27. * @apiGroup 登陆验证类
  28. * @apiParam {String} username 用户名,AES加密
  29. * @apiParam {String} password 密码,AES加密
  30. * @apiParam {String} captchaId 验证码ID
  31. * @apiParam {String} verifyCode 填写的验证码
  32. * @apiUse BaseRes
  33. * @apiSuccess {String} data.token 用户Token
  34. */
  35. @AdminRoute('/login', 'post')
  36. async login() {
  37. const dto = await this.ctx.validate<LoginInfoDto>(LoginInfoDto);
  38. // const { username, password, captchaId, verifyCode } = info;
  39. const success = await this.service.admin.comm.verify.checkImgCaptcha(dto.captchaId, dto.verifyCode);
  40. if (!success) {
  41. this.res({
  42. code: 10002,
  43. });
  44. return;
  45. }
  46. const sign = await this.service.admin.comm.verify.getLoginSign(dto.username, dto.password);
  47. if (!sign) {
  48. this.res({
  49. code: 10003,
  50. });
  51. return;
  52. }
  53. this.res({
  54. data: {
  55. token: sign,
  56. },
  57. });
  58. }
  59. /**
  60. * @api {post} /admin/logout 管理员登出
  61. * @apiGroup 登陆验证类
  62. * @apiUse Auth
  63. * @apiUse BaseRes
  64. */
  65. @AdminRoute('/logout', 'post')
  66. async logout() {
  67. await this.service.admin.comm.verify.clearLoginStatus(this.ctx.token.uid);
  68. this.res();
  69. }
  70. /**
  71. * @api {get} /admin/permmenu 获取权限及菜单
  72. * @apiGroup 登陆验证类
  73. * @apiUse BaseRes
  74. * @apiUse Auth
  75. * @apiSuccess {Array} data.menus 菜单
  76. * @apiSuccess {Array} data.perms 权限描述
  77. */
  78. @AdminRoute('/permmenu', 'get')
  79. async permmenu() {
  80. this.res({
  81. data: await this.service.admin.comm.verify.getPermMenu(this.ctx.token.uid),
  82. });
  83. }
  84. /**
  85. * @api {get} /admin/person 获取当前登录用户信息
  86. * @apiGroup 登陆验证类
  87. * @apiUse Auth
  88. * @apiUse BaseRes
  89. * @apiSuccess {Object} data 管理员信息实体类
  90. */
  91. @AdminRoute('/person', 'get')
  92. async person() {
  93. this.res({
  94. data: await this.service.admin.sys.user.person(this.ctx.token.uid),
  95. });
  96. }
  97. /**
  98. * @api {post} /admin/person 更新管理员信息
  99. * @apiGroup 登陆验证类
  100. * @apiUse Auth
  101. * @apiUse BaseRes
  102. * @apiParam {String} name 名称
  103. * @apiParam {String} nickName 别名
  104. * @apiParam {String} email 邮箱
  105. * @apiParam {String} phone 手机
  106. * @apiParam {String} originPassword 更改前密码
  107. * @apiParam {String} newPassword 新密码
  108. * @apiParam {String} remark 备注
  109. * @apiParam {String} headImg 头像
  110. */
  111. @AdminRoute('/person', 'post')
  112. async personUpdate() {
  113. const dto = await this.ctx.validate<UpdatePersonInfoDto>(UpdatePersonInfoDto);
  114. const result = await this.service.admin.sys.user.personUpdate(this.ctx.token.uid, dto);
  115. if (!result) {
  116. this.res({
  117. code: 10011,
  118. });
  119. } else {
  120. this.res({
  121. data: await this.service.admin.sys.user.person(this.ctx.token.uid),
  122. });
  123. }
  124. }
  125. }