setting_controller.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const officeList = require('../const/cld_office').list;
  10. const settingConst = require('../const/setting.js');
  11. const settingMenu = require('../../config/menu').settingMenu;
  12. const accountGroup = require('../const/account_group').group;
  13. const permission = require('../const/account_permission').permission;
  14. const projectLog = require('../const/project_log');
  15. const imType = require('../const/tender').imType;
  16. const S2b = require('../lib/s2b');
  17. const measureType = require('../const/tender').measureType;
  18. const sendToWormhole = require('stream-wormhole');
  19. const path = require('path');
  20. const funSet = require('../const/fun_set');
  21. module.exports = app => {
  22. class SettingController extends app.BaseController {
  23. /**
  24. * 构造函数
  25. *
  26. * @param {Object} ctx - egg全局context
  27. * @return {void}
  28. */
  29. constructor(ctx) {
  30. super(ctx);
  31. ctx.subMenu = settingMenu;
  32. }
  33. async _checkMenu(pid) {
  34. const ctx = this.ctx;
  35. await this.ctx.service.s2bProj.refreshSessionS2b(pid);
  36. ctx.subMenu.s2b.display = !!ctx.session.sessionProject.gxby || !!ctx.session.sessionProject.dagl;
  37. // this.ctx.subMenu.s2b.display = false;
  38. }
  39. /**
  40. * 项目信息页面(Get)
  41. *
  42. * @param {Object} ctx - egg全局变量
  43. * @return {void}
  44. */
  45. async info(ctx) {
  46. try {
  47. // 获取项目数据
  48. const projectId = ctx.session.sessionProject.id;
  49. await this._checkMenu(projectId);
  50. const projectData = await ctx.service.project.getDataById(projectId);
  51. if (projectData === null) {
  52. throw '没有对应的项目数据';
  53. }
  54. // 获取销售人员数据
  55. const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id);
  56. // 数据规则
  57. const rule = ctx.service.project.rule('saveInfo');
  58. const jsValidator = await this.jsValidator.convert(rule).build();
  59. const officeName = officeList[salesmanData.office];
  60. const date = new Date(projectData.create_time * 1000);// 如果date为10位不需要乘1000
  61. const Y = date.getFullYear() + '-';
  62. const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  63. const D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
  64. const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  65. const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  66. const s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  67. const dateStr = Y + M + D + h + m + s;
  68. const renderData = {
  69. projectData,
  70. salesmanData,
  71. officeName,
  72. officeList,
  73. jsValidator,
  74. dateStr,
  75. };
  76. await this.layout('setting/info.ejs', renderData);
  77. } catch (error) {
  78. console.log(error);
  79. ctx.redirect('/dashboard');
  80. }
  81. }
  82. /**
  83. * 项目设置 -- 账号设置(Get)
  84. * @param ctx
  85. * @return {Promise<void>}
  86. */
  87. async user(ctx) {
  88. try {
  89. // 获取项目数据
  90. const projectId = ctx.session.sessionProject.id;
  91. await this._checkMenu(projectId);
  92. const projectData = await ctx.service.project.getDataById(projectId);
  93. if (projectData === null) {
  94. throw '没有对应的项目数据';
  95. }
  96. if (ctx.session.sessionUser.is_admin === 0) {
  97. throw '没有访问权限';
  98. }
  99. let keyword = '';
  100. if (ctx.query.keyword) {
  101. keyword = ctx.query.keyword;
  102. }
  103. let company = '';
  104. if (ctx.query.company) {
  105. company = ctx.query.company;
  106. }
  107. const page = ctx.page;
  108. const pageSize = ctx.pageSize;
  109. // 过滤数据
  110. ctx.service.projectAccount.searchFilter(ctx.request.query, projectId);
  111. ctx.sort = ['id', 'desc'];
  112. const total = await ctx.service.projectAccount.getCountWithBuilder();
  113. // 获取数据规则
  114. // const rule = ctx.service.projectAccount.rule('updateUser');
  115. // const frontRule = ctx.helper.validateConvert(rule);
  116. // const total = await ctx.service.projectAccount.count({ project_id: projectId });
  117. // 获取项目用户列表
  118. // const accountData = await ctx.service.projectAccount.getAllDataByCondition({
  119. // where: { project_id: projectId },
  120. // columns: ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group'],
  121. // limit: (app.config.pageSize * (page - 1)),
  122. // offset: app.config.pageSize,
  123. // });
  124. // const columns = ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group', 'bind'];
  125. // const accountData = await ctx.service.projectAccount.getListByProjectId(columns, projectId);
  126. const accountData = await ctx.service.projectAccount.getListWithBuilder();
  127. // 获取账号个数
  128. const user_total = await ctx.service.projectAccount.count({ project_id: projectId });
  129. // 分页相关
  130. const pageInfo = {
  131. page,
  132. pageSizeSelect: 1,
  133. pageSize,
  134. total_num: total,
  135. total: Math.ceil(total / pageSize),
  136. queryData: JSON.stringify(ctx.urlInfo.query),
  137. };
  138. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: projectId } });
  139. const renderData = {
  140. projectData,
  141. accountData,
  142. accountGroup,
  143. permission,
  144. pageInfo,
  145. keyword,
  146. user_total,
  147. unitList,
  148. company,
  149. // rule: JSON.stringify(frontRule),
  150. };
  151. await this.layout('setting/user.ejs', renderData, 'setting/user_modal.ejs');
  152. } catch (error) {
  153. console.log(error);
  154. ctx.redirect('/dashboard');
  155. }
  156. }
  157. /**
  158. * 项目设置 -- 账号解绑设置(Post)
  159. * @param ctx
  160. * @return {Promise<void>}
  161. */
  162. async userUnbind(ctx) {
  163. const projectData = ctx.session.sessionProject;
  164. try {
  165. if (projectData.id === undefined) {
  166. throw '不存在对应的项目数据';
  167. }
  168. if (ctx.session.sessionUser.is_admin === 0) {
  169. throw '非管理员无权解绑账号';
  170. }
  171. const accountId = parseInt(ctx.request.body.id);
  172. const result = await ctx.service.projectAccount.update({ bind: 0 }, { id: accountId });
  173. if (!result) {
  174. throw '解绑失败';
  175. }
  176. this.setMessage('解绑成功', this.messageType.SUCCESS);
  177. } catch (error) {
  178. console.log(error);
  179. this.setMessage(error.toString(), this.messageType.ERROR);
  180. }
  181. ctx.redirect(ctx.request.header.referer);
  182. }
  183. /**
  184. * 项目权限 -- 账号设置(Get)
  185. * @param ctx
  186. * @return {Promise<void>}
  187. */
  188. async userPermissionSet(ctx) {
  189. try {
  190. // 获取项目数据
  191. const projectId = ctx.session.sessionProject.id;
  192. const projectData = await ctx.service.project.getDataById(projectId);
  193. if (projectData === null) {
  194. throw '没有对应的项目数据';
  195. }
  196. if (ctx.session.sessionUser.is_admin === 0) {
  197. throw '没有访问权限';
  198. }
  199. let keyword = '';
  200. if (ctx.query.keyword) {
  201. keyword = ctx.query.keyword;
  202. }
  203. let company = '';
  204. if (ctx.query.company) {
  205. company = ctx.query.company;
  206. }
  207. // 获取数据规则
  208. const page = ctx.page;
  209. const pageSize = ctx.pageSize;
  210. const columns = ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'bind', 'account_group', 'permission', 'cooperation'];
  211. // 过滤数据
  212. ctx.service.projectAccount.searchFilter(ctx.request.query, projectId, columns);
  213. ctx.sort = ['id', 'desc'];
  214. const total = await ctx.service.projectAccount.getCountWithBuilder();
  215. const accountData = await ctx.service.projectAccount.getListWithBuilder();
  216. // 获取账号个数
  217. const user_total = await ctx.service.projectAccount.count({ project_id: projectId });
  218. // 分页相关
  219. const pageInfo = {
  220. page,
  221. pageSizeSelect: 1,
  222. pageSize,
  223. total_num: total,
  224. total: Math.ceil(total / pageSize),
  225. queryData: JSON.stringify(ctx.urlInfo.query),
  226. };
  227. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: projectId } });
  228. const renderData = {
  229. projectData,
  230. accountData,
  231. accountGroup,
  232. permission,
  233. keyword,
  234. permissionStr: JSON.stringify(permission),
  235. pageInfo,
  236. user_total,
  237. unitList,
  238. company,
  239. // rule: JSON.stringify(frontRule),
  240. };
  241. await this.layout('setting/user_permission.ejs', renderData, 'setting/user_permission_modal.ejs');
  242. } catch (error) {
  243. console.log(error);
  244. ctx.redirect('/dashboard');
  245. }
  246. }
  247. /**
  248. * 项目权限 -- 参建单位(Get)
  249. * @param ctx
  250. * @return {Promise<void>}
  251. */
  252. async userUnit(ctx) {
  253. try {
  254. // 获取项目数据
  255. const projectId = ctx.session.sessionProject.id;
  256. const projectData = await ctx.service.project.getDataById(projectId);
  257. if (projectData === null) {
  258. throw '没有对应的项目数据';
  259. }
  260. if (ctx.session.sessionUser.is_admin === 0) {
  261. throw '没有访问权限';
  262. }
  263. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: projectId } });
  264. for (const u of unitList) {
  265. const accountList = await ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: projectId, company: u.name } });
  266. u.account_num = accountList.length;
  267. }
  268. // 获取账号个数
  269. const user_total = await ctx.service.projectAccount.count({ project_id: projectId });
  270. const renderData = {
  271. projectData,
  272. accountGroup,
  273. unitList,
  274. user_total,
  275. fujianOssPath: ctx.app.config.fujianOssPath,
  276. };
  277. await this.layout('setting/user_unit.ejs', renderData, 'setting/user_unit_modal.ejs');
  278. } catch (error) {
  279. console.log(error);
  280. ctx.redirect('/dashboard');
  281. }
  282. }
  283. /**
  284. * 项目设置 -- 单位新增设置(Post)
  285. * @param ctx
  286. * @return {Promise<void>}
  287. */
  288. async userUnitAdd(ctx) {
  289. const projectData = ctx.session.sessionProject;
  290. try {
  291. // 验证数据
  292. if (projectData.id === undefined) {
  293. throw '不存在对应的项目数据';
  294. }
  295. // 获取验证规则
  296. const rule = ctx.service.constructionUnit.rule('add');
  297. ctx.validate(rule);
  298. ctx.request.body.pid = projectData.id;
  299. const result = await ctx.service.constructionUnit.save(ctx.request.body);
  300. if (!result) {
  301. throw '保存单位失败';
  302. }
  303. this.setMessage('保存单位成功', this.messageType.SUCCESS);
  304. ctx.redirect('/' + ctx.controllerName + '/user/unit');
  305. } catch (error) {
  306. console.log(error);
  307. this.setMessage(error.toString(), this.messageType.ERROR);
  308. ctx.redirect(ctx.request.header.referer);
  309. }
  310. }
  311. /**
  312. * 项目设置 -- 单位编辑(Post)
  313. * @param ctx
  314. * @return {Promise<void>}
  315. */
  316. async userUnitSave(ctx) {
  317. const projectData = ctx.session.sessionProject;
  318. const responseData = {
  319. err: 0, msg: '', data: null,
  320. };
  321. try {
  322. // 验证数据
  323. if (projectData.id === undefined) {
  324. throw '不存在对应的项目数据';
  325. }
  326. const data = JSON.parse(ctx.request.body.data);
  327. switch (data.type) {
  328. case 'update':
  329. const post_data = {
  330. pid: projectData.id,
  331. id: data.id,
  332. };
  333. post_data[data.val_name] = data.val;
  334. if (data.name) post_data.name = data.name;
  335. const result = await ctx.service.constructionUnit.save(post_data);
  336. if (!result) {
  337. throw '保存单位失败';
  338. }
  339. if (post_data.name) {
  340. const accountList = await ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: projectData.id, company: post_data.name } });
  341. responseData.data = { account_num: accountList.length };
  342. }
  343. break;
  344. case 'delete':
  345. const accountList = await ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: projectData.id, company: data.name } });
  346. if (accountList.length > 0) {
  347. throw '该单位下还存在账号,无法删除';
  348. }
  349. await ctx.service.constructionUnit.del(data.id);
  350. break;
  351. case 'del-sign':
  352. const info = await ctx.service.constructionUnit.getDataById(data.id);
  353. if (info.sign_path) {
  354. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + info.sign_path);
  355. }
  356. await ctx.service.constructionUnit.update({ sign_path: null }, { id: info.id });
  357. break;
  358. default:
  359. break;
  360. }
  361. ctx.body = responseData;
  362. } catch (err) {
  363. this.log(err);
  364. ctx.body = { err: 1, msg: err.toString(), data: null };
  365. }
  366. }
  367. async userUnitUpload(ctx) {
  368. const responseData = {
  369. err: 0, msg: '', data: null,
  370. };
  371. try {
  372. const stream = await ctx.getFileStream();
  373. const create_time = Date.parse(new Date()) / 1000;
  374. const fileInfo = path.parse(stream.filename);
  375. const filepath = `app/public/upload/sign/unit/qianzhang_${create_time + fileInfo.ext}`;
  376. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  377. await sendToWormhole(stream);
  378. const result = await ctx.service.constructionUnit.update({ sign_path: filepath }, { id: stream.fields.id });
  379. if (result) {
  380. responseData.data = { sign_path: filepath };
  381. } else {
  382. throw '添加数据库失败';
  383. }
  384. } catch (err) {
  385. this.log(err);
  386. responseData.err = 1;
  387. responseData.msg = err;
  388. }
  389. ctx.body = responseData;
  390. }
  391. /**
  392. * 项目设置 -- 账号启用和停用设置(Post)
  393. * @param ctx
  394. * @return {Promise<void>}
  395. */
  396. async userSwitch(ctx) {
  397. const responseData = {
  398. err: 0, msg: '', data: null,
  399. };
  400. try {
  401. // 获取项目数据
  402. const projectId = ctx.session.sessionProject.id;
  403. const projectData = await ctx.service.project.getDataById(projectId);
  404. if (projectData === null) {
  405. throw '没有对应的项目数据';
  406. }
  407. if (ctx.session.sessionUser.is_admin === 0) {
  408. throw '没有访问权限';
  409. }
  410. const data = JSON.parse(ctx.request.body.data);
  411. const result = await ctx.service.projectAccount.update(data, { id: data.id });
  412. if (!result) {
  413. throw '提交数据失败';
  414. }
  415. } catch (err) {
  416. this.log(err);
  417. responseData.err = 1;
  418. responseData.msg = err;
  419. }
  420. ctx.body = responseData;
  421. }
  422. /**
  423. * 项目设置 -- 账号添加(Post)
  424. * @param ctx
  425. * @return {Promise<void>}
  426. */
  427. async addUser(ctx) {
  428. const projectData = ctx.session.sessionProject;
  429. try {
  430. // 验证数据
  431. if (projectData.id === undefined) {
  432. throw '不存在对应的项目数据';
  433. }
  434. // 获取验证规则
  435. const rule = ctx.service.projectAccount.rule('add');
  436. ctx.validate(rule);
  437. // 判断新密码的强度
  438. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  439. if (!reg.test(ctx.request.body.password)) {
  440. throw '请设置至少包含数字和字母的密码';
  441. }
  442. ctx.request.body.project_id = projectData.id;
  443. const result = await ctx.service.projectAccount.save(ctx.request.body);
  444. if (!result) {
  445. throw '保存账号数据失败';
  446. }
  447. this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
  448. ctx.redirect('/' + ctx.controllerName + '/user');
  449. } catch (error) {
  450. console.log(error);
  451. ctx.session.postError = error.toString();
  452. this.setMessage(error.toString(), this.messageType.ERROR);
  453. ctx.redirect(ctx.request.header.referer);
  454. }
  455. }
  456. /**
  457. * 项目设置 -- 账号编辑(Post)
  458. * @param ctx
  459. * @return {Promise<void>}
  460. */
  461. async updateUser(ctx) {
  462. const projectData = ctx.session.sessionProject;
  463. try {
  464. // 验证数据
  465. if (projectData.id === undefined) {
  466. throw '不存在对应的项目数据';
  467. }
  468. // 获取验证规则
  469. const rule = ctx.service.projectAccount.rule('modify');
  470. ctx.validate(rule);
  471. const result = await ctx.service.projectAccount.save(ctx.request.body);
  472. if (!result) {
  473. throw '保存账号数据失败';
  474. }
  475. this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
  476. ctx.redirect(ctx.request.header.referer);
  477. } catch (error) {
  478. console.log(error);
  479. this.setMessage(error.toString(), this.messageType.ERROR);
  480. ctx.redirect(ctx.request.header.referer);
  481. }
  482. }
  483. async resetUserPassword(ctx) {
  484. try {
  485. // 获取项目数据
  486. const projectId = ctx.session.sessionProject.id;
  487. const projectData = await ctx.service.project.getDataById(projectId);
  488. if (projectData === null) {
  489. throw '没有对应的项目数据';
  490. }
  491. if (ctx.session.sessionUser.is_admin === 0) {
  492. throw '没有访问权限';
  493. }
  494. const accountId = parseInt(ctx.request.body.id);
  495. let password = ctx.request.body.reset_password;
  496. password = password.toString();
  497. const account = ctx.request.body.account !== undefined ? ctx.request.body.account : '';
  498. if (isNaN(accountId) || accountId <= 0 || password.length < 6) {
  499. throw '参数错误';
  500. }
  501. // 判断新密码的强度
  502. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  503. if (!reg.test(password)) {
  504. throw '请设置至少包含数字和字母的密码';
  505. }
  506. const result = await ctx.service.projectAccount.resetPassword(accountId, password, account);
  507. if (!result) {
  508. throw '重置密码失败!';
  509. }
  510. this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
  511. ctx.redirect(ctx.request.header.referer);
  512. } catch (error) {
  513. console.log(error);
  514. this.setMessage(error.toString(), this.messageType.ERROR);
  515. ctx.redirect(ctx.request.header.referer);
  516. }
  517. }
  518. /**
  519. * 项目设置 -- 账号权限编辑(Post)
  520. * @param ctx
  521. * @return {Promise<void>}
  522. */
  523. async permission(ctx) {
  524. const projectData = ctx.session.sessionProject;
  525. try {
  526. // 验证数据
  527. if (projectData.id === undefined) {
  528. throw '不存在对应的项目数据';
  529. }
  530. const accountId = parseInt(ctx.request.body.id);
  531. const accountInfo = await ctx.service.projectAccount.getDataById(accountId);
  532. if (accountInfo === null) {
  533. throw '不存在该客户';
  534. }
  535. const result = await ctx.service.projectAccount.permissionSave(accountId, ctx.request.body);
  536. if (!result) {
  537. throw '修改权限失败!';
  538. }
  539. this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
  540. ctx.redirect(ctx.request.header.referer);
  541. } catch (error) {
  542. console.log(error);
  543. this.setMessage(error.toString(), this.messageType.ERROR);
  544. ctx.redirect(ctx.request.header.referer);
  545. }
  546. }
  547. /**
  548. * 项目设置 -- 自定义标段分类(Get)
  549. *
  550. * @param ctx
  551. * @return {Promise<void>}
  552. */
  553. async category(ctx) {
  554. try {
  555. // 获取项目数据
  556. const projectId = ctx.session.sessionProject.id;
  557. await this._checkMenu(projectId);
  558. const projectData = await ctx.service.project.getDataById(projectId);
  559. if (projectData === null) {
  560. throw '没有对应的项目数据';
  561. }
  562. if (ctx.session.sessionUser.is_admin === 0) {
  563. throw '没有访问权限';
  564. }
  565. const categoryData = await ctx.service.category.getAllCategory(projectId);
  566. const tenderData = await ctx.service.tender.getList('', null, 1);
  567. const renderData = {
  568. projectData,
  569. categoryType: settingConst.cType,
  570. categoryData,
  571. tenderData,
  572. };
  573. await this.layout('setting/category.ejs', renderData, 'setting/category_modal.ejs');
  574. } catch (error) {
  575. console.log(error);
  576. ctx.redirect('/dashboard');
  577. }
  578. }
  579. /**
  580. * 新增分类(Ajax)
  581. *
  582. * @param ctx
  583. * @return {Promise<void>}
  584. */
  585. async addCategory(ctx) {
  586. try {
  587. const projectId = ctx.session.sessionProject.id;
  588. const responseData = {
  589. err: 0, msg: '', data: null,
  590. };
  591. const data = JSON.parse(ctx.request.body.data);
  592. if (!data.name) {
  593. throw '提交数据错误';
  594. }
  595. responseData.data = await ctx.service.category.addCategory(projectId, data.name, settingConst.cType.key.dropDown);
  596. ctx.body = responseData;
  597. } catch (err) {
  598. this.log(err);
  599. ctx.body = { err: 1, msg: err.toString(), data: null };
  600. }
  601. }
  602. /**
  603. * 编辑分类(Ajax)
  604. *
  605. * @param ctx
  606. * @return {Promise<void>}
  607. */
  608. async updateCategory(ctx) {
  609. try {
  610. const projectId = ctx.session.sessionProject.id;
  611. const responseData = { err: 0, msg: '', data: null };
  612. const data = JSON.parse(ctx.request.body.data);
  613. if (!data.id) {
  614. throw '提交数据错误';
  615. }
  616. if (data.name) {
  617. const count = await ctx.service.category.count({ pid: projectId, name: data.name });
  618. if (count >= 1) {
  619. throw '存在同名类别';
  620. }
  621. }
  622. const result = await ctx.service.category.update(data, { id: data.id });
  623. if (!result) {
  624. throw '提交数据失败';
  625. }
  626. responseData.data = await ctx.service.category.getCategory(data.id);
  627. ctx.body = responseData;
  628. } catch (err) {
  629. this.log(err);
  630. ctx.body = { err: 1, msg: err.toString(), data: null };
  631. }
  632. }
  633. async setCategoryValue(ctx) {
  634. try {
  635. const responseData = { err: 0, msg: '', data: {} };
  636. const data = JSON.parse(ctx.request.body.data);
  637. if (!data.id) {
  638. throw '提交数据错误';
  639. }
  640. await ctx.service.categoryValue.setCategoryValue(data.id, data.updateValue);
  641. responseData.data.category = await ctx.service.category.getCategory(data.id);
  642. responseData.data.tenders = await ctx.service.tender.getList('', null, 1);
  643. ctx.body = responseData;
  644. } catch (err) {
  645. this.log(err);
  646. ctx.body = { err: 1, msg: err instanceof String ? err : '提交数据失败', data: null };
  647. }
  648. }
  649. /**
  650. * 删除分类(Ajax)
  651. *
  652. * @param ctx
  653. * @return {Promise<void>}
  654. */
  655. async deleteCategory(ctx) {
  656. try {
  657. const projectId = ctx.session.sessionProject.id;
  658. const responseData = {
  659. err: 0, msg: '', data: null,
  660. };
  661. const data = JSON.parse(ctx.request.body.data);
  662. if (!data.id) {
  663. throw '提交数据错误';
  664. }
  665. await ctx.service.category.deleteCategory(projectId, data.id);
  666. ctx.body = responseData;
  667. } catch (err) {
  668. this.log(err);
  669. ctx.body = { err: 1, msg: err.toString(), data: null };
  670. }
  671. }
  672. /**
  673. * 调整分类层次排序(Ajax)
  674. *
  675. * @param ctx
  676. * @return {Promise<void>}
  677. */
  678. async resetCategoryLevel(ctx) {
  679. try {
  680. const projectId = ctx.session.sessionProject.id;
  681. const responseData = {
  682. err: 0, msg: '', data: null,
  683. };
  684. const data = JSON.parse(ctx.request.body.data);
  685. await ctx.service.category.resetCategoryLevel(data);
  686. responseData.data = await ctx.service.category.getAllCategory(projectId);
  687. ctx.body = responseData;
  688. } catch (err) {
  689. this.log(err);
  690. ctx.body = { err: 1, msg: err.toString(), data: null };
  691. }
  692. }
  693. async selfCategoryLevel(ctx) {
  694. try {
  695. const data = JSON.parse(ctx.request.body.data);
  696. await ctx.service.projectAccount.defaultUpdate({id: ctx.session.sessionUser.accountId, self_category_level: data.self_category_level || ''});
  697. ctx.body = {
  698. err: 0, msg: '', data: null,
  699. };
  700. } catch (err) {
  701. ctx.log(err);
  702. ctx.ajaxErrorBody(err, '保存自定义分类失败');
  703. }
  704. }
  705. /** update porject info
  706. * @author wangfeng
  707. * @date 2018-10-12 15:48:05
  708. * @param ctx
  709. * @return {Promise<void>}
  710. */
  711. async updateinfo(ctx) {
  712. try {
  713. const projectId = ctx.params.id;
  714. const responseData = {
  715. err: 0, msg: '', data: null,
  716. };
  717. const conditionData = {
  718. id: projectId,
  719. };
  720. const data = ctx.request.body;
  721. const result = await ctx.service.project.update(data, conditionData);
  722. if (!result) {
  723. throw '提交数据失败';
  724. }
  725. ctx.redirect('/setting/info');
  726. } catch (err) {
  727. this.log(err);
  728. ctx.body = { err: 1, msg: err.toString(), data: null };
  729. }
  730. }
  731. /**
  732. * 检测账户是否存在
  733. *
  734. * @param {Object} ctx -egg全局变量
  735. * @return {void}
  736. */
  737. async accountExist(ctx) {
  738. const projectData = ctx.session.sessionProject;
  739. const responseData = {
  740. err: 0, msg: '', data: null,
  741. };
  742. try {
  743. const data = JSON.parse(ctx.request.body.data);
  744. const account = data.account;
  745. if (projectData.id === undefined) {
  746. throw '不存在对应项目';
  747. }
  748. responseData.data = await ctx.service.projectAccount.isAccountExist(account, projectData.id);
  749. ctx.body = responseData;
  750. } catch (error) {
  751. ctx.body = { err: 1, msg: error.toString(), data: null };
  752. }
  753. }
  754. /**
  755. * 显示设置(Get)
  756. * @param {Object} ctx -egg全局变量
  757. * @return {void}
  758. */
  759. async show(ctx) {
  760. try {
  761. // 获取项目数据
  762. const projectId = ctx.session.sessionProject.id;
  763. await this._checkMenu(projectId);
  764. const projectData = await ctx.service.project.getDataById(projectId);
  765. if (!projectData) {
  766. throw '没有对应的项目数据';
  767. }
  768. if (ctx.session.sessionUser.is_admin === 0) {
  769. throw '没有访问权限';
  770. }
  771. const showList = await ctx.service.settingShow.getList(projectData.page_path);
  772. const sjsRela = await ctx.service.project.getSjsRela(projectId);
  773. const renderData = { projectData, showList, sjsRela };
  774. await this.layout('setting/show.ejs', renderData);
  775. } catch (error) {
  776. this.log(error);
  777. ctx.redirect('/dashboard');
  778. }
  779. }
  780. /**
  781. * 更新显示设置列表
  782. * @param {Object} ctx -egg全局变量
  783. * @return {void}
  784. */
  785. async showListUpdate(ctx) {
  786. try {
  787. // 获取项目id
  788. const projectId = ctx.session.sessionProject.id;
  789. const { data } = ctx.request.body;
  790. const { id } = JSON.parse(data);
  791. const result = await ctx.service.settingShow.setDefaultLabel(id, projectId);
  792. const responseData = { err: 0, msg: '', data: result };
  793. ctx.body = responseData;
  794. } catch (error) {
  795. this.log(error);
  796. ctx.body = { err: 1, msg: error.toString(), data: null };
  797. }
  798. }
  799. async showSjsUpdate(ctx) {
  800. try {
  801. const projectId = ctx.session.sessionProject.id;
  802. const data = JSON.parse(ctx.request.body.data);
  803. const result = await ctx.service.project.updateSjsRela(projectId, data.sub, data.field, data.key, data.value);
  804. ctx.body = { err: 0, msg: '', data: result };
  805. } catch (err) {
  806. this.log(err);
  807. this.ajaxErrorBody(error, '保存数据失败');
  808. }
  809. }
  810. async logs(ctx) {
  811. try {
  812. // 获取项目数据
  813. const projectId = ctx.session.sessionProject.id;
  814. await this._checkMenu(projectId);
  815. const projectData = await ctx.service.project.getDataById(projectId);
  816. if (projectData === null) {
  817. throw '没有对应的项目数据';
  818. }
  819. if (ctx.session.sessionUser.is_admin === 0) {
  820. throw '没有访问权限';
  821. }
  822. const settingType = ctx.params.type ? parseInt(ctx.params.type) : 0;
  823. const logs = await ctx.service.projectLog.getLogs(projectId, settingType);
  824. const renderData = {
  825. projectData,
  826. officeList,
  827. projectLog,
  828. settingType,
  829. logs,
  830. };
  831. await this.layout('setting/logs.ejs', renderData);
  832. } catch (error) {
  833. console.log(error);
  834. ctx.redirect('/dashboard');
  835. }
  836. }
  837. async fun(ctx) {
  838. try {
  839. const projectId = ctx.session.sessionProject.id;
  840. await this._checkMenu(projectId);
  841. const projectData = await ctx.service.project.getDataById(projectId);
  842. const funRela = await ctx.service.project.getFunRela(projectId);
  843. if (projectData === null) {
  844. throw '没有对应的项目数据';
  845. }
  846. if (ctx.session.sessionUser.is_admin === 0) {
  847. throw '没有访问权限';
  848. }
  849. const fun_set = await ctx.service.project.getFunSet(projectData.fun_set);
  850. console.log(fun_set);
  851. await this.layout('setting/fun.ejs', {
  852. projectData,
  853. funRela,
  854. imType,
  855. endMonth: funSet.endMonth,
  856. funSet: fun_set,
  857. });
  858. } catch (error) {
  859. ctx.helper.log(error);
  860. ctx.redirect('/dashboard');
  861. }
  862. }
  863. /**
  864. * 保存功能设置相关
  865. * @param ctx
  866. * @returns {Promise<void>}
  867. */
  868. async updateFun(ctx) {
  869. try {
  870. const projectId = ctx.session.sessionProject.id;
  871. const projectData = await ctx.service.project.getDataById(projectId);
  872. if (projectData === null) {
  873. throw '没有对应的项目数据';
  874. }
  875. if (ctx.session.sessionUser.is_admin === 0) {
  876. throw '没有访问权限';
  877. }
  878. const data = JSON.parse(ctx.request.body.data);
  879. if (data) ctx.request.body = data;
  880. const rule = ctx.service.project.rule('fun');
  881. ctx.validate(rule);
  882. const result = await ctx.service.project.updateFunRela(projectId, ctx.request.body);
  883. if (!result) throw '保存数据失败';
  884. this.ctx.session.sessionProject.page_show.openChangeProject = data.openChangeProject ? 1 : 0;
  885. this.ctx.session.sessionProject.page_show.openChangeApply = data.openChangeApply ? 1 : 0;
  886. this.ctx.session.sessionProject.page_show.openChangePlan = data.openChangePlan ? 1 : 0;
  887. this.ctx.session.sessionProject.page_show.openMaterialTax = data.openMaterialTax ? 1 : 0;
  888. this.ctx.session.sessionProject.page_show.openMaterialChecklist = data.openMaterialChecklist ? 1 : 0;
  889. this.ctx.session.sessionProject.page_show.openMaterialSelf = data.openMaterialSelf ? 1 : 0;
  890. this.ctx.session.sessionProject.page_show.openMaterialEditForAudit = data.openMaterialEditForAudit ? 1 : 0;
  891. this.ctx.session.sessionProject.page_show.openStageStart = data.openStageStart ? 1 : 0;
  892. const result2 = await ctx.service.project.updatePageshow(projectId);
  893. if (!result2) throw '保存数据失败';
  894. if (data.addFunSet) {
  895. const funSet = projectData.fun_set ? JSON.parse(projectData.fun_set) : {};
  896. ctx.helper._.defaultsDeep(data.addFunSet, funSet);
  897. const result3 = await ctx.service.project.updateFunSet(projectId, data.addFunSet);
  898. if (!result3) throw '保存数据失败';
  899. }
  900. ctx.body = { err: 0, msg: '', data: null };
  901. } catch (error) {
  902. ctx.helper.log(error);
  903. this.ajaxErrorBody(error, '保存数据失败');
  904. }
  905. }
  906. async s2b (ctx) {
  907. try {
  908. const projectId = ctx.session.sessionProject.id;
  909. await this._checkMenu(projectId);
  910. const projectData = await ctx.service.project.getDataById(projectId);
  911. if (projectData === null) throw '没有对应的项目数据';
  912. if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
  913. const tenders = await ctx.service.tender.getAllDataByCondition({ where: { project_id: projectId } });
  914. for (const t of tenders) {
  915. t.measure_type_str = t.measure_type === measureType.tz.value ? measureType.tz.title : measureType.gcl.title;
  916. const stages = await ctx.service.stage.getAllDataByCondition({ where: { tid: t.id } });
  917. t.stage_count_str = `第${stages.length || 0}期`;
  918. const user = await ctx.service.projectAccount.getAccountInfoById(t.user_id);
  919. t.user_str = user.name + '-' + user.company;
  920. t.show_time = stages.length > 0 ? stages[stages.length - 1].cache_time_r || stages[stages.length - 1].in_time : t.create_time;
  921. }
  922. await this.layout('setting/s2b.ejs', {
  923. projectData,
  924. tenders,
  925. })
  926. } catch (error) {
  927. ctx.helper.log(error);
  928. ctx.redirect('/dashboard');
  929. }
  930. }
  931. async s2bUpdate(ctx) {
  932. try {
  933. const projectId = ctx.session.sessionProject.id;
  934. const projectData = await ctx.service.project.getDataById(projectId);
  935. if (projectData === null) throw '没有对应的项目数据';
  936. if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
  937. const data = JSON.parse(ctx.request.body.data);
  938. if (!data.tid) throw '提交数据错误';
  939. const updateData = {};
  940. if (data.gxby_limit !== undefined) updateData.s2b_gxby_limit = data.gxby_limit;
  941. if (data.gxby_check !== undefined) updateData.s2b_gxby_check = data.gxby_check;
  942. if (data.dagl_limit !== undefined) updateData.s2b_dagl_limit = data.dagl_limit;
  943. if (data.dagl_check !== undefined) updateData.s2b_dagl_check = data.dagl_check;
  944. await ctx.service.tender.saveApiRela(data.tid, updateData);
  945. ctx.body = { err: 0, msg: '', data: null };
  946. } catch (error) {
  947. ctx.helper.log(error);
  948. this.ajaxErrorBody(error, '保存数据失败');
  949. }
  950. }
  951. async s2bUpdateStatus(ctx) {
  952. try {
  953. const projectId = ctx.session.sessionProject.id;
  954. const projectData = await ctx.service.project.getDataById(projectId);
  955. if (projectData === null) throw '没有对应的项目数据';
  956. if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
  957. const data = JSON.parse(ctx.request.body.data);
  958. if (!data.type || data.status === undefined) throw '提交数据错误';
  959. const responseData = { err: 0, msg: '', data: null };
  960. switch (data.type) {
  961. case 'gxby':
  962. responseData.data = await this.ctx.service.s2bProj.updateGxbyStatus(projectId, data.status, data.limit, data.ratio);
  963. break;
  964. case 'dagl':
  965. responseData.data = await this.ctx.service.s2bProj.updateDaglStatus(projectId, data.status, data.limit, data.ratio);
  966. break;
  967. default: throw '提交数据错误';
  968. }
  969. ctx.body = responseData;
  970. } catch (error) {
  971. console.log(error);
  972. ctx.helper.log(error);
  973. this.ajaxErrorBody(error, '保存数据失败');
  974. }
  975. }
  976. async dataCollect(ctx) {
  977. try {
  978. const projectId = ctx.session.sessionProject.id;
  979. await this._checkMenu(projectId);
  980. const projectData = await ctx.service.project.getDataById(projectId);
  981. if (projectData === null) throw '没有对应的项目数据';
  982. projectData.data_collect_pages = projectData.data_collect_pages ? projectData.data_collect_pages.split(',') : [];
  983. if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
  984. const dataCollectAudits = await ctx.service.datacollectAudit.getList(projectId);
  985. // 获取所有项目参与者
  986. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  987. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  988. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  989. });
  990. const accountGroupList = accountGroup.map((item, idx) => {
  991. const groupList = accountList.filter(item => item.account_group === idx);
  992. return { groupName: item, groupList };
  993. });
  994. const categoryData = await ctx.service.category.getAllCategory(ctx.session.sessionProject.id);
  995. const tenders = await ctx.service.tender.getList('', null, 1);
  996. const dcTenders = await ctx.service.datacollectTender.getList(projectId);
  997. await this.layout('setting/datacollect.ejs', {
  998. projectData,
  999. dataCollectAudits,
  1000. accountList,
  1001. accountGroup: accountGroupList,
  1002. categoryData,
  1003. tenders,
  1004. dcTenders,
  1005. }, 'setting/datacollect_modal.ejs');
  1006. } catch (error) {
  1007. ctx.helper.log(error);
  1008. ctx.redirect('/dashboard');
  1009. }
  1010. }
  1011. async dataCollectSave(ctx) {
  1012. try {
  1013. if (ctx.session.sessionUser.is_admin === 0) throw '没有设置权限';
  1014. const projectId = ctx.session.sessionProject.id;
  1015. const responseData = {
  1016. err: 0, msg: '', data: null,
  1017. };
  1018. const data = JSON.parse(ctx.request.body.data);
  1019. if (!data.type) {
  1020. throw '提交数据错误';
  1021. }
  1022. switch (data.type) {
  1023. case 'show':
  1024. responseData.data = await ctx.service.project.update({ data_collect: data.data_collect, data_collect_pages: data.data_collect_pages.join(',') }, { id: projectId });
  1025. ctx.session.sessionProject.dataCollect = data.data_collect;
  1026. ctx.session.sessionProject.showDataCollect = data.data_collect ? 1 : 0;
  1027. break;
  1028. case 'add-audit':
  1029. // 判断该用户的组是否已加入到表中,已加入则提示无需添加
  1030. const auditInfo = await ctx.service.datacollectAudit.getDataByCondition({ pid: projectId, uid: data.id });
  1031. if (auditInfo) {
  1032. throw '该用户已存在权限中,无需重复添加';
  1033. }
  1034. const accountInfo = await ctx.service.projectAccount.getDataById(data.id);
  1035. const groupInfo = await ctx.service.datacollectAudit.getGroupInfo(projectId, accountInfo.account_group);
  1036. if (groupInfo) {
  1037. throw '该用户所在单位已存在权限中,无需单独添加';
  1038. }
  1039. await ctx.service.datacollectAudit.saveAudit(projectId, accountInfo.account_group, data.id);
  1040. responseData.data = await ctx.service.datacollectAudit.getList(projectId);
  1041. break;
  1042. case 'add-group':
  1043. const groupInfo2 = await ctx.service.datacollectAudit.getGroupInfo(projectId, data.id);
  1044. if (groupInfo2) {
  1045. throw '该单位已存在权限中,无需重复添加';
  1046. }
  1047. await ctx.service.datacollectAudit.saveGroup(projectId, data.id);
  1048. responseData.data = await ctx.service.datacollectAudit.getList(projectId);
  1049. break;
  1050. case 'del-audit':
  1051. const auditInfo2 = await ctx.service.datacollectAudit.getDataById(data.id);
  1052. if (!auditInfo2) {
  1053. throw '该用户已不存在权限中,移除失败';
  1054. }
  1055. await ctx.service.datacollectAudit.delAudit(data.id);
  1056. responseData.data = await ctx.service.datacollectAudit.getList(projectId);
  1057. break;
  1058. case 'del-group':
  1059. const groupInfo3 = await ctx.service.datacollectAudit.getDataById(data.id);
  1060. if (!groupInfo3) {
  1061. throw '该单位已不存在权限中,移除失败';
  1062. }
  1063. await ctx.service.datacollectAudit.delAudit(data.id);
  1064. responseData.data = await ctx.service.datacollectAudit.getList(projectId);
  1065. break;
  1066. case 'tender':
  1067. if (ctx.session.sessionProject.page_show.addDataCollect !== parseInt(data.add_datacollect)) {
  1068. ctx.session.sessionProject.page_show.addDataCollect = parseInt(data.add_datacollect);
  1069. await ctx.service.project.updatePageshow(projectId);
  1070. }
  1071. await ctx.service.datacollectTender.updateList(projectId, data.tids);
  1072. responseData.data = {
  1073. dcTenders: await ctx.service.datacollectTender.getList(projectId),
  1074. addDatacollect: ctx.session.sessionProject.page_show.addDataCollect,
  1075. };
  1076. break;
  1077. default: throw '参数有误';
  1078. }
  1079. ctx.body = responseData;
  1080. } catch (err) {
  1081. this.log(err);
  1082. ctx.body = { err: 1, msg: err.toString(), data: null };
  1083. }
  1084. }
  1085. }
  1086. return SettingController;
  1087. };