user.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const crypto = require('crypto');
  7. const { ObjectId } = require('mongoose').Types;
  8. // 用户管理
  9. class UserService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'user');
  12. this.model = this.ctx.model.User;
  13. }
  14. /**
  15. * 登陆
  16. * @param {Object} body 登陆参数
  17. */
  18. async login({ phone, password }) {
  19. let user = await this.model.findOne({ phone }, '+password');
  20. if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  21. const { password: upwd, status } = user;
  22. if (status !== '1') throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态');
  23. if (password !== upwd.secret) throw new BusinessError(ErrorCode.BAD_PASSWORD);
  24. user = JSON.parse(JSON.stringify(user));
  25. delete user.meta;
  26. delete user.__v;
  27. delete user.password;
  28. // 用户信息toekn解密。小程序不适用
  29. // const token = this.ctx.service.util.jwt.encrypt(user);
  30. return user;
  31. }
  32. // 修改密码
  33. async updatePwd({ id }, { password }) {
  34. const data = await this.model.findById(id);
  35. if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  36. data.password = { secret: password };
  37. await data.save();
  38. }
  39. }
  40. module.exports = UserService;