setting_controller.js 49 KB

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