123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- '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 create(body) {
- const { passwd, ...data } = body;
- data.passwd = { secret: passwd };
- return await this.model.create(data);
- }
- // 重写创建方法
- async register(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 = data.passwd;
- newdata.passwd = { secret: pas };
- let result = {};
- if (data.type === '0' || data.type === '1' || data.type === '4') {
- 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' || user.type === '4') {
- 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' || user.type === '4') {
- 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);
- }
- // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
- // 如果两个密码不一致抛出异常
- if (oldpasswd !== user.passwd) {
- throw new BusinessError(ErrorCode.BAD_PASSWORD);
- }
- user.passwd = { secret: newpasswd };
- await user.save();
- }
- async login(data) {
- const { phone, passwd } = data;
- // 根据用户输入的手机号查询其他用户表中是否存在相应数据
- const user = await this.model.findOne({ phone }, '+passwd');
- // 如果用户不存在抛出异常
- if (!user) {
- throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- }
- // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
- // 如果两个密码不一致抛出异常
- if (passwd !== user.passwd.secret) {
- 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;
|