admin.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. async delete({ id }) {
  63. const data = await this.model.findOne({ _id: ObjectId(id) });
  64. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据');
  65. const { code } = data;
  66. const res = await this.model.deleteOne({ _id: ObjectId(id) });
  67. await this.inviteCode.deleteOne({ code });
  68. return res;
  69. }
  70. /**
  71. * 修改密码
  72. * @param {Object} {id,passwd} 用户id和密码
  73. */
  74. async password({ id, passwd }) {
  75. const object = await this.model.findById(id);
  76. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  77. object.passwd = { secret: passwd };
  78. await object.save();
  79. }
  80. /**
  81. * 管理员登陆
  82. * @param {Object} params 登陆信息
  83. * @property code_phone code或者是phone
  84. * @property passwd 密码
  85. */
  86. async login({ code_phone, passwd }) {
  87. const object = await this.model.findOne({ $or: [{ code: code_phone }, { phone: code_phone }] }, '+passwd');
  88. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  89. const { passwd: op } = object;
  90. const { secret } = op;
  91. if (secret !== passwd) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  92. const data = _.omit(JSON.parse(JSON.stringify(object)), ['meta', 'passwd', '__v']);
  93. const { secret: secrets } = this.config.jwt;
  94. const menus = await this.ctx.model.Menu.find({ _id: object.menus.map((i) => ObjectId(i)) }).sort({ sort: 1 });
  95. data.menus = menus;
  96. const token = jwt.sign(data, secrets);
  97. return token;
  98. }
  99. }
  100. module.exports = AdminService;