'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 CompanyuserService extends CrudService { constructor(ctx) { super(ctx, 'company_user'); this.model = this.ctx.model.Companyuser; this.cmodel = this.ctx.model.Company; } // 重写创建方法 async create(data) { const { phone, passwd, company_name, person, institution_name, roles } = data; assert(person && phone && passwd, '缺少部分信息项'); 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); // 给用户发送消息告知注册成功 this.ctx.service.viewnews.insertViewNews('注册成功', '恭喜您注册成功,欢迎使用吉林省小微企业金融综合服务平台', res._id); return res; } // 用户修改密码 async uppasswd(data) { const { uid, oldpasswd, newpasswd } = data; assert(uid && oldpasswd && newpasswd, '缺少部分信息项'); // 根据用户id查询其他用户表中是否存在相应数据 const user = await this.model.findById(uid); const _user = await this.model.findOne({ phone: user.phone }, '+passwd'); // 将用户输入的密码进行加密并与查询到的用户数据密码相比对 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 } = 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 (pas !== _user.passwd) { throw new BusinessError(ErrorCode.BAD_PASSWORD); } return await this.createJwt(user); } // 直接返回用户信息的登录方法,不进行jwt async loginss(data) { const { phone, passwd } = 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 (pas !== _user.passwd) { throw new BusinessError(ErrorCode.BAD_PASSWORD); } return 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, phone, company_name, roles }) { const { secret, expiresIn = '1d', issuer = '3' } = this.config.jwt; const subject = phone; const _userid = id; // 根据uid查询企业基本信息 const company = await this.cmodel.findOne({ uid: _userid }); let res; if (company) { res = { userid: company.id, name: company_name, uid: _userid, roles }; } else { res = { name: company_name, uid: _userid, roles }; } // 将信息转成token返回给页面 const token = await jwt.sign(res, secret, { expiresIn, issuer, subject }); return token; } } module.exports = CompanyuserService;