adminUser.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const assert = require('assert');
  4. const moment = require('moment');
  5. const crypto = require('crypto');
  6. class adminUserService extends Service {
  7. async create({ acct, password, userName, phone, state, roleList }) {
  8. assert(acct, '帐号不存在');
  9. assert(password, '密码不存在');
  10. assert(userName, '用户名不存在');
  11. assert(state, '状态不存在');
  12. const { AdminUser: model } = this.ctx.model;
  13. const user = await model.find({ acct });
  14. if (user.length > 0) return { errmsg: '帐号已存在', errcode: -2001 };
  15. const createAt = moment().format('x');
  16. const hash = crypto.createHmac('sha256', this.app.config.userSecret);
  17. const pwa = hash.update(password).digest('hex');
  18. try {
  19. const res = await model.create({ acct, password: pwa, userName, phone, createAt, state, roleList });
  20. return { errmsg: '', errcode: 0, res };
  21. } catch (error) {
  22. console.log(error);
  23. throw new Error({ errcode: -2001, errmsg: '添加失败' });
  24. }
  25. }
  26. async update({ userName, phone, _id, roleList, state }) {
  27. assert(_id, 'id不存在');
  28. const { AdminUser: model } = this.ctx.model;
  29. try {
  30. await model.findById(_id).update({ userName, phone, roleList, state });
  31. return { errmsg: '', errcode: 0 };
  32. } catch (error) {
  33. console.log(error);
  34. throw new Error({ errcode: -2001, errmsg: '修改失败' });
  35. }
  36. }
  37. async pwdUpdate({ password, _id, confirmPwd }) {
  38. assert(_id, 'id不存在');
  39. const { AdminUser: model } = this.ctx.model;
  40. const hash = crypto.createHmac('sha256', this.app.config.userSecret);
  41. const cpwd = hash.update(confirmPwd).digest('hex');
  42. try {
  43. const res = await model.findById(_id);
  44. if (res.password !== cpwd) {
  45. return { errmsg: '原密码错误', errcode: -2003 };
  46. }
  47. const hash = crypto.createHmac('sha256', this.app.config.userSecret);
  48. const pwd = hash.update(password).digest('hex');
  49. await model.findByIdAndUpdate(_id, { password: pwd });
  50. return { errmsg: '', errcode: 0 };
  51. } catch (error) {
  52. throw new Error({ errcode: -2001, errmsg: '修改失败' });
  53. }
  54. }
  55. async del({ id }) {
  56. assert(id, 'id不存在');
  57. const { AdminUser: model } = this.ctx.model;
  58. try {
  59. await model.findById(id).remove();
  60. return { errmsg: '', errcode: 0 };
  61. } catch (error) {
  62. throw new Error({ errcode: -2001, errmsg: '删除失败' });
  63. }
  64. }
  65. async query({ skip, limit, userName, state, acct }) {
  66. const { AdminUser: model } = this.ctx.model;
  67. const filter = {};
  68. if (userName) filter.userName = userName;
  69. if (state) filter.state = state;
  70. if (acct) filter.acct = acct;
  71. try {
  72. let res;
  73. const total = await model.find({ ...filter });
  74. if (skip && limit) {
  75. res = await model.find({ ...filter }, { password: false }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  76. } else {
  77. res = await model.find({ ...filter }, { password: false });
  78. }
  79. return { errmsg: '', errcode: 0, data: res, total: total.length };
  80. } catch (error) {
  81. throw new Error({ errcode: -2001, errmsg: '查询失败' });
  82. }
  83. }
  84. }
  85. module.exports = adminUserService;