admin.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { ObjectId } = require('mongoose').Types;
  5. const _ = require('lodash');
  6. const jwt = require('jsonwebtoken');
  7. // 管理员
  8. class AdminService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'admin');
  11. this.model = this.ctx.model.Admin;
  12. this.util = this.ctx.service.util.util;
  13. this.inviteCode = this.ctx.model.InviteCode;
  14. }
  15. async query(query, { skip = 0, limit = 0, ...other } = {}) {
  16. query = this.util.turnDateRangeQuery(this.util.turnFilter(query));
  17. let data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
  18. if (data.length > 0) data = JSON.parse(JSON.stringify(data));
  19. const menus = await this.ctx.model.Menu.find();
  20. data = data.map(i => {
  21. const um = menus.filter(f => i.menus.find(mf => ObjectId(mf).equals(f._id)));
  22. i.menus = um;
  23. return i;
  24. });
  25. return data;
  26. }
  27. async fetch({ id }) {
  28. let object = await this.model.findById(id);
  29. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  30. object = JSON.parse(JSON.stringify(object));
  31. const menus = await this.ctx.model.Menu.find({ _id: object.menus.map(i => ObjectId(i)) }).sort({ sort: 1 });
  32. object.menus = menus;
  33. return object;
  34. }
  35. /**
  36. * 创建用户
  37. * @param {Object} params 用户信息
  38. */
  39. async create({ passwd, ...data }) {
  40. data.passwd = { secret: passwd };
  41. const { phone, pid, code } = data;
  42. // 检查手机号
  43. let num = await this.model.count({ phone });
  44. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有管理员/机构管理员/业务管理员 使用该手机号');
  45. // 检查上级id
  46. try {
  47. num = await this.model.count({ _id: ObjectId(pid) });
  48. if (num <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到上级的信息');
  49. } catch (error) {
  50. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到上级的信息');
  51. }
  52. const res = await this.model.create(data);
  53. const icdata = { user_id: res._id, code, user_name: res.name };
  54. try {
  55. const res = await this.inviteCode.create(icdata);
  56. } catch (error) {
  57. await this.model.deleteOne({ _id: res._id });
  58. throw new BusinessError(ErrorCode.DATABASE_FAULT, '该邀请码已被使用,请重新更换邀请码');
  59. }
  60. return res;
  61. }
  62. /**
  63. * 修改密码
  64. * @param {Object} {id,passwd} 用户id和密码
  65. */
  66. async password({ id, passwd }) {
  67. const object = await this.model.findById(id);
  68. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  69. object.passwd = { secret: passwd };
  70. await object.save();
  71. }
  72. /**
  73. * 管理员登陆
  74. * @param {Object} params 登陆信息
  75. * @property code_phone code或者是phone
  76. * @property passwd 密码
  77. */
  78. async login({ code_phone, passwd }) {
  79. const object = await this.model.findOne({ $or: [{ code: code_phone }, { phone: code_phone }] }, '+passwd');
  80. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  81. const { passwd: op } = object;
  82. const { secret } = op;
  83. if (secret !== passwd) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  84. const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'passwd', '__v' ]);
  85. const { secret: secrets } = this.config.jwt;
  86. const menus = await this.ctx.model.Menu.find({ _id: object.menus.map(i => ObjectId(i)) }).sort({ sort: 1 });
  87. data.menus = menus;
  88. const token = jwt.sign(data, secrets);
  89. return token;
  90. }
  91. }
  92. module.exports = AdminService;