adminUser.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. const crypto = require('crypto');
  3. const assert = require('assert');
  4. const Service = require('egg').Service;
  5. class AdminUserService extends Service {
  6. constructor(ctx) {
  7. super(ctx);
  8. this.model = this.ctx.model.AdminUser;
  9. this.UserBindRoleModel = this.ctx.model.UserBindRole;
  10. }
  11. async create({ userName, password, openid, name, phone }) {
  12. assert(userName, '缺少用户名');
  13. assert(password, '缺少密码');
  14. password = crypto.createHash('md5').update(password).digest('hex');
  15. const obj = await this.model.findOne({ userName });
  16. if (obj) return { errcode: -1001, errmsg: '用户名已存在', data: '' };
  17. try {
  18. const res = await this.model.create({ userName, password, openid, name, phone });
  19. return { errcode: 0, errmsg: 'ok', data: res };
  20. } catch (error) {
  21. throw error;
  22. }
  23. }
  24. async update({ id, userName, password, openid, name, phone }) {
  25. assert(id, '缺少用户ID');
  26. if (password) password = crypto.createHash('md5').update(password).digest('hex');
  27. try {
  28. const user = await this.model.findOne({ _id: id });
  29. if (!user) return { errcode: -1001, errmsg: '用户不存在', data: '' };
  30. await this.model.updateOne({ _id: id }, { userName, password, openid, name, phone });
  31. return { errcode: 0, errmsg: 'ok', data: 'update' };
  32. } catch (error) {
  33. throw error;
  34. }
  35. }
  36. async delete({ id }) {
  37. assert(id, '缺少用户ID');
  38. try {
  39. const user = await this.model.findOne({ _id: id });
  40. if (!user) return { errcode: -1001, errmsg: '用户不存在', data: '' };
  41. await this.model.remove({ _id: id });
  42. await this.UserBindRoleModel.find({ userId: id }).remove();
  43. return { errcode: 0, errmsg: 'ok', data: 'delete' };
  44. } catch (error) {
  45. throw error;
  46. }
  47. }
  48. async query({ skip, limit, phone, name, userName }) {
  49. const filter = {};
  50. if (userName || name || phone) filter.$or = [];
  51. if (userName) filter.$or.push({ code: { $regex: userName } });
  52. if (name) filter.$or.push({ name: { $regex: name } });
  53. if (phone) filter.$or.push({ phone: { $regex: phone } });
  54. try {
  55. const total = await this.model.find({ ...filter }, { password: false });
  56. let res;
  57. if (skip && limit) {
  58. res = await this.model.find({ ...filter }, { password: false }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  59. } else {
  60. res = await this.model.find({ ...filter }, { password: false });
  61. }
  62. return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
  63. } catch (error) {
  64. throw error;
  65. }
  66. }
  67. async updatePwd({ id, password, oldpassword }) {
  68. assert(id, '缺少用ID');
  69. assert(password, '缺少新密码');
  70. assert(oldpassword, '缺少原密码');
  71. oldpassword = crypto.createHash('md5').update(oldpassword).digest('hex');
  72. const user = await this.model.findOne({ _id: id });
  73. if (!user) return { errcode: -1001, errmsg: '用户不存在', data: '' };
  74. if (user.password !== oldpassword) return { errcode: -1001, errmsg: '原密码不正确', data: '' };
  75. password = crypto.createHash('md5').update(password).digest('hex');
  76. try {
  77. const res = await this.model.updateOne({ _id: id }, { password });
  78. return { errcode: 0, errmsg: 'ok', data: { ...res, password: '' } };
  79. } catch (error) {
  80. throw error;
  81. }
  82. }
  83. }
  84. module.exports = AdminUserService;