user.js 3.9 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 _ = require('lodash');
  5. const assert = require('assert');
  6. //
  7. class UserService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'user');
  10. this.redis = this.app.redis;
  11. this.model = this.ctx.model.User.User;
  12. this.emailKey = 'bindEmail:';
  13. this.httpUtil = this.ctx.service.util.httpUtil;
  14. this.emailServiceUrl = _.get(this.app, 'config.httpPrefix.email');
  15. this.conenctCode = '&&';
  16. }
  17. async beforeCreate(data) {
  18. const openid = _.get(data, 'openid');
  19. const phone = _.get(data, 'phone');
  20. if (!openid && phone) {
  21. const num = await this.model.count({ phone });
  22. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该手机号已注册');
  23. } else if (openid) {
  24. const num = await this.model.count({ openid });
  25. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该微信号已注册');
  26. }
  27. return data;
  28. }
  29. async resetPwd({ id }, { password }) {
  30. const data = await this.model.findById(id);
  31. if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  32. data.password = { secret: password };
  33. await data.save();
  34. }
  35. /**
  36. * 登陆
  37. * @param {Object} body 登陆参数
  38. * @param body.phone 账户
  39. * @param body.password 密码
  40. */
  41. async login({ phone, password }) {
  42. const { populate } = this.getRefMods();
  43. let user = await this.model.findOne({ phone }, '+password').populate(populate);
  44. if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  45. const { password: upwd, status } = user;
  46. if (status !== '0') throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态');
  47. if (password !== upwd.secret) throw new BusinessError(ErrorCode.BAD_PASSWORD);
  48. // // 使用redis存储,后续的任何操作进行token的校验
  49. // await this.setUserInRedis(user);
  50. user = JSON.parse(JSON.stringify(user));
  51. delete user.password;
  52. delete user.meta;
  53. delete user.__v;
  54. const token = this.ctx.service.util.jwt.encrypt(user);
  55. return token;
  56. }
  57. /**
  58. * 微信登录
  59. * @param {Object} body 登陆参数
  60. * @param body.openid 微信小程序的openid
  61. */
  62. async wxLogin({ openid }) {
  63. const { populate } = this.getRefMods();
  64. const user = await this.model.findOne({ openid }).populate(populate);
  65. if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  66. const { status } = user;
  67. if (status !== '0') throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态');
  68. delete user.meta;
  69. delete user.__v;
  70. const token = this.ctx.service.util.jwt.encrypt(user);
  71. return token;
  72. }
  73. /**
  74. * 绑定邮箱验证码
  75. * @param {Object} body 请求体
  76. * @param body.id 用户id
  77. * @param body.email 用户要绑定的邮箱
  78. */
  79. async toBindEmail({ id, email }) {
  80. const code = _.random(100000, 999999);
  81. const value = `${email}${this.conenctCode}${code}`;
  82. await this.redis.set(`${this.emailKey}${id}`, value, 'EX', 300);
  83. // 发邮件
  84. const data = { template: 'bindEmail', receiver: email, params: { code } };
  85. const url = `${this.emailServiceUrl}/sendEmail`;
  86. const res = await this.httpUtil.cpost(url, data);
  87. console.log(res);
  88. return res;
  89. }
  90. async checkBindEmail({ code, id, email }) {
  91. const redisData = await this.redis.get(`${this.emailKey}${id}`);
  92. if (!redisData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '验证码已超时');
  93. const arr = redisData.split(this.conenctCode);
  94. const rEmail = _.head(arr);
  95. const rCode = _.last(arr);
  96. if (code !== rCode) throw new BusinessError(ErrorCode.DATA_INVALID, '验证码错误');
  97. if (email !== rEmail) throw new BusinessError(ErrorCode.DATA_INVALID, '要绑定的邮箱与接收验证码的邮箱不是同一个邮箱');
  98. await this.model.updateOne({ _id: id }, { email });
  99. }
  100. }
  101. module.exports = UserService;