123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- 'use strict';
- const uuid = require('uuid/v1');
- const md5 = require('md5');
- const Service = require('../service/baseService');
- const mongoose = require('mongoose');
- const { sma4: sm4 } = require('../util/sm4');
- class SysUserService extends Service {
- tag() {
- return this.ctx.model.SysUserModel;
- }
- async login(data) {
- const { model } = this.ctx;
- const pop = this.ctx.getUserPop();
- const result = await model.SysUserModel
- .find({ loginName: data.loginName, loginPwd: data.loginPwd })
- .populate(pop);
- if (result.length === 1) {
- const newUser = JSON.parse(JSON.stringify(result[0]));
- newUser.sessionId = uuid()
- .replace(/-/g, '');
- delete newUser.loginPwd;
- await this.app.redis.set(newUser.sessionId, JSON.stringify(newUser), 'EX',
- this.ctx.app.config.sessionTimeOut);
- return newUser.sessionId;
- }
- return null;
- }
- async updatePwdWithOpenId(query) {
- const { ctx, service } = this;
- const { oldPwd, pwd } = query;
- const users = await service.sysUserService.list({ openId: ctx.openId, loginPwd: md5(oldPwd) });
- if (users.length === 1) {
- return await service.sysUserService.update(users[0]._id, { loginPwd: md5(pwd) });
- }
- return null;
- }
- async bing(query) {
- const { service, ctx } = this;
- const { name, pwd, openId } = query;
- const user = ctx.user;
- const result = await service.sysUserService.oneData({ loginName: name, loginPwd: pwd });
- if (result) {
- if ('' + result.role._id === this.app.config.defaultUserRoleId) { // 采集员
- if (user && user.loginName === name) {
- console.log('user.loginName === name' + name);
- return null;
- }
- if (!result.openId || result.openId === '') {
- if (result.file && result.file !== '') {
- if (result.userName && result.sex
- && result.company && result.job
- && result.politicalOutlook && result.phone) {
- const res = await service.sysUserService.update(result._id, { openId: ctx.get('openId') });
- // const res = await service.sysUserService.update(result._id, { openId });
- console.log('result._id=======' + result._id);
- console.log('openId===========' + ctx.get('openId'));
- console.log('updateRes========' + JSON.stringify(res));
- return null;
- }
- return '请管理员到后台完善资料方可登录!';
- }
- return '请管理员到后台上传认证文件方可登录!';
- }
- return '该账号已被其他用户绑定!';
- }
- return '请使用采集员账号!';
- }
- return '用户名或者密码错误,请联系管理员!';
- }
- async deleteWithSub(data) {
- const { ctx, service } = this;
- const { model } = ctx;
- const infoList = await service.infoService.list({ userid: data.id });
- if (infoList.length > 0) {
- return ' 账号下已有采集数据,禁止删除 ';
- }
- const visitList = await service.visitService.list({ userid: data.id });
- if (visitList.length > 0) {
- return ' 账号下已有探访数据,禁止删除 ';
- }
- const user = await this.one(data.id, ctx.getUserPop());
- if (user.role._id == this.app.config.defaultAdminRoleId) {
- return '没有权限,不能删除';
- }
- if (data.userId == data.id) {
- return '不能删除自己';
- }
- await model.SysUserModel.deleteOne({ _id: data.id });
- return null;
- }
- async batchAdd(data) {
- const { model } = this.ctx;
- // 通过传来的 deptId 获取当前code
- const dept = await model.SysDeptModel.find({ _id: data.deptId });
- const code = dept[0].code;
- const level = dept[0].level;
- // dept1 - 5 前端传入 type 前端传入 admin 是管理员 user 是采集员
- if (data.type == 'admin') {
- delete data.type;
- // 生成管理员
- const users = await model.SysUserModel.find({
- loginName: code,
- role: this.app.config.defaultManagerRoleId, dept: data.deptId,
- });
- if (users.length > 0) {
- return { code: 1, data: {}, message: '当前地区已存在管理员' };
- }
- data.dept = data.deptId;
- data.role = this.app.config.defaultManagerRoleId;
- data.loginName = code;
- data.loginPwd = md5(this.app.config.defaultPassword);
- await model.SysUserModel.create(data);
- // 生成地区自主上报员
- if (level === '5') {
- const ownerCode = code + '000';
- const ownerUser = await model.SysUserModel.find({
- loginName: ownerCode,
- role: this.app.config.defaultOwnerManagerRoleId, dept: data.deptId,
- });
- if (ownerUser.length === 0) {
- data.role = this.app.config.defaultOwnerManagerRoleId;
- data.loginName = ownerCode;
- data.loginPwd = md5(this.app.config.defaultPassword);
- await model.SysUserModel.create(data);
- }
- }
- return { code: 0, data: {}, message: code };
- } else if (data.type == 'user' && data.deptId == data.dept5) {
- delete data.type;
- let str = '';
- const userCodes = [];
- let userNum = data.userNum || 1;
- userNum = parseInt(userNum) > 20 ? '20' : userNum;
- for (let i = 0; i < userNum; i++) {
- const users = await model.SysUserModel.find({
- role: this.app.config.defaultUserRoleId, dept: data.deptId,
- })
- .sort({ loginName: 1 });
- if (users.length > 0) {
- // 如果存在 就取最后一个
- let number = users[users.length - 1].loginName.replace(code, '');
- number = parseInt(number);
- number++;
- let num = '' + number;
- while (num.length < 3) {
- num = '0' + num;
- }
- data.dept = data.deptId;
- data.role = this.app.config.defaultUserRoleId;
- data.loginName = code + num;
- data.loginPwd = md5(this.app.config.defaultPassword);
- await model.SysUserModel.create(data);
- userCodes.push(code + num);
- } else {
- data.dept = data.deptId;
- data.role = this.app.config.defaultUserRoleId;
- data.loginName = code + '001';
- data.loginPwd = md5(this.app.config.defaultPassword);
- await model.SysUserModel.create(data);
- userCodes.push(code + '001');
- }
- }
- str += userCodes.join(',');
- // 生成采集员
- return { code: 0, data: {}, message: str };
- }
- return { code: 1, data: {}, message: '参数错误' };
- }
- // async valueByUser(data) {
- // const { ctx } = this;
- // const { model } = ctx;
- //
- // const result = await this.listForPage(data, ctx.getUserPop());
- // // const result = await this.listForPage(data);
- // const newResult = [];
- // for (const item of result.list) {
- // const valueInfoCount = await model.ValueModel.find({ userid: item._id, type: '0' })
- // .countDocuments();
- // const valueVisitCount = await model.ValueModel.find({ userid: item._id, type: '1' })
- // .countDocuments();
- // newResult.push({ _id: item._id,
- // loginName: item.loginName,
- // userName: item.userName,
- // dept: item.dept1.name + item.dept2.name + item.dept3.name + item.dept4.name + item.dept5.name,
- // valueInfoCount, valueVisitCount });
- // }
- //
- // return { list: newResult, count: result.count };
- // }
- // 积分数据列表查询---倒序显示
- async valueByUser(data) {
- const { ctx } = this;
- const { model } = ctx;
- // const page = parseInt(data.page);
- // const rows = parseInt(data.rows);
- const page = data.page || 1;
- const rows = Number.parseInt(data.rows) || this.app.config.defaultPageSize;
- const where = {};
- if (data.dept1) {
- where.dept1 = this.app.mongoose.Types.ObjectId(data.dept1);// 省
- }
- if (data.dept2) {
- where.dept2 = this.app.mongoose.Types.ObjectId(data.dept2); // 市
- }
- if (data.dept3) {
- where.dept3 = this.app.mongoose.Types.ObjectId(data.dept3); // 区
- }
- if (data.dept4) {
- where.dept4 = this.app.mongoose.Types.ObjectId(data.dept4); // 乡
- }
- if (data.dept5) {
- where.dept5 = this.app.mongoose.Types.ObjectId(data.dept5); // 社区
- }
- if (data.queryName) {
- const oneUser = await model.SysUserModel.find({ loginName: data.queryName });
- if (oneUser.length > 0) {
- where.userid = oneUser[0]._id;
- }
- }
- if (data.userName) {
- const oneUser = await model.SysUserModel.find({ userName: sm4.encrypt_ECB(data.userName) });
- if (oneUser.length > 0) {
- where.userid = oneUser[0]._id;
- }
- }
- const restltList = await this.ctx.model.ValueModel.aggregate([
- {
- $match: where,
- },
- { $group:
- {
- _id: '$userid',
- dept1: { $first: '$dept1' },
- dept2: { $first: '$dept2' },
- dept3: { $first: '$dept3' },
- dept4: { $first: '$dept4' },
- dept5: { $first: '$dept5' },
- type0: { $sum: {
- $cond: [{ $eq: [ '$type', '0' ] }, 1, 0 ], // 采集审核通过数
- } },
- type1: { $sum: {
- $cond: [{ $eq: [ '$type', '1' ] }, 1, 0 ], // 探访数
- } },
- },
- },
- { $project: {
- _id: 1,
- dept1: 1,
- dept2: 1,
- dept3: 1,
- dept4: 1,
- dept5: 1,
- type0: 1,
- type1: 1,
- totalValue: { $add: [{ $multiply: [ '$type0', 5 ] }, { $multiply: [ '$type1', 1 ] }] },
- } },
- { $sort: { totalValue: -1 } },
- {
- $skip: rows * (parseInt(page) - 1),
- },
- {
- $limit: rows,
- },
- { $lookup: { from: 'sys_user', localField: '_id', foreignField: '_id', as: 'user' } },
- { $unwind: { path: '$user', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_dept', localField: 'dept1', foreignField: '_id', as: 'dept1' } },
- { $unwind: { path: '$dept1', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_dept', localField: 'dept2', foreignField: '_id', as: 'dept2' } },
- { $unwind: { path: '$dept2', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_dept', localField: 'dept3', foreignField: '_id', as: 'dept3' } },
- { $unwind: { path: '$dept3', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_dept', localField: 'dept4', foreignField: '_id', as: 'dept4' } },
- { $unwind: { path: '$dept4', preserveNullAndEmptyArrays: true } },
- { $lookup: { from: 'sys_dept', localField: 'dept5', foreignField: '_id', as: 'dept5' } },
- { $unwind: { path: '$dept5', preserveNullAndEmptyArrays: true } },
- { $project: {
- _id: 1,
- loginName: '$user.loginName',
- userName: '$user.userName',
- valueInfoCount: '$type0',
- valueVisitCount: '$type1',
- totalValue: '$totalValue',
- dept: { $concat: [ '$dept1.name', '$dept2.name', '$dept3.name', '$dept4.name', '$dept5.name' ] },
- } },
- ]).allowDiskUse(true);// 允许使用磁盘缓存
- const countList = await this.ctx.model.ValueModel.aggregate([
- {
- $match: where,
- },
- { $group:
- {
- _id: '$userid',
- },
- },
- ]).allowDiskUse(true);// 允许使用磁盘缓存
- return { list: restltList, count: countList.length };
- }
- async valueByUserOne() {
- const { ctx } = this;
- const { model } = ctx;
- const user = ctx.user;
- const valueInfoCount = await model.ValueModel.find({ userid: user._id, type: '0' })
- .countDocuments();
- const valueVisitCount = await model.ValueModel.find({ userid: user._id, type: '1' })
- .countDocuments();
- return { _id: user._id, loginName: user.loginName, valueInfoCount, valueVisitCount };
- }
- // 个人积分排名
- async sumUserOne() {
- const { ctx } = this;
- const { model } = ctx;
- const user = ctx.user;
- // const infoCount = await model.InfoModel.aggregate([
- // { $match: { dept1: user.dept1._id, userid: user._id, status: '3' } },
- // { $group: { _id: '$_id', count: { $sum: 1 } } },
- // { $count: 'count' },
- // ]).allowDiskUse(true);
- const totalCount = await model.VisitModel.find({ userid: user._id }).countDocuments();
- const visitCount = await model.VisitModel.aggregate([
- { $match: { dept1: user.dept1._id, userid: user._id } },
- { $group: { _id: '$infoId', count: { $sum: 1 } } },
- { $count: 'count' },
- ]).allowDiskUse(true);
- const visitOrder = await model.VisitModel.aggregate([
- { $match: { dept1: user.dept1._id } },
- { $group: { _id: '$userid', count: { $sum: 1 } } }, // 地区下所有采集员的探访次数
- { $group: { _id: '$count', rank: { $sum: 1 } } }, // 探访次数 统计
- { $match: { _id: { $gt: totalCount } } }, // 排名
- { $group: { _id: null, count: { $sum: '$rank' } } },
- { $project: { _id: 0, count: 1 } },
- ]).allowDiskUse(true);
- if (visitOrder.length == 0) {
- const arr = { count: 0 };
- visitOrder.push(arr);
- }
- return { _id: user._id, loginName: user.loginName, userName: user.userName, totalCount, visitCount, visitOrder };
- }
- // getUserCond(data, page, rows, sort) {
- // const cond = [];
- //
- // cond.push({ $match: data });
- // console.log('提交件', data);
- //
- // if (sort) {
- // cond.push({ $sort: sort });
- // }
- // cond.push({ $skip: (page - 1) * rows });
- // cond.push({ $limit: rows });
- // cond.push({
- // $lookup:
- // {
- // from: 'value',
- // localField: '_id',
- // foreignField: 'userid',
- // as: 'values',
- // },
- // });
- // cond.push({ $unwind: { path: '$values', preserveNullAndEmptyArrays: true } });
- // cond.push({ $group:
- // {
- // _id: { id: '$_id', type: '$values.type' },
- // loginName: { $first: '$loginName' },
- // counter: { $sum: {
- // $cond: [ '$values', 1, 0 ],
- // } },
- // },
- // });
- // cond.push({
- // $group:
- // {
- // _id: { id: '$_id.id' },
- // loginName: { $first: '$loginName' },
- // number: { $push: { counter: '$counter', type: '$_id.type' } },
- // },
- // });
- // cond.push({
- // $project: {
- // _id: '$_id.id',
- // loginName: 1,
- // number: 1,
- // },
- // });
- // return cond;
- //
- // }
- async findAdminUser() {
- return await this.ctx.model.SysUserModel.findOne({ loginName: 'admin' });
- }
- async findUser(deptName, level, userName, parentName) {
- const { ctx } = this;
- const { model } = ctx;
- ctx.logger.error('level', deptName, level, parentName);
- if (level > 3) {
- const res = await model.SysUserModel.aggregate([
- { $lookup: { from: 'sys_dept', localField: 'dept', foreignField: '_id', as: 'deptName' } },
- { $lookup: { from: 'sys_dept', localField: `dept${level - 1}`, foreignField: '_id', as: `dept${level - 1}Name` } },
- { $unwind: { path: '$deptName', preserveNullAndEmptyArrays: true } },
- { $unwind: { path: '$dept' + (level - 1) + 'Name', preserveNullAndEmptyArrays: true } },
- { $match: {
- 'deptName.name': deptName,
- ['dept' + (level - 1) + 'Name.name']: parentName,
- role: this.app.mongoose.Types.ObjectId(this.app.config.defaultManagerRoleId),
- } },
- ]).allowDiskUse(true);
- if (res.length > 0) {
- const filters = res.filter(item => item.loginName == userName || userName.indexOf(item.loginName) == 0);
- if (filters.length > 0) {
- return filters[0];
- }
- return res[0];
- }
- } else {
- const res = await model.SysUserModel.aggregate([
- { $lookup: { from: 'sys_dept', localField: 'dept', foreignField: '_id', as: 'deptName' } },
- { $unwind: { path: '$deptName', preserveNullAndEmptyArrays: true } },
- { $match: {
- 'deptName.name': deptName,
- loginName: { $ne: 'admin' },
- role: this.app.mongoose.Types.ObjectId(this.app.config.defaultManagerRoleId),
- } },
- ]).allowDiskUse(true);
- if (res.length > 0) {
- const filters = res.filter(item => item.loginName == userName || userName.indexOf(item.loginName) == 0);
- if (filters.length > 0) {
- return filters[0];
- }
- return res[0];
- }
- }
- return null;
- }
- }
- module.exports = SysUserService;
|