setting_controller.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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', 'company_id', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'bind', 'account_group', 'permission', 'cooperation', 'notice_again', 'unit_sign_path'];
  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. fujianOssPath: ctx.app.config.fujianOssPath,
  250. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.setting.user),
  251. // rule: JSON.stringify(frontRule),
  252. };
  253. await this.layout('setting/user_permission.ejs', renderData, 'setting/user_permission_modal.ejs');
  254. } catch (error) {
  255. console.log(error);
  256. ctx.redirect('/dashboard');
  257. }
  258. }
  259. /**
  260. * 项目权限 -- 参建单位(Get)
  261. * @param ctx
  262. * @return {Promise<void>}
  263. */
  264. async userUnit(ctx) {
  265. try {
  266. // 获取项目数据
  267. const projectId = ctx.session.sessionProject.id;
  268. const projectData = await ctx.service.project.getDataById(projectId);
  269. if (projectData === null) {
  270. throw '没有对应的项目数据';
  271. }
  272. if (ctx.session.sessionUser.is_admin === 0) {
  273. throw '没有访问权限';
  274. }
  275. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({where: {pid: projectId}});
  276. for (const u of unitList) {
  277. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  278. where: {
  279. project_id: projectId,
  280. company: u.name
  281. }
  282. });
  283. u.account_num = accountList.length;
  284. }
  285. // 获取账号个数
  286. const user_total = await ctx.service.projectAccount.count({project_id: projectId});
  287. const renderData = {
  288. projectData,
  289. accountGroup,
  290. unitList,
  291. user_total,
  292. company: null,
  293. fujianOssPath: ctx.app.config.fujianOssPath,
  294. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.setting.user),
  295. };
  296. await this.layout('setting/user_unit.ejs', renderData, 'setting/user_unit_modal.ejs');
  297. } catch (error) {
  298. console.log(error);
  299. ctx.redirect('/dashboard');
  300. }
  301. }
  302. /**
  303. * 项目设置 -- 单位新增设置(Post)
  304. * @param ctx
  305. * @return {Promise<void>}
  306. */
  307. async userUnitAdd(ctx) {
  308. const projectData = ctx.session.sessionProject;
  309. try {
  310. // 验证数据
  311. if (projectData.id === undefined) {
  312. throw '不存在对应的项目数据';
  313. }
  314. // 获取验证规则
  315. const rule = ctx.service.constructionUnit.rule('add');
  316. ctx.validate(rule);
  317. ctx.request.body.pid = projectData.id;
  318. const result = await ctx.service.constructionUnit.save(ctx.request.body);
  319. if (!result) {
  320. throw '保存单位失败';
  321. }
  322. this.setMessage('保存单位成功', this.messageType.SUCCESS);
  323. ctx.redirect('/' + ctx.controllerName + '/user/unit');
  324. } catch (error) {
  325. console.log(error);
  326. this.setMessage(error.toString(), this.messageType.ERROR);
  327. ctx.redirect(ctx.request.header.referer);
  328. }
  329. }
  330. /**
  331. * 项目设置 -- 单位编辑(Post)
  332. * @param ctx
  333. * @return {Promise<void>}
  334. */
  335. async userUnitSave(ctx) {
  336. const projectData = ctx.session.sessionProject;
  337. const responseData = {
  338. err: 0, msg: '', data: {},
  339. };
  340. try {
  341. // 验证数据
  342. if (projectData.id === undefined) {
  343. throw '不存在对应的项目数据';
  344. }
  345. const data = JSON.parse(ctx.request.body.data);
  346. switch (data.type) {
  347. case 'update':
  348. const post_data = {
  349. pid: projectData.id,
  350. id: data.id,
  351. };
  352. post_data[data.val_name] = data.val;
  353. if (data.name) post_data.name = data.name;
  354. const result = await ctx.service.constructionUnit.save(post_data);
  355. if (!result) {
  356. throw '保存单位失败';
  357. }
  358. if (post_data.name) {
  359. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  360. where: {
  361. project_id: projectData.id,
  362. company: post_data.name
  363. }
  364. });
  365. responseData.data = {account_num: accountList.length};
  366. }
  367. break;
  368. case 'delete':
  369. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  370. where: {
  371. project_id: projectData.id,
  372. company: data.name
  373. }
  374. });
  375. if (accountList.length > 0) {
  376. throw '该单位下还存在账号,无法删除';
  377. }
  378. await ctx.service.constructionUnit.del(data.id);
  379. break;
  380. case 'del-sign':
  381. const info = await ctx.service.constructionUnit.getDataById(data.id);
  382. let path = null;
  383. if (info.sign_path) {
  384. // 不删除地址,只删除数据库数据,防止已签章的报表丢失
  385. // await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + info.sign_path);
  386. const paths = info.sign_path.split('&%&');
  387. const newPath = paths.filter(item => item !== data.path);
  388. path = newPath.length !== 0 ? newPath.join('&%&') : null;
  389. }
  390. await ctx.service.constructionUnit.update({ sign_path: path }, { id: info.id });
  391. // 用户已选章也要判断,存在也要移除
  392. const userList = await ctx.service.projectAccount.getAllDataByCondition({ where: { company: info.name, project_id: projectData.id } });
  393. if (userList.length > 0) {
  394. for (const user of userList) {
  395. if (user.unit_sign_path && user.unit_sign_path.includes(data.path)) {
  396. const newSignPath = user.unit_sign_path.split('&%&').filter(item => item !== data.path).join('&%&');
  397. await ctx.service.projectAccount.update({ unit_sign_path: newSignPath ? newSignPath : null }, { id: user.id });
  398. }
  399. }
  400. }
  401. responseData.data = path;
  402. break;
  403. case 'import-users':
  404. responseData.data = await ctx.service.projectAccount.addUsers(projectData.id, data.users);
  405. break;
  406. default:
  407. break;
  408. }
  409. ctx.body = responseData;
  410. } catch (err) {
  411. this.log(err);
  412. ctx.body = {err: 1, msg: err.toString(), data: null};
  413. }
  414. }
  415. async userUnitUpload(ctx) {
  416. const responseData = {
  417. err: 0, msg: '', data: null,
  418. };
  419. let stream;
  420. try {
  421. const parts = ctx.multipart({ autoFields: true });
  422. const paths = [];
  423. let index = 0;
  424. while ((stream = await parts()) !== undefined) {
  425. const create_time = Date.parse(new Date()) / 1000;
  426. const fileInfo = path.parse(stream.filename);
  427. const filepath = `app/public/upload/sign/unit/qianzhang_${create_time + index.toString() + fileInfo.ext}`;
  428. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  429. await sendToWormhole(stream);
  430. paths.push(filepath);
  431. ++index;
  432. }
  433. const unit = await ctx.service.constructionUnit.getDataById(parts.field.id);
  434. const sign_path = unit.sign_path ? unit.sign_path.split('&%&') : [];
  435. sign_path.push(...paths);
  436. const result = await ctx.service.constructionUnit.update({ sign_path: sign_path.join('&%&') }, { id: unit.id });
  437. if (result) {
  438. responseData.data = { sign_path: sign_path.join('&%&') };
  439. } else {
  440. throw '添加数据库失败';
  441. }
  442. } catch (err) {
  443. this.log(err);
  444. responseData.err = 1;
  445. responseData.msg = err;
  446. }
  447. ctx.body = responseData;
  448. }
  449. /**
  450. * 项目设置 -- 账号启用和停用设置(Post)
  451. * @param ctx
  452. * @return {Promise<void>}
  453. */
  454. async userSwitch(ctx) {
  455. const responseData = {
  456. err: 0, msg: '', data: null,
  457. };
  458. try {
  459. // 获取项目数据
  460. const projectId = ctx.session.sessionProject.id;
  461. const projectData = await ctx.service.project.getDataById(projectId);
  462. if (projectData === null) {
  463. throw '没有对应的项目数据';
  464. }
  465. if (ctx.session.sessionUser.is_admin === 0) {
  466. throw '没有访问权限';
  467. }
  468. const data = JSON.parse(ctx.request.body.data);
  469. const result = await ctx.service.projectAccount.update(data, {id: data.id});
  470. if (!result) {
  471. throw '提交数据失败';
  472. }
  473. } catch (err) {
  474. this.log(err);
  475. responseData.err = 1;
  476. responseData.msg = err;
  477. }
  478. ctx.body = responseData;
  479. }
  480. /**
  481. * 项目设置 -- 账号添加(Post)
  482. * @param ctx
  483. * @return {Promise<void>}
  484. */
  485. async addUser(ctx) {
  486. const projectData = ctx.session.sessionProject;
  487. try {
  488. // 验证数据
  489. if (projectData.id === undefined) {
  490. throw '不存在对应的项目数据';
  491. }
  492. // 获取验证规则
  493. const rule = ctx.service.projectAccount.rule('add');
  494. ctx.validate(rule);
  495. // 判断新密码的强度
  496. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  497. if (!reg.test(ctx.request.body.password)) {
  498. throw '请设置至少包含数字和字母的密码';
  499. }
  500. ctx.request.body.project_id = projectData.id;
  501. const result = await ctx.service.projectAccount.save(ctx.request.body);
  502. if (!result) {
  503. throw '保存账号数据失败';
  504. }
  505. this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
  506. ctx.redirect('/' + ctx.controllerName + '/user');
  507. } catch (error) {
  508. console.log(error);
  509. ctx.session.postError = error.toString();
  510. this.setMessage(error.toString(), this.messageType.ERROR);
  511. ctx.redirect(ctx.request.header.referer);
  512. }
  513. }
  514. /**
  515. * 项目设置 -- 账号编辑(Post)
  516. * @param ctx
  517. * @return {Promise<void>}
  518. */
  519. async updateUser(ctx) {
  520. const projectData = ctx.session.sessionProject;
  521. try {
  522. // 验证数据
  523. if (projectData.id === undefined) {
  524. throw '不存在对应的项目数据';
  525. }
  526. // 获取验证规则
  527. const rule = ctx.service.projectAccount.rule('modify');
  528. ctx.validate(rule);
  529. const result = await ctx.service.projectAccount.save(ctx.request.body);
  530. if (!result) {
  531. throw '保存账号数据失败';
  532. }
  533. this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
  534. ctx.redirect(ctx.request.header.referer);
  535. } catch (error) {
  536. console.log(error);
  537. this.setMessage(error.toString(), this.messageType.ERROR);
  538. ctx.redirect(ctx.request.header.referer);
  539. }
  540. }
  541. async resetUserPassword(ctx) {
  542. try {
  543. // 获取项目数据
  544. const projectId = ctx.session.sessionProject.id;
  545. const projectData = await ctx.service.project.getDataById(projectId);
  546. if (projectData === null) {
  547. throw '没有对应的项目数据';
  548. }
  549. if (ctx.session.sessionUser.is_admin === 0) {
  550. throw '没有访问权限';
  551. }
  552. const accountId = parseInt(ctx.request.body.id);
  553. let password = ctx.request.body.reset_password;
  554. password = password.toString();
  555. const account = ctx.request.body.account !== undefined ? ctx.request.body.account : '';
  556. if (isNaN(accountId) || accountId <= 0 || password.length < 6) {
  557. throw '参数错误';
  558. }
  559. // 判断新密码的强度
  560. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  561. if (!reg.test(password)) {
  562. throw '请设置至少包含数字和字母的密码';
  563. }
  564. const result = await ctx.service.projectAccount.resetPassword(accountId, password, account);
  565. if (!result) {
  566. throw '重置密码失败!';
  567. }
  568. this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
  569. ctx.redirect(ctx.request.header.referer);
  570. } catch (error) {
  571. console.log(error);
  572. this.setMessage(error.toString(), this.messageType.ERROR);
  573. ctx.redirect(ctx.request.header.referer);
  574. }
  575. }
  576. /**
  577. * 项目设置 -- 账号权限编辑(Post)
  578. * @param ctx
  579. * @return {Promise<void>}
  580. */
  581. async permission(ctx) {
  582. const projectData = ctx.session.sessionProject;
  583. try {
  584. // 验证数据
  585. if (projectData.id === undefined) {
  586. throw '不存在对应的项目数据';
  587. }
  588. const accountId = parseInt(ctx.request.body.id);
  589. const accountInfo = await ctx.service.projectAccount.getDataById(accountId);
  590. if (accountInfo === null) {
  591. throw '不存在该客户';
  592. }
  593. const result = await ctx.service.projectAccount.permissionSave(accountId, ctx.request.body);
  594. if (!result) {
  595. throw '修改权限失败!';
  596. }
  597. this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
  598. ctx.redirect(ctx.request.header.referer);
  599. } catch (error) {
  600. console.log(error);
  601. this.setMessage(error.toString(), this.messageType.ERROR);
  602. ctx.redirect(ctx.request.header.referer);
  603. }
  604. }
  605. // todo 暂时保留,如果支付审批不需要,则取消
  606. /**
  607. * 项目设置 -- 自定义标段分类(Get)
  608. *
  609. * @param ctx
  610. * @return {Promise<void>}
  611. */
  612. async category(ctx) {
  613. try {
  614. // 获取项目数据
  615. const projectId = ctx.session.sessionProject.id;
  616. await this._checkMenu(projectId);
  617. const projectData = await ctx.service.project.getDataById(projectId);
  618. if (projectData === null) {
  619. throw '没有对应的项目数据';
  620. }
  621. if (ctx.session.sessionUser.is_admin === 0) {
  622. throw '没有访问权限';
  623. }
  624. const categoryData = await ctx.service.category.getAllCategory(projectId);
  625. const tenderData = await ctx.service.tender.getList('', null, 1);
  626. const renderData = {
  627. projectData,
  628. categoryType: settingConst.cType,
  629. categoryData,
  630. tenderData,
  631. };
  632. await this.layout('setting/category.ejs', renderData, 'setting/category_modal.ejs');
  633. } catch (error) {
  634. console.log(error);
  635. ctx.redirect('/dashboard');
  636. }
  637. }
  638. /**
  639. * 新增分类(Ajax)
  640. *
  641. * @param ctx
  642. * @return {Promise<void>}
  643. */
  644. async addCategory(ctx) {
  645. try {
  646. const projectId = ctx.session.sessionProject.id;
  647. const responseData = {
  648. err: 0, msg: '', data: null,
  649. };
  650. const data = JSON.parse(ctx.request.body.data);
  651. if (!data.name) {
  652. throw '提交数据错误';
  653. }
  654. responseData.data = await ctx.service.category.addCategory(projectId, data.name, settingConst.cType.key.dropDown);
  655. ctx.body = responseData;
  656. } catch (err) {
  657. this.log(err);
  658. ctx.body = {err: 1, msg: err.toString(), data: null};
  659. }
  660. }
  661. /**
  662. * 编辑分类(Ajax)
  663. *
  664. * @param ctx
  665. * @return {Promise<void>}
  666. */
  667. async updateCategory(ctx) {
  668. try {
  669. const projectId = ctx.session.sessionProject.id;
  670. const responseData = {err: 0, msg: '', data: null};
  671. const data = JSON.parse(ctx.request.body.data);
  672. if (!data.id) {
  673. throw '提交数据错误';
  674. }
  675. if (data.name) {
  676. const count = await ctx.service.category.count({pid: projectId, name: data.name});
  677. if (count >= 1) {
  678. throw '存在同名类别';
  679. }
  680. }
  681. const result = await ctx.service.category.update(data, {id: data.id});
  682. if (!result) {
  683. throw '提交数据失败';
  684. }
  685. responseData.data = await ctx.service.category.getCategory(data.id);
  686. ctx.body = responseData;
  687. } catch (err) {
  688. this.log(err);
  689. ctx.body = {err: 1, msg: err.toString(), data: null};
  690. }
  691. }
  692. async setCategoryValue(ctx) {
  693. try {
  694. const responseData = {err: 0, msg: '', data: {}};
  695. const data = JSON.parse(ctx.request.body.data);
  696. if (!data.id) {
  697. throw '提交数据错误';
  698. }
  699. await ctx.service.categoryValue.setCategoryValue(data.id, data.updateValue);
  700. responseData.data.category = await ctx.service.category.getCategory(data.id);
  701. responseData.data.tenders = await ctx.service.tender.getList('', null, 1);
  702. ctx.body = responseData;
  703. } catch (err) {
  704. this.log(err);
  705. ctx.body = {err: 1, msg: err instanceof String ? err : '提交数据失败', data: null};
  706. }
  707. }
  708. /**
  709. * 删除分类(Ajax)
  710. *
  711. * @param ctx
  712. * @return {Promise<void>}
  713. */
  714. async deleteCategory(ctx) {
  715. try {
  716. const projectId = ctx.session.sessionProject.id;
  717. const responseData = {
  718. err: 0, msg: '', data: null,
  719. };
  720. const data = JSON.parse(ctx.request.body.data);
  721. if (!data.id) {
  722. throw '提交数据错误';
  723. }
  724. await ctx.service.category.deleteCategory(projectId, data.id);
  725. ctx.body = responseData;
  726. } catch (err) {
  727. this.log(err);
  728. ctx.body = {err: 1, msg: err.toString(), data: null};
  729. }
  730. }
  731. /**
  732. * 调整分类层次排序(Ajax)
  733. *
  734. * @param ctx
  735. * @return {Promise<void>}
  736. */
  737. async resetCategoryLevel(ctx) {
  738. try {
  739. const projectId = ctx.session.sessionProject.id;
  740. const responseData = {
  741. err: 0, msg: '', data: null,
  742. };
  743. const data = JSON.parse(ctx.request.body.data);
  744. await ctx.service.category.resetCategoryLevel(data);
  745. responseData.data = await ctx.service.category.getAllCategory(projectId);
  746. ctx.body = responseData;
  747. } catch (err) {
  748. this.log(err);
  749. ctx.body = {err: 1, msg: err.toString(), data: null};
  750. }
  751. }
  752. async selfCategoryLevel(ctx) {
  753. try {
  754. const data = JSON.parse(ctx.request.body.data);
  755. await ctx.service.projectAccount.defaultUpdate({
  756. id: ctx.session.sessionUser.accountId,
  757. self_category_level: data.self_category_level || ''
  758. });
  759. ctx.body = {
  760. err: 0, msg: '', data: null,
  761. };
  762. } catch (err) {
  763. ctx.log(err);
  764. ctx.ajaxErrorBody(err, '保存自定义分类失败');
  765. }
  766. }
  767. /** update porject info
  768. * @author wangfeng
  769. * @date 2018-10-12 15:48:05
  770. * @param ctx
  771. * @return {Promise<void>}
  772. */
  773. async updateinfo(ctx) {
  774. try {
  775. const projectId = ctx.params.id;
  776. const responseData = {
  777. err: 0, msg: '', data: null,
  778. };
  779. const conditionData = {
  780. id: projectId,
  781. };
  782. const data = ctx.request.body;
  783. const result = await ctx.service.project.update(data, conditionData);
  784. if (!result) {
  785. throw '提交数据失败';
  786. }
  787. ctx.redirect('/setting/info');
  788. } catch (err) {
  789. this.log(err);
  790. ctx.body = {err: 1, msg: err.toString(), data: null};
  791. }
  792. }
  793. async updateProjectSet(ctx) {
  794. try {
  795. const projectId = ctx.session.sessionProject.id;
  796. const data = JSON.parse(ctx.request.body.data);
  797. const allowField = ['notice_setting']; // 暂时只一个
  798. if (!ctx.helper._.includes(allowField, data.field)) {
  799. throw '非配置的字段无法使用本方法';
  800. }
  801. const result = await ctx.service.project.updateProjectSet(projectId, data.field, data.pData);
  802. if (!result) {
  803. throw '提交数据失败';
  804. }
  805. ctx.body = { err: 0, msg: '', data: result };
  806. } catch (err) {
  807. this.log(err);
  808. ctx.body = { err: 1, msg: err.toString(), data: null };
  809. }
  810. }
  811. /**
  812. * 检测账户是否存在
  813. *
  814. * @param {Object} ctx -egg全局变量
  815. * @return {void}
  816. */
  817. async accountExist(ctx) {
  818. const projectData = ctx.session.sessionProject;
  819. const responseData = {
  820. err: 0, msg: '', data: null,
  821. };
  822. try {
  823. const data = JSON.parse(ctx.request.body.data);
  824. const account = data.account;
  825. if (projectData.id === undefined) {
  826. throw '不存在对应项目';
  827. }
  828. responseData.data = await ctx.service.projectAccount.isAccountExist(account, projectData.id);
  829. ctx.body = responseData;
  830. } catch (error) {
  831. ctx.body = {err: 1, msg: error.toString(), data: null};
  832. }
  833. }
  834. /**
  835. * 显示设置(Get)
  836. * @param {Object} ctx -egg全局变量
  837. * @return {void}
  838. */
  839. async show(ctx) {
  840. try {
  841. // 获取项目数据
  842. const projectId = ctx.session.sessionProject.id;
  843. await this._checkMenu(projectId);
  844. const projectData = await ctx.service.project.getDataById(projectId);
  845. if (!projectData) {
  846. throw '没有对应的项目数据';
  847. }
  848. if (ctx.session.sessionUser.is_admin === 0) {
  849. throw '没有访问权限';
  850. }
  851. const showList = await ctx.service.settingShow.getList(projectData.page_path);
  852. const sjsRela = await ctx.service.project.getSjsRela(projectId);
  853. const projectSpread = await ctx.service.projectSpread.getProjectSpreadType(projectId);
  854. const renderData = {projectData, showList, sjsRela, settingConst, projectSpread};
  855. await this.layout('setting/show.ejs', renderData);
  856. } catch (error) {
  857. this.log(error);
  858. ctx.redirect('/dashboard');
  859. }
  860. }
  861. /**
  862. * 更新显示设置列表
  863. * @param {Object} ctx -egg全局变量
  864. * @return {void}
  865. */
  866. async showListUpdate(ctx) {
  867. try {
  868. // 获取项目id
  869. const projectId = ctx.session.sessionProject.id;
  870. const {data} = ctx.request.body;
  871. const {id} = JSON.parse(data);
  872. const result = await ctx.service.settingShow.setDefaultLabel(id, projectId);
  873. const responseData = {err: 0, msg: '', data: result};
  874. ctx.body = responseData;
  875. } catch (error) {
  876. this.log(error);
  877. ctx.body = {err: 1, msg: error.toString(), data: null};
  878. }
  879. }
  880. async showSjsUpdate(ctx) {
  881. try {
  882. const projectId = ctx.session.sessionProject.id;
  883. const data = JSON.parse(ctx.request.body.data);
  884. const result = await ctx.service.project.updateSjsRela(projectId, data.sub, data.field, data.key, data.value);
  885. ctx.body = {err: 0, msg: '', data: result};
  886. } catch (err) {
  887. this.log(err);
  888. this.ajaxErrorBody(err, '保存数据失败');
  889. }
  890. }
  891. async s2b(ctx) {
  892. try {
  893. const projectId = ctx.session.sessionProject.id;
  894. await this._checkMenu(projectId);
  895. const projectData = await ctx.service.project.getDataById(projectId);
  896. if (projectData === null) throw '没有对应的项目数据';
  897. if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
  898. const limitList = await ctx.service.multiLimit.getLimits(ctx.session.sessionProject.id);
  899. const tenders = await ctx.service.tender.getAllDataByCondition({where: {project_id: projectId}});
  900. for (const t of tenders) {
  901. t.measure_type_str = t.measure_type === measureType.tz.value ? measureType.tz.title : measureType.gcl.title;
  902. const stages = await ctx.service.stage.getAllDataByCondition({where: {tid: t.id}});
  903. t.stage_count_str = `第${stages.length || 0}期`;
  904. const user = await ctx.service.projectAccount.getAccountInfoById(t.user_id);
  905. t.user_str = user.name + '-' + user.company;
  906. t.show_time = stages.length > 0 ? stages[stages.length - 1].cache_time_r || stages[stages.length - 1].in_time : t.create_time;
  907. }
  908. await this.layout('setting/s2b.ejs', {
  909. projectData,
  910. tenders,
  911. limitList,
  912. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.setting.s2b),
  913. }, 'setting/s2b_modal.ejs');
  914. } catch (error) {
  915. ctx.helper.log(error);
  916. ctx.redirect('/dashboard');
  917. }
  918. }
  919. async s2bUpdate(ctx) {
  920. try {
  921. const projectId = ctx.session.sessionProject.id;
  922. const projectData = await ctx.service.project.getDataById(projectId);
  923. if (projectData === null) throw '没有对应的项目数据';
  924. if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
  925. const data = JSON.parse(ctx.request.body.data);
  926. if (!data.tid) throw '提交数据错误';
  927. const updateData = {};
  928. if (data.gxby_limit !== undefined) updateData.s2b_gxby_limit = data.gxby_limit;
  929. if (data.gxby_check !== undefined) updateData.s2b_gxby_check = data.gxby_check;
  930. if (data.dagl_limit !== undefined) updateData.s2b_dagl_limit = data.dagl_limit;
  931. if (data.dagl_check !== undefined) updateData.s2b_dagl_check = data.dagl_check;
  932. if (data.multi_limit !== undefined) updateData.s2b_multi_limit = data.multi_limit;
  933. if (data.multi_check !== undefined) updateData.s2b_multi_check = data.multi_check;
  934. await ctx.service.tender.saveApiRela(data.tid, updateData);
  935. ctx.body = {err: 0, msg: '', data: null};
  936. } catch (error) {
  937. ctx.helper.log(error);
  938. this.ajaxErrorBody(error, '保存数据失败');
  939. }
  940. }
  941. async s2bUpdateStatus(ctx) {
  942. try {
  943. const projectId = ctx.session.sessionProject.id;
  944. const projectData = await ctx.service.project.getDataById(projectId);
  945. if (projectData === null) throw '没有对应的项目数据';
  946. if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
  947. const data = JSON.parse(ctx.request.body.data);
  948. if (!data.type || data.status === undefined) throw '提交数据错误';
  949. const responseData = {err: 0, msg: '', data: null};
  950. switch (data.type) {
  951. case 'gxby':
  952. responseData.data = await this.ctx.service.s2bProj.updateGxbyStatus(projectId, data.status, data.limit, data.ratio);
  953. break;
  954. case 'dagl':
  955. responseData.data = await this.ctx.service.s2bProj.updateDaglStatus(projectId, data.status, data.limit, data.ratio);
  956. break;
  957. default:
  958. throw '提交数据错误';
  959. }
  960. ctx.body = responseData;
  961. } catch (error) {
  962. console.log(error);
  963. ctx.helper.log(error);
  964. this.ajaxErrorBody(error, '保存数据失败');
  965. }
  966. }
  967. async saveLimit(ctx) {
  968. try {
  969. const data = JSON.parse(ctx.request.body.data);
  970. const result = await ctx.service.multiLimit.saveLimit(data);
  971. ctx.body = { err: 0, msg: '', data: result };
  972. } catch (err) {
  973. ctx.log(err);
  974. ctx.ajaxErrorBody(err, '修改数据失败');
  975. }
  976. }
  977. async saveLimitOption(ctx) {
  978. try {
  979. const data = JSON.parse(ctx.request.body.data);
  980. const result = await ctx.service.multiLimit.saveLimitOption(data);
  981. ctx.body = { err: 0, msg: '', data: result };
  982. } catch (err) {
  983. ctx.log(err);
  984. ctx.ajaxErrorBody(err, '修改数据失败');
  985. }
  986. }
  987. async tender(ctx) {
  988. try {
  989. const projectId = ctx.session.sessionProject.id;
  990. await this._checkMenu(projectId);
  991. const projectData = await ctx.service.project.getDataById(projectId);
  992. if (projectData === null) throw '没有对应的项目数据';
  993. if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
  994. const limits = await this.ctx.service.multiLimit.getLimitList(this.ctx.session.sessionProject.id);
  995. const renderData = {
  996. projectData,
  997. tender: ctx.tender.data,
  998. limits,
  999. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.setting.tender),
  1000. };
  1001. await this.layout('setting/tender.ejs', renderData);
  1002. } catch (err) {
  1003. ctx.log(err);
  1004. }
  1005. }
  1006. async tenderLoad(ctx) {
  1007. try {
  1008. const data = JSON.parse(ctx.request.body.data);
  1009. if (!data.filter) throw '请求参数错误';
  1010. const filter = data.filter.split(';');
  1011. const result = {};
  1012. for (const f of filter) {
  1013. switch (f) {
  1014. case 'xmj':
  1015. result[f] = await this.ctx.service.ledger.getAllDataByCondition({
  1016. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf', 'code', 'name', 'unit'],
  1017. where: { tender_id: ctx.tender.id },
  1018. });
  1019. break;
  1020. case 'limit':
  1021. result[f] = await this.ctx.service.ledgerExtra.getAllDataByCondition({
  1022. columns: ['id', 'multi_limit'],
  1023. where: { tid: ctx.tender.id },
  1024. });
  1025. break;
  1026. default:
  1027. throw '未知数据类型';
  1028. }
  1029. }
  1030. ctx.body = { err: 0, msg: '', data: result };
  1031. } catch (err) {
  1032. ctx.log(err);
  1033. ctx.ajaxErrorBody(err, '加载数据错误');
  1034. }
  1035. }
  1036. async tenderUpdate(ctx) {
  1037. try {
  1038. const data = JSON.parse(ctx.request.body.data);
  1039. await this.ctx.service.ledgerExtra.updateMultiLimit(data.id, data.multi_limit);
  1040. ctx.body = {err: 0, msg: '', data };
  1041. } catch (err) {
  1042. ctx.log(err);
  1043. ctx.ajaxErrorBody(err, '保存数据错误');
  1044. }
  1045. }
  1046. async spread(ctx) {
  1047. try {
  1048. // 获取项目数据
  1049. const projectId = ctx.session.sessionProject.id;
  1050. await this._checkMenu(projectId);
  1051. const projectData = await ctx.service.project.getDataById(projectId);
  1052. if (!projectData) throw '没有对应的项目数据';
  1053. const sType = ctx.query.stype;
  1054. const code = ctx.query.code;
  1055. const projectSpread = await this.ctx.service.projectSpread.getProjectSpread(projectData.id, code);
  1056. const renderData = {
  1057. BaseSetCol: SpreadConst.BaseSetCol[sType],
  1058. sType,
  1059. code,
  1060. colSet: projectSpread[sType],
  1061. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.setting.spread),
  1062. };
  1063. await this.layout('setting/spread.ejs', renderData, 'setting/spread_modal.ejs');
  1064. } catch (error) {
  1065. this.log(error);
  1066. ctx.redirect('/dashboard');
  1067. }
  1068. }
  1069. async saveSpread(ctx) {
  1070. try {
  1071. const projectId = ctx.session.sessionProject.id;
  1072. const data = JSON.parse(ctx.request.body.data);
  1073. await this.ctx.service.projectSpread.updateProjectSet(projectId, data.code, data.sType, data.colSet);
  1074. const projectSpread = await this.ctx.service.projectSpread.getProjectSpread(projectId, data.code);
  1075. ctx.body = { err: 0, msg: '', data: { colSet: projectSpread[data.sType] } };
  1076. } catch (error) {
  1077. ctx.log(error);
  1078. ctx.ajaxErrorBody(error, '保存列设置失败,请刷新后重试');
  1079. }
  1080. }
  1081. async previewSpread(ctx) {
  1082. try {
  1083. const data = JSON.parse(ctx.request.body.data);
  1084. const [billsSpread, posSpread] = this.ctx.service.projectSpread.generateRelaSpread(data.sType, data.colSet);
  1085. if (billsSpread.frozenField) billsSpread.frozenColCount = billsSpread.cols.findIndex(x => {return x.field === billsSpread.frozenField; }) + 1;
  1086. if (posSpread.frozenField) posSpread.frozenColCount = posSpread.cols.findIndex(x => {return x.field === posSpread.frozenField; }) + 1;
  1087. ctx.body = { err: 0, msg: '', data: { billsSpread, posSpread } };
  1088. } catch (error) {
  1089. ctx.log(error);
  1090. ctx.ajaxErrorBody(error, '保存列设置失败,请刷新后重试');
  1091. }
  1092. }
  1093. async resetSpread(ctx) {
  1094. try {
  1095. const projectId = ctx.session.sessionProject.id;
  1096. const data = JSON.parse(ctx.request.body.data);
  1097. const spreadTemplate = SpreadConst.ProjectSpreadTemplate.find(x => { return x.code === data.code; });
  1098. const colSet = spreadTemplate.template[data.sType];
  1099. await this.ctx.service.projectSpread.updateProjectSet(projectId, data.code, data.sType, colSet);
  1100. const projectSpread = await this.ctx.service.projectSpread.getProjectSpread(projectId, data.code);
  1101. ctx.body = { err: 0, msg: '', data: { colSet: projectSpread[data.sType] } };
  1102. } catch (error) {
  1103. ctx.log(error);
  1104. ctx.ajaxErrorBody(error, '保存列设置失败,请刷新后重试');
  1105. }
  1106. }
  1107. }
  1108. return SettingController;
  1109. };