setting_controller.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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 spread(ctx) {
  1034. try {
  1035. // 获取项目数据
  1036. const projectId = ctx.session.sessionProject.id;
  1037. await this._checkMenu(projectId);
  1038. const projectData = await ctx.service.project.getDataById(projectId);
  1039. if (!projectData) throw '没有对应的项目数据';
  1040. const sType = ctx.query.stype;
  1041. const code = ctx.query.code;
  1042. const projectSpread = await this.ctx.service.projectSpread.getProjectSpread(projectData.id, code);
  1043. const renderData = {
  1044. BaseSetCol: SpreadConst.BaseSetCol[sType],
  1045. sType,
  1046. code,
  1047. colSet: projectSpread[sType],
  1048. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.setting.spread),
  1049. };
  1050. await this.layout('setting/spread.ejs', renderData, 'setting/spread_modal.ejs');
  1051. } catch (error) {
  1052. this.log(error);
  1053. ctx.redirect('/dashboard');
  1054. }
  1055. }
  1056. async saveSpread(ctx) {
  1057. try {
  1058. const projectId = ctx.session.sessionProject.id;
  1059. const data = JSON.parse(ctx.request.body.data);
  1060. await this.ctx.service.projectSpread.updateProjectSet(projectId, data.code, data.sType, data.colSet);
  1061. const projectSpread = await this.ctx.service.projectSpread.getProjectSpread(projectId, data.code);
  1062. ctx.body = { err: 0, msg: '', data: { colSet: projectSpread[data.sType] } };
  1063. } catch (error) {
  1064. ctx.log(error);
  1065. ctx.ajaxErrorBody(error, '保存列设置失败,请刷新后重试');
  1066. }
  1067. }
  1068. async previewSpread(ctx) {
  1069. try {
  1070. const data = JSON.parse(ctx.request.body.data);
  1071. const [billsSpread, posSpread] = this.ctx.service.projectSpread.generateRelaSpread(data.sType, data.colSet);
  1072. ctx.body = { err: 0, msg: '', data: { billsSpread, posSpread } };
  1073. } catch (error) {
  1074. ctx.log(error);
  1075. ctx.ajaxErrorBody(error, '保存列设置失败,请刷新后重试');
  1076. }
  1077. }
  1078. async resetSpread(ctx) {
  1079. try {
  1080. const projectId = ctx.session.sessionProject.id;
  1081. const data = JSON.parse(ctx.request.body.data);
  1082. const spreadTemplate = SpreadConst.ProjectSpreadTemplate.find(x => { return x.code === data.code; });
  1083. const colSet = spreadTemplate.template[data.sType];
  1084. await this.ctx.service.projectSpread.updateProjectSet(projectId, data.code, data.sType, colSet);
  1085. const projectSpread = await this.ctx.service.projectSpread.getProjectSpread(projectId, data.code);
  1086. ctx.body = { err: 0, msg: '', data: { colSet: projectSpread[data.sType] } };
  1087. } catch (error) {
  1088. ctx.log(error);
  1089. ctx.ajaxErrorBody(error, '保存列设置失败,请刷新后重试');
  1090. }
  1091. }
  1092. }
  1093. return SettingController;
  1094. };