'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 ExpertuserService extends CrudService { constructor(ctx) { super(ctx, 'experts_user'); this.model = this.ctx.model.Expertsuser; this.umodel = 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 }; const user = await this.ctx.curl(url, { method: 'post', headers: { 'content-type': 'application/json', }, dataType: 'json', data: JSON.stringify(newdata), }); if (user.data.errcode === 0) { const result = await this.model.findById(res.id); result.userid = user.data.data.id; result.save(); } } return res; } async update({ id }, data) { const user = await this.model.findById(id); const { phone } = data; const phoneList = await this.model.find({ phone }); 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.gender = data.gender; user.phone = data.phone; user.email = data.email; user.addr = data.addr; user.img_path = data.img_path; user.birthday = data.birthday; user.level = data.level; user.levelname = data.levelname; user.position = data.position; user.school = data.school; user.xl = data.xl; user.xw = data.xw; user.major = data.major; user.professional = data.professional; user.resume = data.resume; user.project = data.project; user.academic = data.academic; user.paper = data.paper; user.remark = data.remark; user.role = data.role; user.status = data.status; 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 upgrade({ id }, data) { await this.umodel.deleteOne({ _id: ObjectId(id) }); const { phone, uid } = data; const phoneList = await this.model.find({ phone }); const is_has = phoneList.find(f => f.id !== id); if (is_has) throw new BusinessError('此手机号已被注册,请更换手机号'); const user = {}; if (data.name) { user.name = data.name; } if (data.password) { user.password = { secret: data.password }; } user.cardnumber = data.cardnumber; user.gender = data.gender; user.phone = data.phone; user.email = data.email; user.addr = data.addr; user.img_path = data.img_path; user.birthday = data.birthday; user.level = data.level; user.levelname = data.levelname; user.position = data.position; user.school = data.school; user.xl = data.xl; user.xw = data.xw; user.major = data.major; user.professional = data.professional; user.resume = data.resume; user.project = data.project; user.academic = data.academic; user.paper = data.paper; user.remark = data.remark; user.role = data.role; user.status = data.status; const res = await this.model.create(user); if (res) { const url = this.ctx.app.config.axios.auth.baseUrl + '/update/' + uid; const newdata = { uid: res.id, role: data.role, name: data.name }; await this.ctx.curl(url, { method: 'post', headers: { 'content-type': 'application/json', }, dataType: 'json', data: JSON.stringify(newdata), }); } return res; } } module.exports = ExpertuserService;