|
@@ -0,0 +1,206 @@
|
|
|
+'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: pas };
|
|
|
+ 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) {
|
|
|
+ const {
|
|
|
+ name,
|
|
|
+ phone,
|
|
|
+ passwd,
|
|
|
+ openid,
|
|
|
+ role,
|
|
|
+ menus,
|
|
|
+ remark,
|
|
|
+ uid,
|
|
|
+ deptid,
|
|
|
+ deptname,
|
|
|
+ pid,
|
|
|
+ } = data;
|
|
|
+ const user = await this.model.findById(id, '+passwd');
|
|
|
+ if (name) {
|
|
|
+ user.name = name;
|
|
|
+ }
|
|
|
+ if (phone) {
|
|
|
+ user.phone = phone;
|
|
|
+ }
|
|
|
+ if (passwd) {
|
|
|
+ const newpasswd = await this.createJwtPwd(passwd);
|
|
|
+ user.passwd = { secret: newpasswd };
|
|
|
+ }
|
|
|
+ if (openid) {
|
|
|
+ user.openid = openid;
|
|
|
+ }
|
|
|
+ if (role) {
|
|
|
+ user.role = role;
|
|
|
+ }
|
|
|
+ if (menus) {
|
|
|
+ user.menus = menus;
|
|
|
+ }
|
|
|
+ if (uid) {
|
|
|
+ user.uid = uid;
|
|
|
+ }
|
|
|
+ if (deptid) {
|
|
|
+ user.deptid = deptid;
|
|
|
+ }
|
|
|
+ if (deptname) {
|
|
|
+ user.deptname = deptname;
|
|
|
+ }
|
|
|
+ if (pid) {
|
|
|
+ user.pid = pid;
|
|
|
+ }
|
|
|
+ if (remark) {
|
|
|
+ user.remark = remark;
|
|
|
+ }
|
|
|
+ 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 = { code: { $regex: /^.{9}$/ } };
|
|
|
+ pid ? (query.pid = pid) : '';
|
|
|
+ const total = await this.model.count(query);
|
|
|
+ const data = await this.model
|
|
|
+ .find(query)
|
|
|
+ .skip(Number(skip))
|
|
|
+ .limit(Number(limit));
|
|
|
+ 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;
|