sysUserController.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. 'use strict';
  2. const md5 = require('md5');
  3. // const Excel = require('exceljs');
  4. const excelUtils = require('../util/excelUtils');
  5. const requestIp = require('request-ip');
  6. const sm4 = require('../util/sm4').sma4;
  7. const Controller = require('../extend/baseController');
  8. class SysUserController extends Controller {
  9. tag() {
  10. return this.ctx.service.sysUserService;
  11. }
  12. async update() {
  13. const { ctx } = this;
  14. const query = ctx.request.body;
  15. const { id } = query;
  16. delete query.id;
  17. // TODO 数据加密处理-CH
  18. const one = await this.tag().one(id);
  19. query.userName === sm4.decrypt_ECB(one.userName) ? delete query.userName : query.userName = sm4.encrypt_ECB(query.userName);
  20. query.phone === sm4.decrypt_ECB(one.phone) ? delete query.phone : query.phone = sm4.encrypt_ECB(query.phone);
  21. const result = await this.tag().update(id, query);
  22. ctx.success(result);
  23. }
  24. async onePop() {
  25. const { ctx } = this;
  26. const pop = this.ctx.getUserPop();
  27. const result = await this.tag().one(ctx.query.id, pop);
  28. ctx.logic(result, '查询失败');
  29. }
  30. async batchAdd() {
  31. const { ctx, service } = this;
  32. const query = ctx.request.body;
  33. delete query._id;
  34. if (!query.dept1) {
  35. delete query.dept1;
  36. }
  37. if (!query.dept2) {
  38. delete query.dept2;
  39. }
  40. if (!query.dept3) {
  41. delete query.dept3;
  42. }
  43. if (!query.dept4) {
  44. delete query.dept4;
  45. }
  46. if (!query.dept5) {
  47. delete query.dept5;
  48. }
  49. ctx.body = await service.sysUserService.batchAdd(query);
  50. }
  51. async updatePwd() {
  52. const { ctx, service } = this;
  53. const query = ctx.request.body;
  54. const { oldPwd, pwd } = query;
  55. const user = ctx.user;
  56. if (user.loginPwd == md5(oldPwd)) {
  57. await service.sysUserService.update('' + user._id, { loginPwd: md5(pwd) });
  58. // 添加log
  59. const IP = requestIp.getClientIp(ctx.request);
  60. const addQuery = {};
  61. if (user.role._id != this.app.config.defaultAdminRoleId) {
  62. if (user.dept1) {
  63. addQuery.dept1 = user.dept1;
  64. }
  65. if (user.dept2) {
  66. addQuery.dept2 = user.dept2;
  67. }
  68. if (user.dept3) {
  69. addQuery.dept3 = user.dept3;
  70. }
  71. if (user.dept4) {
  72. addQuery.dept4 = user.dept4;
  73. }
  74. if (user.dept5) {
  75. addQuery.dept5 = user.dept5;
  76. }
  77. }
  78. addQuery.loginName = user.loginName;
  79. addQuery.role = user.role;
  80. // addQuery.tableName = 'user';
  81. addQuery.type = '修改密码';
  82. addQuery.detail = JSON.stringify(query);
  83. addQuery.ipAddress = IP;
  84. addQuery.state = 'PC';
  85. await this.service.sysLogService.add(addQuery);
  86. ctx.success();
  87. } else {
  88. ctx.error('原密码不正确');
  89. }
  90. }
  91. async selectOne() {
  92. const { ctx } = this;
  93. const query = ctx.request.body;
  94. const { loginName, loginPwd } = query;
  95. const par1 = { loginName };
  96. const result = await ctx.model.SysUserModel.find({ $and: [ par1, { loginPwd: md5(loginPwd) }] });
  97. ctx.logic(result, '查询失败');
  98. }
  99. async updatePwdBeforeLogin() {
  100. const { ctx } = this;
  101. const query = ctx.request.body;
  102. const { loginName, oldPassword, newPassword } = query;
  103. const par1 = { loginName };
  104. const resultCount = await ctx.model.SysUserModel.find({ $and: [ par1, { loginPwd: md5(oldPassword) }] }).count();
  105. if (resultCount > 0) {
  106. const result = await ctx.model.SysUserModel.update(par1, { loginPwd: md5(newPassword), updatePwdTime: new Date(), updatePwdState: 1 });
  107. ctx.success(result);
  108. } else {
  109. ctx.logic(resultCount, '修改失败,请检查原始密码是否正确');
  110. }
  111. }
  112. async setPassword() {
  113. const { ctx, service } = this;
  114. const query = ctx.query;
  115. const { id } = query;
  116. const result = await service.sysUserService.update(id,
  117. { loginPwd: md5(this.app.config.defaultPassword) });
  118. // 添加log
  119. const IP = requestIp.getClientIp(ctx.request);
  120. const addQuery = {};
  121. if (ctx.user.role._id != this.app.config.defaultAdminRoleId) {
  122. if (ctx.user.dept1) {
  123. addQuery.dept1 = ctx.user.dept1;
  124. }
  125. if (ctx.user.dept2) {
  126. addQuery.dept2 = ctx.user.dept2;
  127. }
  128. if (ctx.user.dept3) {
  129. addQuery.dept3 = ctx.user.dept3;
  130. }
  131. if (ctx.user.dept4) {
  132. addQuery.dept4 = ctx.user.dept4;
  133. }
  134. if (ctx.user.dept5) {
  135. addQuery.dept5 = ctx.user.dept5;
  136. }
  137. }
  138. addQuery.loginName = ctx.user.loginName;
  139. addQuery.role = ctx.user.role;
  140. // addQuery.tableName = 'user';
  141. addQuery.type = '重置密码';
  142. addQuery.detail = JSON.stringify(query);
  143. addQuery.ipAddress = IP;
  144. addQuery.state = 'PC';
  145. await this.service.sysLogService.add(addQuery);
  146. ctx.success(result);
  147. }
  148. async setOpenId() {
  149. const { ctx, service } = this;
  150. const query = ctx.query;
  151. const { id } = query;
  152. const result = await service.sysUserService.update(id,
  153. { openId: '' });
  154. const result2 = await service.sysUserService.update(id,
  155. { appletsId: '' });
  156. ctx.success(result);
  157. }
  158. async setAppletsId() {
  159. const { ctx, service } = this;
  160. const query = ctx.query;
  161. const { id } = query;
  162. const result = await service.sysUserService.update(id,
  163. { appletsId: '' });
  164. ctx.success(result);
  165. }
  166. async upload() {
  167. const { ctx, service } = this;
  168. const result = await service.imageHandleService.upload();
  169. ctx.logic(result, '上传失败');
  170. }
  171. async listForPage() {
  172. const { ctx } = this;
  173. const user = ctx.user;
  174. const level = user.dept.level;
  175. // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
  176. if (!ctx.query.dept1) {
  177. delete ctx.query.dept1;
  178. }
  179. if (!ctx.query.dept2) {
  180. delete ctx.query.dept2;
  181. }
  182. if (!ctx.query.dept3) {
  183. delete ctx.query.dept3;
  184. }
  185. if (!ctx.query.dept4) {
  186. delete ctx.query.dept4;
  187. }
  188. if (!ctx.query.dept5) {
  189. delete ctx.query.dept5;
  190. }
  191. delete ctx.query.deptId;
  192. // admin的dept 存在冲突,所以它不需要结合
  193. if (user.role._id != this.app.config.defaultAdminRoleId) {
  194. ctx.query['dept' + level] = user.dept._id;
  195. }
  196. // 根据type 决定查什么角色
  197. if (ctx.query.type === 'admin') {
  198. ctx.query.role = this.app.config.defaultManagerRoleId;
  199. } else if ((ctx.query.type === 'user')) {
  200. ctx.query.role = this.app.config.defaultUserRoleId;
  201. }
  202. delete ctx.query.type;
  203. ctx.setRegexMongoSql('queryName', 'loginName');
  204. // 判断如果当前是采集员看数据的话 只能看他自己
  205. if (user.role._id + '' == this.app.config.defaultUserRoleId) {
  206. ctx.query.loginName = user.loginName;
  207. }
  208. ctx.setOrder('loginName');
  209. const accoutStatus = ctx.query.accoutStatus;
  210. if (accoutStatus) {
  211. switch (accoutStatus) {
  212. case '0':// 未认证
  213. ctx.query.$or = [{ file: { $exists: false } }, { file: { $in: '' } }];
  214. break;
  215. case '1':// 已认证 未绑定
  216. ctx.query.$and = [
  217. { file: { $exists: true, $ne: '' } },
  218. {
  219. $and: [
  220. { $or: [{ openId: { $exists: false } }, { openId: { $in: '' } }] },
  221. { $or: [{ appletsId: { $exists: false } }, { appletsId: { $in: '' } }] },
  222. ],
  223. },
  224. ];
  225. break;
  226. case '2':// 已认证 已绑定
  227. ctx.query.$and = [
  228. { file: { $exists: true, $ne: '' } },
  229. {
  230. $or: [
  231. { openId: { $exists: true, $ne: '' } },
  232. { appletsId: { $exists: true, $ne: '' } },
  233. ],
  234. },
  235. ];
  236. break;
  237. default:
  238. break;
  239. }
  240. }
  241. delete ctx.query.accoutStatus;
  242. const deptLevel = ctx.query.deptLevel;
  243. if (deptLevel) {
  244. switch (deptLevel) {
  245. case '3':// 区
  246. ctx.query.$and = [
  247. // { dept1: user.dept1._id },
  248. // { role: this.app.config.defaultManagerRoleId },
  249. { dept3: { $exists: true } },
  250. { dept4: { $exists: false } },
  251. { dept5: { $exists: false } },
  252. ];
  253. break;
  254. case '4':// 街道
  255. ctx.query.$and = [
  256. // { dept1: user.dept1._id },
  257. // { role: this.app.config.defaultManagerRoleId },
  258. { dept4: { $exists: true } },
  259. { dept5: { $exists: false } },
  260. ];
  261. break;
  262. case '5':// 村
  263. ctx.query.$and = [
  264. // { dept1: user.dept1._id },
  265. // { role: this.app.config.defaultManagerRoleId },
  266. { dept5: { $exists: true } },
  267. ];
  268. break;
  269. default:
  270. break;
  271. }
  272. }
  273. delete ctx.query.deptLevel;
  274. const result = await this.tag().listForPage(ctx.query, ctx.getUserPop());
  275. ctx.success(result);
  276. }
  277. async exportExcelByUser() {
  278. const { ctx } = this;
  279. delete ctx.query.sessionId;
  280. const user = ctx.user;
  281. const level = user.dept.level;
  282. // admin的dept 存在冲突,所以它不需要结合
  283. if (user.role._id != this.app.config.defaultAdminRoleId) {
  284. ctx.query['dept' + level] = user.dept._id;
  285. }
  286. // 根据type 决定查什么角色
  287. const type = ctx.query.type;
  288. if (type === 'admin') {
  289. ctx.query.role = this.app.config.defaultManagerRoleId;
  290. } else if ((ctx.query.type === 'user')) {
  291. ctx.query.role = this.app.config.defaultUserRoleId;
  292. }
  293. delete ctx.query.type;
  294. // 判断如果当前是采集员看数据的话 只能看他自己
  295. if (user.role._id + '' == this.app.config.defaultUserRoleId) {
  296. ctx.query.loginName = user.loginName;
  297. }
  298. ctx.setOrder('loginName');
  299. const result = await this.tag().list(ctx.query, ctx.getUserPop());
  300. if (result.length > 4 * 10000) {
  301. this.ctx.error('数据量过大,请联系管理员导出', 500);
  302. return;
  303. }
  304. // 导出是否*号替换开关-2024-5-16-CH
  305. const rule = await ctx.model.SysDeptDecModel.find();
  306. let ruleStatus = 0;
  307. rule.forEach(item => {
  308. if (item.level === user.dept.level) ruleStatus = item.excelStatus;
  309. });
  310. // 导出数据解密- CH
  311. for (let i = 0; i < result.length; i++) {
  312. result[i].userName = ruleStatus === 0 ? sm4.decrypt_ECB(result[i].userName) : excelUtils.formatName(sm4.decrypt_ECB(result[i].userName));
  313. result[i].phone = ruleStatus === 0 ? sm4.decrypt_ECB(result[i].phone) : excelUtils.formatPhone(sm4.decrypt_ECB(result[i].phone));
  314. }
  315. const config = [{
  316. sheetOptions: { pageSetup: { orientation: 'landscape', fitToHeight: true } },
  317. sheetHeader: [
  318. {
  319. headerName:
  320. '吉林省民政厅居家老年人巡视关爱探访系统' + (type === 'admin' ? '管理员账号' : '采集员账号'),
  321. headerConfig: { height: 40 },
  322. },
  323. ],
  324. sheetKey: [
  325. { label: '序号', key: 'num', letter: 'A', width: 6 },
  326. { label: '省', key: 'dept1.name', letter: 'B', width: 10 },
  327. { label: '地市', key: 'dept2.name', letter: 'C', width: 20 },
  328. { label: '县(市、区)', key: 'dept3.name', letter: 'D', width: 20 },
  329. { label: '乡镇(街道)', key: 'dept4.name', letter: 'E', width: 20 },
  330. { label: '村(居)民委员会', key: 'dept5.name', letter: 'F', width: 20 },
  331. { label: '账号', key: 'loginName', letter: 'G', width: 20 },
  332. { label: '角色', key: 'role.name', letter: 'H', width: 20 },
  333. { label: '姓名', key: 'userName', letter: 'I', width: 10 },
  334. { label: '性别', key: 'sex', letter: 'J', width: 6 },
  335. { label: '所在单位', key: 'company', letter: 'K', width: 20 },
  336. { label: '职务', key: 'job', letter: 'L', width: 20 },
  337. { label: '政治面貌', key: 'politicalOutlook', letter: 'M', width: 20 },
  338. { label: '常用联系电话', key: 'phone', letter: 'N', width: 20 },
  339. ],
  340. sheetData: result,
  341. }];
  342. const workbook = excelUtils.getExcel(config);
  343. if (!workbook) {
  344. this.ctx.error();
  345. return;
  346. }
  347. this.ctx.set('Content-Type', 'application/vnd.openxmlformats');
  348. this.ctx.set('Content-Disposition', "attachment;filename*=UTF-8' '" + encodeURIComponent(new Date().getTime()) + '.xlsx');
  349. this.ctx.body = await workbook.xlsx.writeBuffer();
  350. }
  351. async updateInfoWithUser() {
  352. const { ctx, service } = this;
  353. const query = ctx.request.body;
  354. const userId = ctx.user._id;
  355. delete query.id;
  356. if (userId) {
  357. const result = await service.sysUserService.update(userId, query);
  358. ctx.success(result);
  359. } else {
  360. ctx.error('修改用户信息失败,用户id 为空');
  361. }
  362. }
  363. async deleteWithSub() {
  364. const { ctx } = this;
  365. const query = ctx.query;
  366. query.userId = ctx.user._id;
  367. const result = await this.tag().deleteWithSub(query);
  368. if (result) {
  369. ctx.error(result);
  370. } else {
  371. ctx.success();
  372. }
  373. }
  374. // 积分数据列表查询---倒序显示
  375. async valueByUser() {
  376. const { ctx } = this;
  377. const user = ctx.user;
  378. const level = user.dept.level;
  379. // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
  380. if (!ctx.query.dept1) {
  381. delete ctx.query.dept1;
  382. }
  383. if (!ctx.query.dept2) {
  384. delete ctx.query.dept2;
  385. }
  386. if (!ctx.query.dept3) {
  387. delete ctx.query.dept3;
  388. }
  389. if (!ctx.query.dept4) {
  390. delete ctx.query.dept4;
  391. }
  392. if (!ctx.query.dept5) {
  393. delete ctx.query.dept5;
  394. }
  395. delete ctx.query.deptId;
  396. // admin的dept 存在冲突,所以它不需要结合
  397. if (user.role._id != this.app.config.defaultAdminRoleId) {
  398. ctx.query['dept' + level] = user.dept._id;
  399. }
  400. // 判断如果当前是采集员看数据的话 只能看他自己
  401. if (user.role._id + '' == this.app.config.defaultUserRoleId) {
  402. ctx.query.loginName = user.loginName;
  403. }
  404. if (!ctx.query.queryName) {
  405. delete ctx.query.queryName;
  406. }
  407. if (!ctx.query.userName) {
  408. delete ctx.query.userName;
  409. }
  410. const result = await this.tag().valueByUser(ctx.query);
  411. ctx.logic(result, '暂无积分数据');
  412. }
  413. async valueByUserOne() {
  414. const { ctx } = this;
  415. const result = await this.tag().valueByUserOne();
  416. ctx.success(result);
  417. }
  418. // 个人积分排名
  419. async sumUserOne() {
  420. const { ctx } = this;
  421. const result = await this.tag().sumUserOne();
  422. ctx.success(result);
  423. }
  424. }
  425. module.exports = SysUserController;