otheruser.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 OtheruserService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'other_user');
  11. this.model = this.ctx.model.Otheruser;
  12. this.imodel = this.ctx.model.Institution;
  13. this.cumodel = this.ctx.model.Companyuser;
  14. }
  15. // 重写创建方法
  16. async create(data) {
  17. const { name, phone, passwd, type, characterid } = data;
  18. console.log(data);
  19. assert(name && phone && passwd && type, '缺少部分信息项');
  20. assert(/^\d{11}$/i.test(phone), 'phone无效');
  21. const user = await this.model.findOne({ phone });
  22. if (user) {
  23. throw new BusinessError(ErrorCode.DATA_EXISTED);
  24. }
  25. const newdata = data;
  26. const pas = await this.createJwtPwd(passwd);
  27. newdata.passwd = pas;
  28. const res = await this.model.create(newdata);
  29. if (type === '1') {
  30. await this.imodel.create({ uid: res.id, name });
  31. }
  32. return res;
  33. }
  34. // 重写修改方法
  35. async update({ id }, data) {
  36. const { name, phone, passwd, type, characterid } = data;
  37. const user = await this.model.findById(id, '+passwd');
  38. if (name) {
  39. user.name = name;
  40. }
  41. if (phone) {
  42. user.phone = phone;
  43. }
  44. if (passwd) {
  45. const newpasswd = await this.createJwtPwd(passwd);
  46. user.passwd = newpasswd;
  47. }
  48. if (type) {
  49. user.type = type;
  50. }
  51. if (characterid) {
  52. user.characterid = characterid;
  53. }
  54. await user.save();
  55. }
  56. // 用户修改密码
  57. async uppasswd(data) {
  58. const { uid, oldpasswd, newpasswd } = data;
  59. assert(uid && oldpasswd && newpasswd, '缺少部分信息项');
  60. // 根据用户id查询其他用户表中是否存在相应数据
  61. const user = await this.model.findById(uid, '+passwd');
  62. // 如果用户不存在抛出异常
  63. if (!user) {
  64. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  65. }
  66. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  67. const _oldpasswd = await this.createJwtPwd(oldpasswd);
  68. // 如果两个密码不一致抛出异常
  69. if (_oldpasswd !== user.passwd) {
  70. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  71. }
  72. const _newpasswd = await this.createJwtPwd(newpasswd);
  73. user.passwd = _newpasswd;
  74. await user.save();
  75. }
  76. // 用户登录
  77. async login(data) {
  78. const { phone, passwd, type } = data;
  79. // 根据用户输入的手机号查询其他用户表中是否存在相应数据
  80. const user = await this.model.findOne({ phone });
  81. // 如果用户不存在抛出异常
  82. if (!user) {
  83. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  84. }
  85. const _user = await this.model.findOne({ phone }, '+passwd');
  86. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  87. const pas = await this.createJwtPwd(passwd);
  88. if (type !== user.type) {
  89. throw new BusinessError(ErrorCode.ACCESS_DENIED);
  90. }
  91. // 如果两个密码不一致抛出异常
  92. if (pas !== _user.passwd) {
  93. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  94. }
  95. // 取出用户的类型,根据用户类型返回相应信息
  96. return await this.createJwt(user);
  97. }
  98. // 创建登录Token
  99. async createJwtPwd(password) {
  100. const { secret, expiresIn, issuer } = this.config.jwt;
  101. const token = await jwt.sign(password, secret);
  102. return token;
  103. }
  104. // 创建登录Token
  105. async createJwt({ id, name, phone, type, characterid }) {
  106. const { secret, expiresIn = '1d', issuer = type } = this.config.jwt;
  107. const subject = phone;
  108. const _userid = id;
  109. let res = {};
  110. // 0-金控集团后台管理员,1-金融机构用户,2-政府用户
  111. if (type === '0') {
  112. res = { userid: _userid, name, type, uid: _userid, characterid };
  113. } else if (type === '1') {
  114. // 取得金融机构信息
  115. const institution = await this.imodel.findOne({ uid: _userid });
  116. if (institution) {
  117. res = { userid: institution.id, name, type, uid: _userid, characterid };
  118. } else {
  119. res = { name, type, uid: _userid, characterid };
  120. }
  121. } else if (type === '2') {
  122. res = { userid: _userid, name, type, uid: _userid, characterid };
  123. }
  124. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  125. return token;
  126. }
  127. async findUserByUserid(uid) {
  128. let user = await this.model.findById(uid);
  129. let newdata = {};
  130. if (user) {
  131. let type = '';
  132. // 用户类型,0-金控集团后台管理员,1-金融机构用户,2-政府用户
  133. if (user.type === '0') {
  134. type = '金控集团后台管理员';
  135. } else if (user.type === '1') {
  136. type = '金融机构用户';
  137. } else if (user.type === '2') {
  138. type = '政府用户';
  139. }
  140. newdata = { userid: user.id, name: user.name, type };
  141. } else {
  142. user = await this.cumodel.findById(uid);
  143. newdata = { userid: user.id, name: user.company_name, type: '企业用户' };
  144. }
  145. return newdata;
  146. }
  147. }
  148. module.exports = OtheruserService;