adminUser.js 2.8 KB

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