admin.js 4.2 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. let data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
  19. if (data.length > 0) data = JSON.parse(JSON.stringify(data));
  20. // const menus = await this.ctx.model.System.Menu.find();
  21. // data = data.map(i => {
  22. // const um = menus.filter(f => i.menus.find(mf => ObjectId(mf).equals(f._id)));
  23. // i.menus = um;
  24. // return i;
  25. // });
  26. return data;
  27. }
  28. // async fetch({ id }) {
  29. // let object = await this.model.findById(id);
  30. // if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  31. // object = JSON.parse(JSON.stringify(object));
  32. // // const menus = await this.ctx.model.System.Menu.find({ _id: object.menus.map(i => ObjectId(i)) }).sort({ sort: 1 });
  33. // // object.menus = menus;
  34. // return object;
  35. // }
  36. /**
  37. * 创建用户
  38. * @param {Object} params 用户信息
  39. */
  40. async create({ password, ...data }) {
  41. data.password = { secret: password };
  42. const { phone, pid, code } = data;
  43. // 检查手机号
  44. let num = await this.model.count({ phone });
  45. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有管理员/机构管理员/业务管理员 使用该手机号');
  46. // 检查上级id
  47. try {
  48. num = await this.model.count({ _id: ObjectId(pid) });
  49. if (num <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到上级的信息');
  50. } catch (error) {
  51. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到上级的信息');
  52. }
  53. const res = await this.model.create(data);
  54. // // 邀请码添加去掉
  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;