sysUserController.js 14 KB

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