setting_controller.js 55 KB

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