123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- '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, phone } = data;
- assert(name, '用户名不能为空');
- assert(password, '密码不能为空');
- const has_phone = await this.model.findOne({ phone, role });
- if (has_phone) {
- throw new BusinessError('此身份手机号已被注册,请更换手机号');
- }
- data.password = { secret: password };
- const res = await this.model.create(data);
- if (res) {
- const newdata = {
- name,
- phone: data.phone,
- passwd: data.password,
- uid: res.id,
- role: data.role,
- pid: data.pid,
- deptname: data.deptname,
- code: data.code,
- status: data.status,
- };
- const auth_user = await this.umodel.findOne({ phone, role });
- if (auth_user) {
- throw new BusinessError('此身份手机号已被注册,请更换手机号');
- }
- const authres = await this.umodel.create(newdata);
- return authres;
- }
- 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 };
- }
- const model = [
- 'name',
- 'education',
- 'school',
- 'birthDate',
- 'phone',
- 'qqwx',
- 'email',
- 'company',
- 'zwzc',
- 'expertise',
- 'expertimage',
- 'workexperience',
- 'scientific',
- 'undertakingproject',
- 'scienceaward',
- 'social',
- 'userid',
- 'code',
- 'role',
- 'status',
- 'pid',
- 'deptname',
- 'password',
- ];
- for (const key of model) {
- if (data[key]) {
- user[key] = data[key];
- }
- }
- 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.phone = data.phone;
- user.code = data.code;
- user.role = data.role;
- user.status = data.status;
- user.company = data.company;
- user.school = data.school;
- user.birthDate = data.birthDate;
- user.qqwx = data.qqwx;
- user.email = data.email;
- user.expertimage = data.expertimage;
- user.workexperience = data.workexperience;
- user.scientific = data.scientific;
- user.undertakingproject = data.undertakingproject;
- user.scienceaward = data.scienceaward;
- user.expertise = data.expertise;
- user.education = data.education;
- user.zwzc = data.zwzc;
- user.social = data.social;
- 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;
- }
- // 首页查询
- async indexQuery(data, { limit }) {
- const res = await this.model.aggregate([
- { $match: data },
- { $sample: { size: parseInt(limit) } },
- ]);
- return res;
- }
- // 重写删除方法
- /**
- * 根据id删除=>修改isdel为1
- * @param {Object} {id} 只接收id,不过需要解构,因为是object形式过来的
- */
- async delete({ id }) {
- const res = await this.model.update({ _id: ObjectId(id) }, { isdel: '1' });
- return res;
- }
- }
- module.exports = ExpertuserService;
|