'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 UserService extends CrudService { constructor(ctx) { super(ctx, 'user'); this.model = this.ctx.model.User; this.smodel = this.ctx.model.Staff; this.emodel = this.ctx.model.Expert; } // 重写创建方法 async register(data) { console.log(data); assert(data.name && data.phone && data.passwd, '缺少部分信息项'); assert(/^\d{11}$/i.test(data.phone), '无效的手机号'); const user = await this.model.findOne({ phone: data.phone }); if (user) { throw new BusinessError(ErrorCode.DATA_EXISTED); } const newdata = _.cloneDeep(data); const pas = await this.createJwtPwd(data.passwd); newdata.passwd = pas; let result = {}; if (data.type === '0' || data.type === '1') { result = await this.smodel.create(data); } if (data.type === '2') { result = await this.emodel.create(data); } newdata.userid = result.id; const res = await this.model.create(newdata); return res; } async update({ id }, data) { const user = await this.model.findById(id); const { userid, openid, role_id, name, phone, type } = data; if (userid) { user.userid = userid; } if (openid) { user.openid = openid; } if (role_id) { user.role_id = role_id; } if (name) { if (user.type === '0' || user.type === '1') { const staff = await this.smodel.findById(user.userid); staff.name = name; await staff.save(); } if (user.type === '2') { const expert = await this.emodel.findById(user.userid); expert.name = name; await expert.save(); } user.name = name; } if (phone) { const _user = await this.model.find({ phone }); if (_.isEqual(_user.length, 0) || (_.isEqual(_user.length, 1) && _.isEqual(_user[0].id, user.id))) { if (user.type === '0' || user.type === '1') { const staff = await this.smodel.findById(user.userid); staff.phone = phone; await staff.save(); } if (user.type === '2') { const expert = await this.emodel.findById(user.userid); expert.phone = phone; await expert.save(); } user.phone = phone; } else { throw new BusinessError(ErrorCode.DATA_EXISTED); } } if (type) { user.type = type; } const res = await user.save(); return res; } // 用户修改密码 async uppasswd(data) { const { userid, oldpasswd, newpasswd } = data; assert(userid && oldpasswd && newpasswd, '缺少部分信息项'); // 根据用户id查询用户表中是否存在相应数据 const user = await this.model.findById(userid, '+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 } = 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); } async createJwtPwd(password) { const { secret, expiresIn, issuer } = this.config.jwt; const token = await jwt.sign(password, secret); return token; } // 创建登录Token async createJwt({ id, userid, openid, role_id, name, phone, type }) { const { secret, expiresIn = '1d', issuer = type } = this.config.jwt; const subject = phone; const _uid = id; const res = { userid, uid: _uid, openid, role_id, name, phone, type }; const token = await jwt.sign(res, secret, { expiresIn, issuer, subject }); return token; } } module.exports = UserService;