sysUserController.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. { $or: [{ openId: { $exists: false } }, { openId: { $in: '' } }] },
  185. ];
  186. break;
  187. case '2':// 已认证 已绑定
  188. ctx.query.$and = [
  189. { file: { $exists: true, $ne: '' } },
  190. { openId: { $exists: true, $ne: '' } },
  191. ];
  192. break;
  193. default:
  194. break;
  195. }
  196. }
  197. delete ctx.query.accoutStatus;
  198. const deptLevel = ctx.query.deptLevel;
  199. if (deptLevel) {
  200. switch (deptLevel) {
  201. case '3':// 区
  202. ctx.query.$and = [
  203. // { dept1: user.dept1._id },
  204. // { role: this.app.config.defaultManagerRoleId },
  205. { dept3: { $exists: true } },
  206. { dept4: { $exists: false } },
  207. { dept5: { $exists: false } },
  208. ];
  209. break;
  210. case '4':// 街道
  211. ctx.query.$and = [
  212. // { dept1: user.dept1._id },
  213. // { role: this.app.config.defaultManagerRoleId },
  214. { dept4: { $exists: true } },
  215. { dept5: { $exists: false } },
  216. ];
  217. break;
  218. case '5':// 村
  219. ctx.query.$and = [
  220. // { dept1: user.dept1._id },
  221. // { role: this.app.config.defaultManagerRoleId },
  222. { dept5: { $exists: true } },
  223. ];
  224. break;
  225. default:
  226. break;
  227. }
  228. }
  229. delete ctx.query.deptLevel;
  230. const result = await this.tag().listForPage(ctx.query, ctx.getUserPop());
  231. ctx.success(result);
  232. }
  233. async exportExcelByUser() {
  234. const { ctx } = this;
  235. delete ctx.query.sessionId;
  236. const user = ctx.user;
  237. const level = user.dept.level;
  238. // admin的dept 存在冲突,所以它不需要结合
  239. if (user.role._id != this.app.config.defaultAdminRoleId) {
  240. ctx.query['dept' + level] = user.dept._id;
  241. }
  242. // 根据type 决定查什么角色
  243. const type = ctx.query.type;
  244. if (type === 'admin') {
  245. ctx.query.role = this.app.config.defaultManagerRoleId;
  246. } else if ((ctx.query.type === 'user')) {
  247. ctx.query.role = this.app.config.defaultUserRoleId;
  248. }
  249. delete ctx.query.type;
  250. // 判断如果当前是采集员看数据的话 只能看他自己
  251. if (user.role._id + '' == this.app.config.defaultUserRoleId) {
  252. ctx.query.loginName = user.loginName;
  253. }
  254. ctx.setOrder('loginName');
  255. const result = await this.tag().list(ctx.query, ctx.getUserPop());
  256. if (result.length > 4 * 10000) {
  257. this.ctx.error('数据量过大,请联系管理员导出', 500);
  258. return;
  259. }
  260. const config = [{
  261. sheetOptions: { pageSetup: { orientation: 'landscape', fitToHeight: true } },
  262. sheetHeader: [
  263. {
  264. headerName:
  265. '吉林省民政厅居家老年人巡视关爱探访系统' + (type === 'admin' ? '管理员账号' : '采集员账号'),
  266. headerConfig: { height: 40 },
  267. },
  268. ],
  269. sheetKey: [
  270. { label: '序号', key: 'num', letter: 'A', width: 6 },
  271. { label: '省', key: 'dept1.name', letter: 'B', width: 10 },
  272. { label: '地市', key: 'dept2.name', letter: 'C', width: 20 },
  273. { label: '县(市、区)', key: 'dept3.name', letter: 'D', width: 20 },
  274. { label: '乡镇(街道)', key: 'dept4.name', letter: 'E', width: 20 },
  275. { label: '村(居)民委员会', key: 'dept5.name', letter: 'F', width: 20 },
  276. { label: '账号', key: 'loginName', letter: 'G', width: 20 },
  277. { label: '角色', key: 'role.name', letter: 'H', width: 20 },
  278. { label: '姓名', key: 'userName', letter: 'I', width: 10 },
  279. { label: '性别', key: 'sex', letter: 'J', width: 6 },
  280. { label: '所在单位', key: 'company', letter: 'K', width: 20 },
  281. { label: '职务', key: 'job', letter: 'L', width: 20 },
  282. { label: '政治面貌', key: 'politicalOutlook', letter: 'M', width: 20 },
  283. { label: '常用联系电话', key: 'phone', letter: 'N', width: 20 },
  284. ],
  285. sheetData: result,
  286. }];
  287. const workbook = excelUtils.getExcel(config);
  288. if (!workbook) {
  289. this.ctx.error();
  290. return;
  291. }
  292. this.ctx.set('Content-Type', 'application/vnd.openxmlformats');
  293. this.ctx.set('Content-Disposition', "attachment;filename*=UTF-8' '" + encodeURIComponent(new Date().getTime()) + '.xlsx');
  294. this.ctx.body = await workbook.xlsx.writeBuffer();
  295. }
  296. async updateInfoWithUser() {
  297. const { ctx, service } = this;
  298. const query = ctx.request.body;
  299. const userId = ctx.user._id;
  300. delete query.id;
  301. if (userId) {
  302. const result = await service.sysUserService.update(userId, query);
  303. ctx.success(result);
  304. } else {
  305. ctx.error('修改用户信息失败,用户id 为空');
  306. }
  307. }
  308. async deleteWithSub() {
  309. const { ctx } = this;
  310. const query = ctx.query;
  311. query.userId = ctx.user._id;
  312. const result = await this.tag().deleteWithSub(query);
  313. if (result) {
  314. ctx.error(result);
  315. } else {
  316. ctx.success();
  317. }
  318. }
  319. // async valueByUser() {
  320. // const { ctx } = this;
  321. // const user = ctx.user;
  322. // const level = user.dept.level;
  323. // // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
  324. // if (!ctx.query.dept1) {
  325. // delete ctx.query.dept1;
  326. // }
  327. // if (!ctx.query.dept2) {
  328. // delete ctx.query.dept2;
  329. // }
  330. // if (!ctx.query.dept3) {
  331. // delete ctx.query.dept3;
  332. // }
  333. // if (!ctx.query.dept4) {
  334. // delete ctx.query.dept4;
  335. // }
  336. // if (!ctx.query.dept5) {
  337. // delete ctx.query.dept5;
  338. // }
  339. // delete ctx.query.deptId;
  340. //
  341. // // admin的dept 存在冲突,所以它不需要结合
  342. // if (user.role._id != this.app.config.defaultAdminRoleId) {
  343. // ctx.query['dept' + level] = user.dept._id;
  344. // }
  345. //
  346. // ctx.query.role = this.app.config.defaultUserRoleId;
  347. // ctx.setRegexMongoSql('queryName', 'loginName');
  348. // if (!ctx.query.userName) {
  349. // delete ctx.query.userName;
  350. // } else {
  351. // ctx.query.userName = { $regex: ctx.query.userName };
  352. // }
  353. // // 判断如果当前是采集员看数据的话 只能看他自己
  354. // if (user.role._id + '' == this.app.config.defaultUserRoleId) {
  355. // ctx.query.loginName = user.loginName;
  356. // }
  357. // ctx.setOrder('loginName');
  358. //
  359. // const result = await this.tag().valueByUser(ctx.query);
  360. //
  361. // ctx.success(result);
  362. // }
  363. // 积分数据列表查询---倒序显示
  364. async valueByUser() {
  365. const { ctx } = this;
  366. const user = ctx.user;
  367. const level = user.dept.level;
  368. // 判断当前的dept权限 和传入的5级权限 不能超过当前人dept
  369. if (!ctx.query.dept1) {
  370. delete ctx.query.dept1;
  371. }
  372. if (!ctx.query.dept2) {
  373. delete ctx.query.dept2;
  374. }
  375. if (!ctx.query.dept3) {
  376. delete ctx.query.dept3;
  377. }
  378. if (!ctx.query.dept4) {
  379. delete ctx.query.dept4;
  380. }
  381. if (!ctx.query.dept5) {
  382. delete ctx.query.dept5;
  383. }
  384. delete ctx.query.deptId;
  385. // admin的dept 存在冲突,所以它不需要结合
  386. if (user.role._id != this.app.config.defaultAdminRoleId) {
  387. ctx.query['dept' + level] = user.dept._id;
  388. }
  389. // 判断如果当前是采集员看数据的话 只能看他自己
  390. if (user.role._id + '' == this.app.config.defaultUserRoleId) {
  391. ctx.query.loginName = user.loginName;
  392. }
  393. if (!ctx.query.queryName) {
  394. delete ctx.query.queryName;
  395. }
  396. if (!ctx.query.userName) {
  397. delete ctx.query.userName;
  398. }
  399. const result = await this.tag().valueByUser(ctx.query);
  400. ctx.logic(result, '暂无积分数据');
  401. }
  402. async valueByUserOne() {
  403. const { ctx } = this;
  404. const result = await this.tag().valueByUserOne();
  405. ctx.success(result);
  406. }
  407. // 个人积分排名
  408. async sumUserOne() {
  409. const { ctx } = this;
  410. const result = await this.tag().sumUserOne();
  411. ctx.success(result);
  412. }
  413. }
  414. module.exports = SysUserController;