companyuser.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. const jwt = require('jsonwebtoken');
  8. class CompanyuserService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'company_user');
  11. this.model = this.ctx.model.Companyuser;
  12. this.cmodel = this.ctx.model.Company;
  13. }
  14. // 重写创建方法
  15. async create(data) {
  16. const { phone, passwd, company_name, person, institution_name, roles } = data;
  17. assert(person && phone && passwd, '缺少部分信息项');
  18. assert(/^\d{11}$/i.test(phone), 'phone无效');
  19. const user = await this.model.findOne({ phone });
  20. if (user) {
  21. throw new BusinessError(ErrorCode.DATA_EXISTED);
  22. }
  23. const newdata = data;
  24. const pas = await this.createJwtPwd(passwd);
  25. newdata.passwd = pas;
  26. const res = await this.model.create(newdata);
  27. // 给用户发送消息告知注册成功
  28. this.ctx.service.viewnews.insertViewNews('注册成功', '恭喜您注册成功,欢迎使用吉林省小微企业金融综合服务平台', res._id);
  29. return res;
  30. }
  31. // 用户修改密码
  32. async uppasswd(data) {
  33. const { uid, oldpasswd, newpasswd } = data;
  34. assert(uid && oldpasswd && newpasswd, '缺少部分信息项');
  35. // 根据用户id查询其他用户表中是否存在相应数据
  36. const user = await this.model.findById(uid);
  37. const _user = await this.model.findOne({ phone: user.phone }, '+passwd');
  38. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  39. const _oldpasswd = await this.createJwtPwd(oldpasswd);
  40. // 如果两个密码不一致抛出异常
  41. if (_oldpasswd !== _user.passwd) {
  42. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  43. }
  44. const _newpasswd = await this.createJwtPwd(newpasswd);
  45. user.passwd = _newpasswd;
  46. await user.save();
  47. }
  48. // 用户修改密码
  49. async unpasswd(data) {
  50. const { uid, newpasswd } = data;
  51. assert(uid && newpasswd, '缺少部分信息项');
  52. // 根据用户id查询其他用户表中是否存在相应数据
  53. const user = await this.model.findById(uid);
  54. const _newpasswd = await this.createJwtPwd(newpasswd);
  55. user.passwd = _newpasswd;
  56. await user.save();
  57. }
  58. async login(data) {
  59. const { phone, passwd } = data;
  60. // 根据用户输入的手机号查询其他用户表中是否存在相应数据
  61. const user = await this.model.findOne({ phone });
  62. // 如果用户不存在抛出异常
  63. if (!user) {
  64. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  65. }
  66. const _user = await this.model.findOne({ phone }, '+passwd');
  67. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  68. const pas = await this.createJwtPwd(passwd);
  69. // 如果两个密码不一致抛出异常
  70. if (pas !== _user.passwd) {
  71. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  72. }
  73. return await this.createJwt(user);
  74. }
  75. // 直接返回用户信息的登录方法,不进行jwt
  76. async loginss(data) {
  77. const { phone, passwd } = data;
  78. // 根据用户输入的手机号查询其他用户表中是否存在相应数据
  79. const user = await this.model.findOne({ phone });
  80. // 如果用户不存在抛出异常
  81. if (!user) {
  82. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  83. }
  84. const _user = await this.model.findOne({ phone }, '+passwd');
  85. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  86. const pas = await this.createJwtPwd(passwd);
  87. // 如果两个密码不一致抛出异常
  88. if (pas !== _user.passwd) {
  89. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  90. }
  91. return user;
  92. }
  93. // 创建登录Token
  94. async createJwtPwd(password) {
  95. const { secret, expiresIn, issuer } = this.config.jwt;
  96. const token = await jwt.sign(password, secret);
  97. return token;
  98. }
  99. // 创建登录Token
  100. async createJwt({ id, phone, company_name, roles }) {
  101. const { secret, expiresIn = '1d', issuer = '3' } = this.config.jwt;
  102. const subject = phone;
  103. const _userid = id;
  104. // 根据uid查询企业基本信息
  105. const company = await this.cmodel.findOne({ uid: _userid });
  106. let res;
  107. if (company) {
  108. res = { userid: company.id, name: company_name, uid: _userid, roles };
  109. } else {
  110. res = { name: company_name, uid: _userid, roles };
  111. }
  112. // 将信息转成token返回给页面
  113. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  114. return token;
  115. }
  116. }
  117. module.exports = CompanyuserService;