admin.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const Transaction = require('mongoose-transactions');
  7. //
  8. class AdminService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'admin');
  11. this.model = this.ctx.model.User.Admin;
  12. this.tran = new Transaction();
  13. }
  14. async beforeCreate(data) {
  15. const { account } = data;
  16. const num = await this.model.count({ account });
  17. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该账号名已存在');
  18. return data;
  19. }
  20. async beforeUpdate(filter, update) {
  21. const { account } = update;
  22. const id = _.get(filter, '_id', _.get(filter, 'id'));
  23. const num = await this.model.count({ _id: { $ne: id }, account });
  24. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该账号名已存在');
  25. return { filter, update };
  26. }
  27. /**
  28. * 登陆
  29. * @param {Object} body 登陆参数
  30. * @param body.account 账户
  31. * @param body.password 密码
  32. */
  33. async login({ account, password }) {
  34. const { populate } = this.getRefMods();
  35. let user = await this.model.findOne({ account }, '+password').populate(populate);
  36. if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  37. const { password: upwd } = user;
  38. if (password !== upwd.secret) throw new BusinessError(ErrorCode.BAD_PASSWORD);
  39. // // 使用redis存储,后续的任何操作进行token的校验
  40. // await this.setUserInRedis(user);
  41. user = JSON.parse(JSON.stringify(user));
  42. delete user.password;
  43. delete user.meta;
  44. delete user.__v;
  45. const token = this.ctx.service.util.jwt.encrypt(user);
  46. return token;
  47. }
  48. async resetPwd({ id }, { password }) {
  49. const data = await this.model.findById(id);
  50. if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  51. data.password = { secret: password };
  52. await data.save();
  53. }
  54. /**
  55. * 邮箱重置账号密码
  56. * @param {Object} body 参数体
  57. * @param body.account 要重置的账号
  58. */
  59. async emailResetPwd({ account }) {
  60. const admin = await this.model.findOne({ account });
  61. if (!admin) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户');
  62. const { email } = admin;
  63. if (!email) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到账户的邮箱');
  64. // 重置密码+发邮件
  65. const secret = this.ctx.service.util.trade.createNonceStr();
  66. try {
  67. this.tran.update('Admin', admin._id, { password: { secret } });
  68. await this.ctx.service.util.email.resetPwd(email, secret);
  69. await this.tran.run();
  70. } catch (error) {
  71. this.tran.rollback();
  72. throw new Error(error);
  73. } finally {
  74. this.tran.clean();
  75. }
  76. }
  77. /**
  78. * 检验当前管理员密码,生成操作key
  79. * @param {Object} body 参数体
  80. * @param {String} body.password 管理员密码
  81. * @param {String} body.target 目标key
  82. */
  83. async toMakeKey({ password, target }) {
  84. const admin = _.get(this.ctx, 'admin');
  85. const _id = _.get(admin, '_id');
  86. const taData = await this.model.findById(_id, '+password');
  87. if (!taData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到当前操作管理人');
  88. const { password: upwd } = taData;
  89. if (password !== upwd.secret) throw new BusinessError(ErrorCode.BAD_PASSWORD);
  90. const key = this.ctx.service.util.user.getDeleteUserKey(_id, target);
  91. return key;
  92. }
  93. }
  94. module.exports = AdminService;