123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- '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.rmodel = this.ctx.model.Role;
- }
- // 重写创建方法
- async create(data) {
- const { name, phone, passwd, role } = data;
- console.log(data);
- assert(name && phone && passwd, '缺少部分信息项');
- // if (`${role}` !== '5') { assert(/^\d{11}$/i.test(phone), 'phone无效'); }
- // const user = await this.model.findOne({ phone });
- // if (user) {
- // throw new BusinessError(ErrorCode.DATA_EXISTED);
- // }
- const newdata = data;
- // const pas = await this.createJwtPwd(passwd);
- newdata.passwd = { secret: passwd };
- const res = await this.model.create(newdata);
- return res;
- }
- // 创建登录Token
- async createJwtPwd(password) {
- const { secret, expiresIn, issuer } = this.config.jwt;
- const token = await jwt.sign(password, secret);
- return token;
- }
- // 重写修改方法
- async update({ id }, data) {
- console.log(id);
- console.log(data);
- const user = await this.model.findById(id);
- if (data.name)user.name = data.name;
- if (data.phone) user.phone = data.phone;
- if (data.uid) user.uid = data.uid;
- if (data.role) user.role = data.role;
- if (data.menus) user.menus = data.menus;
- if (data.pid) user.pid = data.pid;
- if (data.deptname) user.deptname = data.deptname;
- if (data.remark) user.remark = data.remark;
- if (data.code) user.code = data.code;
- if (data.isdel) user.isdel = data.isdel;
- await user.save();
- }
- // 用户修改密码
- async uppasswd(data) {
- const { id, oldpasswd, newpasswd } = data;
- assert(id && oldpasswd && newpasswd, '缺少部分信息项');
- // 根据用户id查询其他用户表中是否存在相应数据
- const user = await this.model.findById(id, '+passwd');
- // 如果用户不存在抛出异常
- if (!user) {
- throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- }
- // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
- const _oldpasswd = await this.createJwtPwd(oldpasswd);
- // 如果两个密码不一致抛出异常
- if (_oldpasswd !== user.passwd.secret) {
- throw new BusinessError(ErrorCode.BAD_PASSWORD);
- }
- const _newpasswd = await this.createJwtPwd(newpasswd);
- user.passwd = { secret: _newpasswd };
- await user.save();
- }
- async querymenus({ id }) {
- const user = await this.model.findById(id);
- if (!user) {
- throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- }
- const _menus = [];
- for (const elm of user.menus) {
- const _menu = await this.rmodel.findById({ _id: ObjectId(elm) });
- if (_menu) {
- _menus.push({ id: elm, role_name: _menu.role_name, url: _menu.url });
- }
- }
- return { id, menus: _menus };
- }
- // 按条件更新方法
- async updatebyuid({ id }, data) {
- const user = await this.model.findOne({ uid: id });
- if (user) {
- user.name = data.name;
- user.deptname = data.deptname;
- }
- await user.save();
- }
- // 按条件更新方法
- async bind(data) {
- const user = await this.model.findById(data.uid);
- if (!user) {
- throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- }
- user.openid = data.openid;
- return await user.save();
- }
- async findByOpenid(openid) {
- const user = await this.model.findOne({ openid });
- return user;
- }
- async finduserlist({ skip, limit, ...info }) {
- const query = {
- ...info,
- $or: [{ role: '4' }, { role: '5' }, { role: '6' }],
- };
- const total = await this.model.count(query);
- const data = await this.model
- .find(query)
- .skip(Number(skip))
- .limit(Number(limit));
- return { data, total };
- }
- async businessuser({ pid, skip, limit }) {
- const query = { };
- pid ? (query.pid = pid) : '';
- const total = await this.model.count(query);
- const data = await this.model
- .find(query)
- .skip(Number(skip))
- .limit(Number(limit));
- console.log(data);
- return { data, total };
- }
- // 重写删除方法
- /**
- * 根据id删除=>修改isdel为1
- * @param {Object} {id} 只接收id,不过需要解构,因为是object形式过来的
- */
- async delete({ id }) {
- const res = await this.model.update({ _id: ObjectId(id) }, { isdel: '1' });
- if (res) {
- const { config } = this.app;
- if (!config) throw new BusinessError(ErrorCode.SERVICE_FAULT, '系统错误,需要检查系统项目设置');
- const { project } = config;
- if (!project) throw new BusinessError(ErrorCode.SERVICE_FAULT, '系统错误,需要检查系统中各项目的设置');
- const { market } = project;
- if (!market) throw new BusinessError(ErrorCode.SERVICE_FAULT, '系统错误,需要检查系统项目中项目设置的各项目配置');
- const url = `${market}/product/userdelete`;
- const r = await this.ctx.curl(url, {
- method: 'post',
- headers: {
- 'content-type': 'application/json',
- },
- dataType: 'json',
- data: { id },
- });
- console.log(r);
- }
- return res;
- }
- }
- module.exports = UserService;
|