'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const jwt = require('jsonwebtoken'); class OtheruserService extends CrudService { constructor(ctx) { super(ctx, 'other_user'); this.model = this.ctx.model.Otheruser; this.imodel = this.ctx.model.Institution; this.cumodel = this.ctx.model.Companyuser; } // 重写创建方法 async create(data) { const { name, phone, passwd, type, characterid } = data; console.log(data); assert(name && phone && passwd && type, '缺少部分信息项'); assert(/^\d{11}$/i.test(phone), 'phone无效'); const user = await this.model.findOne({ phone }); if (user) { throw new BusinessError(ErrorCode.DATA_EXISTED); } const newdata = data; const pas = await this.createJwtPwd(passwd); newdata.passwd = pas; const res = await this.model.create(newdata); if (type === '1') { await this.imodel.create({ uid: res.id, name }); } return res; } // 重写修改方法 async update({ id }, data) { const { name, phone, passwd, type, characterid } = data; const user = await this.model.findById(id, '+passwd'); if (name) { user.name = name; } if (phone) { user.phone = phone; } if (passwd) { const newpasswd = await this.createJwtPwd(passwd); user.passwd = newpasswd; } if (type) { user.type = type; } if (characterid) { user.characterid = characterid; } await user.save(); } // 用户修改密码 async uppasswd(data) { const { uid, oldpasswd, newpasswd } = data; assert(uid && oldpasswd && newpasswd, '缺少部分信息项'); // 根据用户id查询其他用户表中是否存在相应数据 const user = await this.model.findById(uid, '+passwd'); // 如果用户不存在抛出异常 if (!user) { throw new BusinessError(ErrorCode.USER_NOT_EXIST); } // 将用户输入的密码进行加密并与查询到的用户数据密码相比对 const _oldpasswd = await this.createJwtPwd(oldpasswd); // 如果两个密码不一致抛出异常 if (_oldpasswd !== user.passwd) { throw new BusinessError(ErrorCode.BAD_PASSWORD); } const _newpasswd = await this.createJwtPwd(newpasswd); user.passwd = _newpasswd; await user.save(); } // 用户登录 async login(data) { const { phone, passwd, type } = data; // 根据用户输入的手机号查询其他用户表中是否存在相应数据 const user = await this.model.findOne({ phone }); // 如果用户不存在抛出异常 if (!user) { throw new BusinessError(ErrorCode.USER_NOT_EXIST); } const _user = await this.model.findOne({ phone }, '+passwd'); // 将用户输入的密码进行加密并与查询到的用户数据密码相比对 const pas = await this.createJwtPwd(passwd); if (type !== user.type) { throw new BusinessError(ErrorCode.ACCESS_DENIED); } // 如果两个密码不一致抛出异常 if (pas !== _user.passwd) { throw new BusinessError(ErrorCode.BAD_PASSWORD); } // 取出用户的类型,根据用户类型返回相应信息 return await this.createJwt(user); } // 创建登录Token async createJwtPwd(password) { const { secret, expiresIn, issuer } = this.config.jwt; const token = await jwt.sign(password, secret); return token; } // 创建登录Token async createJwt({ id, name, phone, type, characterid }) { const { secret, expiresIn = '1d', issuer = type } = this.config.jwt; const subject = phone; const _userid = id; let res = {}; // 0-金控集团后台管理员,1-金融机构用户,2-政府用户 if (type === '0') { res = { userid: _userid, name, type, uid: _userid, characterid }; } else if (type === '1') { // 取得金融机构信息 const institution = await this.imodel.findOne({ uid: _userid }); if (institution) { res = { userid: institution.id, name, type, uid: _userid, characterid }; } else { res = { name, type, uid: _userid, characterid }; } } else if (type === '2') { res = { userid: _userid, name, type, uid: _userid, characterid }; } const token = await jwt.sign(res, secret, { expiresIn, issuer, subject }); return token; } async findUserByUserid(uid) { let user = await this.model.findById(uid); let newdata = {}; if (user) { let type = ''; // 用户类型,0-金控集团后台管理员,1-金融机构用户,2-政府用户 if (user.type === '0') { type = '金控集团后台管理员'; } else if (user.type === '1') { type = '金融机构用户'; } else if (user.type === '2') { type = '政府用户'; } newdata = { userid: user.id, name: user.name, type }; } else { user = await this.cumodel.findById(uid); newdata = { userid: user.id, name: user.company_name, type: '企业用户' }; } return newdata; } } module.exports = OtheruserService;