'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; } async create(data) { const { name, password, role } = data; assert(name, '用户名不能为空'); assert(password, '密码不能为空'); const { phone } = data; const has_phone = await this.model.findOne({ phone, role }); if (has_phone) { throw new BusinessError('此身份手机号已被注册,请更换手机号'); } const newdata = data; newdata.password = { secret: password }; const res = await this.model.create(newdata); if (res) { const url = this.ctx.app.config.axios.auth.baseUrl; const newdata = { name, phone: data.phone, passwd: password, uid: res.id, role: data.role, pid: data.pid, deptname: data.deptname, code: data.code, openid: data.openid, }; await this.ctx.curl(url, { method: 'post', headers: { 'content-type': 'application/json', }, dataType: 'json', data: JSON.stringify(newdata), }); } return res; } // 用户修改密码 async uppasswd(data) { const { uid, newpasswd } = data; assert(uid && newpasswd, '缺少部分信息项'); // 根据用户id查询其他用户表中是否存在相应数据 const user = await this.model.findById(uid, '+password'); // 如果用户不存在抛出异常 if (!user) { throw new BusinessError(ErrorCode.USER_NOT_EXIST); } user.password = { secret: data.newpasswd }; await user.save(); } async update({ id }, data) { const user = await this.model.findById(id); // const { phone, role } = data; // const phoneList = await this.model.find({ phone, role }); // const is_has = phoneList.find(f => f.id !== id); // if (is_has) throw new BusinessError('此身份手机号已被注册,请更换手机号'); if (data.name) { user.name = data.name; } if (data.password) { user.password = { secret: data.password }; } user.cardnumber = data.cardnumber; user.phone = data.phone; user.email = data.email; user.addr = data.addr; user.img_path = data.img_path; user.is_qy = data.is_qy; user.cardfile_a = data.cardfile_a; user.cardfile_b = data.cardfile_b; user.img_qy = data.img_qy; user.resume = data.resume; user.major = data.major; user.institution_type = data.institution_type; user.institution_name = data.institution_name; user.institution_code = data.institution_code; user.institution_nature = data.institution_nature; user.office_phone = data.office_phone; user.profession = data.profession; user.status = data.status; user.is_del = data.is_del; user.role = data.role; user.token = data.token; user.columnid = data.columnid; if (data.deptname) { user.deptname = data.deptname; } if (data.pid) { user.pid = data.pid; } const res = await user.save(); if (res) { const url = this.ctx.app.config.axios.auth.baseUrl + '/updatebyuid/' + res.id; const newdata = { name: data.name, deptname: data.deptname }; await this.ctx.curl(url, { method: 'post', headers: { 'content-type': 'application/json', }, dataType: 'json', data: JSON.stringify(newdata), }); } return res; } async login({ phone, password, role }) { assert(phone, '手机号不能为空'); assert(password, '密码不能为空'); assert(role, '需要选择用户类型'); const user = await this.model.findOne({ phone, role }, '+password'); if (!user) { throw new BusinessError(ErrorCode.USER_NOT_EXIST); } if (user.password.secret !== password) { throw new BusinessError(ErrorCode.BAD_PASSWORD); } if (user.status === '0') { throw new BusinessError('用户未审核,请等待审核后再登陆'); } else if (user.status === '2') { throw new BusinessError('用户申请被拒绝,请联系平台管理员'); } return await this.createJwt(user); } // 创建登录Token async createJwt({ id, name, phone, is_qy, img_path, email, resume, major, office_phone, profession, role, status, columnid, }) { const { secret, expiresIn = '1d', issuer } = this.config.jwt; const subject = phone; // const _userid = id; const res = { name, phone, is_qy, id, img_path, email, resume, major, office_phone, profession, role, status, columnid, }; const token = await jwt.sign(res, secret, { expiresIn, issuer, subject }); return token; } } module.exports = UserService;