otheruser.js 5.9 KB

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