123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- 'use strict';
- const md5 = require('md5');
- // const Excel = require('exceljs');
- const excelUtils = require('../util/excelUtils');
- const requestIp = require('request-ip');
- const sm4 = require('../util/sm4').sma4;
- const Controller = require('../extend/baseController');
- class SysUserController extends Controller {
- tag() {
- return this.ctx.service.sysUserService;
- }
- async update() {
- const { ctx } = this;
- const query = ctx.request.body;
- const { id } = query;
- delete query.id;
- // TODO 数据加密处理-CH
- const one = await this.tag().one(id);
- query.userName === sm4.decrypt_ECB(one.userName) ? delete query.userName : query.userName = sm4.encrypt_ECB(query.userName);
- query.phone === sm4.decrypt_ECB(one.phone) ? delete query.phone : query.phone = sm4.encrypt_ECB(query.phone);
- const result = await this.tag().update(id, query);
- ctx.success(result);
- }
- async onePop() {
- const { ctx } = this;
- const pop = this.ctx.getUserPop();
- const result = await this.tag().one(ctx.query.id, pop);
- ctx.logic(result, '查询失败');
- }
- async batchAdd() {
- const { ctx, service } = this;
- const query = ctx.request.body;
- delete query._id;
- if (!query.dept1) {
- delete query.dept1;
- }
- if (!query.dept2) {
- delete query.dept2;
- }
- if (!query.dept3) {
- delete query.dept3;
- }
- if (!query.dept4) {
- delete query.dept4;
- }
- if (!query.dept5) {
- delete query.dept5;
- }
- ctx.body = await service.sysUserService.batchAdd(query);
- }
- async updatePwd() {
- const { ctx, service } = this;
- const query = ctx.request.body;
- const { oldPwd, pwd } = query;
- const user = ctx.user;
- if (user.loginPwd == md5(oldPwd)) {
- await service.sysUserService.update('' + user._id, { loginPwd: md5(pwd) });
- // 添加log
- const IP = requestIp.getClientIp(ctx.request);
- const addQuery = {};
- if (user.role._id != this.app.config.defaultAdminRoleId) {
- if (user.dept1) {
- addQuery.dept1 = user.dept1;
- }
- if (user.dept2) {
- addQuery.dept2 = user.dept2;
- }
- if (user.dept3) {
- addQuery.dept3 = user.dept3;
- }
- if (user.dept4) {
- addQuery.dept4 = user.dept4;
- }
- if (user.dept5) {
- addQuery.dept5 = user.dept5;
- }
- }
- addQuery.loginName = user.loginName;
- addQuery.role = user.role;
- // addQuery.tableName = 'user';
- addQuery.type = '修改密码';
- addQuery.detail = JSON.stringify(query);
- addQuery.ipAddress = IP;
- addQuery.state = 'PC';
- await this.service.sysLogService.add(addQuery);
- ctx.success();
- } else {
- ctx.error('原密码不正确');
- }
- }
- async selectOne() {
- const { ctx } = this;
- const query = ctx.request.body;
- const { loginName, loginPwd } = query;
- const par1 = { loginName };
- const result = await ctx.model.SysUserModel.find({ $and: [ par1, { loginPwd: md5(loginPwd) }] });
- ctx.logic(result, '查询失败');
- }
- async updatePwdBeforeLogin() {
- const { ctx } = this;
- const query = ctx.request.body;
- const { loginName, oldPassword, newPassword } = query;
- const par1 = { loginName };
- const resultCount = await ctx.model.SysUserModel.find({ $and: [ par1, { loginPwd: md5(oldPassword) }] }).count();
- if (resultCount > 0) {
- const result = await ctx.model.SysUserModel.update(par1, { loginPwd: md5(newPassword), updatePwdTime: new Date(), updatePwdState: 1 });
- ctx.success(result);
- } else {
- ctx.logic(resultCount, '修改失败,请检查原始密码是否正确');
- }
- }
- async setPassword() {
- const { ctx, service } = this;
- const query = ctx.query;
- const { id } = query;
- const result = await service.sysUserService.update(id,
- { loginPwd: md5(this.app.config.defaultPassword) });
- // 添加log
- const IP = requestIp.getClientIp(ctx.request);
- const addQuery = {};
- if (ctx.user.role._id != this.app.config.defaultAdminRoleId) {
- if (ctx.user.dept1) {
- addQuery.dept1 = ctx.user.dept1;
- }
- if (ctx.user.dept2) {
- addQuery.dept2 = ctx.user.dept2;
- }
- if (ctx.user.dept3) {
- addQuery.dept3 = ctx.user.dept3;
- }
- if (ctx.user.dept4) {
- addQuery.dept4 = ctx.user.dept4;
- }
- if (ctx.user.dept5) {
- addQuery.dept5 = ctx.user.dept5;
- }
- }
- addQuery.loginName = ctx.user.loginName;
- addQuery.role = ctx.user.role;
- // addQuery.tableName = 'user';
- addQuery.type = '重置密码';
- addQuery.detail = JSON.stringify(query);
- addQuery.ipAddress = IP;
- addQuery.state = 'PC';
- await this.service.sysLogService.add(addQuery);
- ctx.success(result);
- }
- async setOpenId() {
- const { ctx, service } = this;
- const query = ctx.query;
- const { id } = query;
- const result = await service.sysUserService.update(id,
- { openId: '' });
- const result2 = await service.sysUserService.update(id,
- { appletsId: '' });
- ctx.success(result);
- }
- async setAppletsId() {
- const { ctx, service } = this;
- const query = ctx.query;
- const { id } = query;
- const result = await service.sysUserService.update(id,
- { appletsId: '' });
- ctx.success(result);
- }
- async upload() {
- const { ctx, service } = this;
- const result = await service.imageHandleService.upload();
- ctx.logic(result, '上传失败');
- }
- async listForPage() {
- const { ctx } = this;
- const user = ctx.user;
- const level = user.dept.level;
- // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
- if (!ctx.query.dept1) {
- delete ctx.query.dept1;
- }
- if (!ctx.query.dept2) {
- delete ctx.query.dept2;
- }
- if (!ctx.query.dept3) {
- delete ctx.query.dept3;
- }
- if (!ctx.query.dept4) {
- delete ctx.query.dept4;
- }
- if (!ctx.query.dept5) {
- delete ctx.query.dept5;
- }
- delete ctx.query.deptId;
- // admin的dept 存在冲突,所以它不需要结合
- if (user.role._id != this.app.config.defaultAdminRoleId) {
- ctx.query['dept' + level] = user.dept._id;
- }
- // 根据type 决定查什么角色
- if (ctx.query.type === 'admin') {
- ctx.query.role = this.app.config.defaultManagerRoleId;
- } else if ((ctx.query.type === 'user')) {
- ctx.query.role = this.app.config.defaultUserRoleId;
- }
- delete ctx.query.type;
- ctx.setRegexMongoSql('queryName', 'loginName');
- // 判断如果当前是采集员看数据的话 只能看他自己
- if (user.role._id + '' == this.app.config.defaultUserRoleId) {
- ctx.query.loginName = user.loginName;
- }
- ctx.setOrder('loginName');
- const accoutStatus = ctx.query.accoutStatus;
- if (accoutStatus) {
- switch (accoutStatus) {
- case '0':// 未认证
- ctx.query.$or = [{ file: { $exists: false } }, { file: { $in: '' } }];
- break;
- case '1':// 已认证 未绑定
- ctx.query.$and = [
- { file: { $exists: true, $ne: '' } },
- {
- $and: [
- { $or: [{ openId: { $exists: false } }, { openId: { $in: '' } }] },
- { $or: [{ appletsId: { $exists: false } }, { appletsId: { $in: '' } }] },
- ],
- },
- ];
- break;
- case '2':// 已认证 已绑定
- ctx.query.$and = [
- { file: { $exists: true, $ne: '' } },
- {
- $or: [
- { openId: { $exists: true, $ne: '' } },
- { appletsId: { $exists: true, $ne: '' } },
- ],
- },
- ];
- break;
- default:
- break;
- }
- }
- delete ctx.query.accoutStatus;
- const deptLevel = ctx.query.deptLevel;
- if (deptLevel) {
- switch (deptLevel) {
- case '3':// 区
- ctx.query.$and = [
- // { dept1: user.dept1._id },
- // { role: this.app.config.defaultManagerRoleId },
- { dept3: { $exists: true } },
- { dept4: { $exists: false } },
- { dept5: { $exists: false } },
- ];
- break;
- case '4':// 街道
- ctx.query.$and = [
- // { dept1: user.dept1._id },
- // { role: this.app.config.defaultManagerRoleId },
- { dept4: { $exists: true } },
- { dept5: { $exists: false } },
- ];
- break;
- case '5':// 村
- ctx.query.$and = [
- // { dept1: user.dept1._id },
- // { role: this.app.config.defaultManagerRoleId },
- { dept5: { $exists: true } },
- ];
- break;
- default:
- break;
- }
- }
- delete ctx.query.deptLevel;
- const result = await this.tag().listForPage(ctx.query, ctx.getUserPop());
- ctx.success(result);
- }
- async exportExcelByUser() {
- const { ctx } = this;
- delete ctx.query.sessionId;
- const user = ctx.user;
- const level = user.dept.level;
- // admin的dept 存在冲突,所以它不需要结合
- if (user.role._id != this.app.config.defaultAdminRoleId) {
- ctx.query['dept' + level] = user.dept._id;
- }
- // 根据type 决定查什么角色
- const type = ctx.query.type;
- if (type === 'admin') {
- ctx.query.role = this.app.config.defaultManagerRoleId;
- } else if ((ctx.query.type === 'user')) {
- ctx.query.role = this.app.config.defaultUserRoleId;
- }
- delete ctx.query.type;
- // 判断如果当前是采集员看数据的话 只能看他自己
- if (user.role._id + '' == this.app.config.defaultUserRoleId) {
- ctx.query.loginName = user.loginName;
- }
- ctx.setOrder('loginName');
- const result = await this.tag().list(ctx.query, ctx.getUserPop());
- if (result.length > 4 * 10000) {
- this.ctx.error('数据量过大,请联系管理员导出', 500);
- return;
- }
- // 导出是否*号替换开关-2024-5-16-CH
- const rule = await ctx.model.SysDeptDecModel.find();
- let ruleStatus = 0;
- rule.forEach(item => {
- if (item.level === user.dept.level) ruleStatus = item.excelStatus;
- });
- // 导出数据解密- CH
- for (let i = 0; i < result.length; i++) {
- result[i].userName = ruleStatus === 0 ? sm4.decrypt_ECB(result[i].userName) : excelUtils.formatName(sm4.decrypt_ECB(result[i].userName));
- result[i].phone = ruleStatus === 0 ? sm4.decrypt_ECB(result[i].phone) : excelUtils.formatPhone(sm4.decrypt_ECB(result[i].phone));
- }
- const config = [{
- sheetOptions: { pageSetup: { orientation: 'landscape', fitToHeight: true } },
- sheetHeader: [
- {
- headerName:
- '吉林省民政厅居家老年人巡视关爱探访系统' + (type === 'admin' ? '管理员账号' : '采集员账号'),
- headerConfig: { height: 40 },
- },
- ],
- sheetKey: [
- { label: '序号', key: 'num', letter: 'A', width: 6 },
- { label: '省', key: 'dept1.name', letter: 'B', width: 10 },
- { label: '地市', key: 'dept2.name', letter: 'C', width: 20 },
- { label: '县(市、区)', key: 'dept3.name', letter: 'D', width: 20 },
- { label: '乡镇(街道)', key: 'dept4.name', letter: 'E', width: 20 },
- { label: '村(居)民委员会', key: 'dept5.name', letter: 'F', width: 20 },
- { label: '账号', key: 'loginName', letter: 'G', width: 20 },
- { label: '角色', key: 'role.name', letter: 'H', width: 20 },
- { label: '姓名', key: 'userName', letter: 'I', width: 10 },
- { label: '性别', key: 'sex', letter: 'J', width: 6 },
- { label: '所在单位', key: 'company', letter: 'K', width: 20 },
- { label: '职务', key: 'job', letter: 'L', width: 20 },
- { label: '政治面貌', key: 'politicalOutlook', letter: 'M', width: 20 },
- { label: '常用联系电话', key: 'phone', letter: 'N', width: 20 },
- ],
- sheetData: result,
- }];
- const workbook = excelUtils.getExcel(config);
- if (!workbook) {
- this.ctx.error();
- return;
- }
- this.ctx.set('Content-Type', 'application/vnd.openxmlformats');
- this.ctx.set('Content-Disposition', "attachment;filename*=UTF-8' '" + encodeURIComponent(new Date().getTime()) + '.xlsx');
- this.ctx.body = await workbook.xlsx.writeBuffer();
- }
- async updateInfoWithUser() {
- const { ctx, service } = this;
- const query = ctx.request.body;
- const userId = ctx.user._id;
- delete query.id;
- if (userId) {
- const result = await service.sysUserService.update(userId, query);
- ctx.success(result);
- } else {
- ctx.error('修改用户信息失败,用户id 为空');
- }
- }
- async deleteWithSub() {
- const { ctx } = this;
- const query = ctx.query;
- query.userId = ctx.user._id;
- const result = await this.tag().deleteWithSub(query);
- if (result) {
- ctx.error(result);
- } else {
- ctx.success();
- }
- }
- // 积分数据列表查询---倒序显示
- async valueByUser() {
- const { ctx } = this;
- const user = ctx.user;
- const level = user.dept.level;
- // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
- if (!ctx.query.dept1) {
- delete ctx.query.dept1;
- }
- if (!ctx.query.dept2) {
- delete ctx.query.dept2;
- }
- if (!ctx.query.dept3) {
- delete ctx.query.dept3;
- }
- if (!ctx.query.dept4) {
- delete ctx.query.dept4;
- }
- if (!ctx.query.dept5) {
- delete ctx.query.dept5;
- }
- delete ctx.query.deptId;
- // admin的dept 存在冲突,所以它不需要结合
- if (user.role._id != this.app.config.defaultAdminRoleId) {
- ctx.query['dept' + level] = user.dept._id;
- }
- // 判断如果当前是采集员看数据的话 只能看他自己
- if (user.role._id + '' == this.app.config.defaultUserRoleId) {
- ctx.query.loginName = user.loginName;
- }
- if (!ctx.query.queryName) {
- delete ctx.query.queryName;
- }
- if (!ctx.query.userName) {
- delete ctx.query.userName;
- }
- const result = await this.tag().valueByUser(ctx.query);
- ctx.logic(result, '暂无积分数据');
- }
- async valueByUserOne() {
- const { ctx } = this;
- const result = await this.tag().valueByUserOne();
- ctx.success(result);
- }
- // 个人积分排名
- async sumUserOne() {
- const { ctx } = this;
- const result = await this.tag().sumUserOne();
- ctx.success(result);
- }
- }
- module.exports = SysUserController;
|