admin.js 4.1 KB

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