setting_controller.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. module.exports = app => {
  19. class SettingController extends app.BaseController {
  20. /**
  21. * 构造函数
  22. *
  23. * @param {Object} ctx - egg全局context
  24. * @return {void}
  25. */
  26. constructor(ctx) {
  27. super(ctx);
  28. ctx.subMenu = settingMenu;
  29. }
  30. async _checkMenu(pid) {
  31. const s2bData = new S2b(this.ctx);
  32. const s2bProj = await s2bData.getS2bProj(pid);
  33. // this.ctx.subMenu.s2b.display = !!s2bProj && (!!s2bProj.gxby || !!s2bProj.dagl);
  34. this.ctx.subMenu.s2b.display = false;
  35. return s2bProj;
  36. }
  37. /**
  38. * 项目信息页面(Get)
  39. *
  40. * @param {Object} ctx - egg全局变量
  41. * @return {void}
  42. */
  43. async info(ctx) {
  44. try {
  45. // 获取项目数据
  46. const projectId = ctx.session.sessionProject.id;
  47. await this._checkMenu(projectId);
  48. const projectData = await ctx.service.project.getDataById(projectId);
  49. if (projectData === null) {
  50. throw '没有对应的项目数据';
  51. }
  52. // 获取销售人员数据
  53. const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id);
  54. // 数据规则
  55. const rule = ctx.service.project.rule('saveInfo');
  56. const jsValidator = await this.jsValidator.convert(rule).build();
  57. const officeName = officeList[salesmanData.office];
  58. const date = new Date(projectData.create_time * 1000);// 如果date为10位不需要乘1000
  59. const Y = date.getFullYear() + '-';
  60. const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  61. const D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
  62. const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  63. const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  64. const s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  65. const dateStr = Y + M + D + h + m + s;
  66. const renderData = {
  67. projectData,
  68. salesmanData,
  69. officeName,
  70. officeList,
  71. jsValidator,
  72. dateStr,
  73. };
  74. await this.layout('setting/info.ejs', renderData);
  75. } catch (error) {
  76. console.log(error);
  77. ctx.redirect('/dashboard');
  78. }
  79. }
  80. /**
  81. * 项目设置 -- 账号设置(Get)
  82. * @param ctx
  83. * @return {Promise<void>}
  84. */
  85. async user(ctx) {
  86. try {
  87. // 获取项目数据
  88. const projectId = ctx.session.sessionProject.id;
  89. await this._checkMenu(projectId);
  90. const projectData = await ctx.service.project.getDataById(projectId);
  91. if (projectData === null) {
  92. throw '没有对应的项目数据';
  93. }
  94. if (ctx.session.sessionUser.is_admin === 0) {
  95. throw '没有访问权限';
  96. }
  97. let keyword = '';
  98. if (ctx.query.keyword) {
  99. keyword = ctx.query.keyword;
  100. }
  101. const page = ctx.page;
  102. const pageSize = ctx.pageSize;
  103. // 过滤数据
  104. ctx.service.projectAccount.searchFilter(ctx.request.query, projectId);
  105. ctx.sort = ['id', 'desc'];
  106. const total = await ctx.service.projectAccount.getCountWithBuilder();
  107. // 获取数据规则
  108. // const rule = ctx.service.projectAccount.rule('updateUser');
  109. // const frontRule = ctx.helper.validateConvert(rule);
  110. // const total = await ctx.service.projectAccount.count({ project_id: projectId });
  111. // 获取项目用户列表
  112. // const accountData = await ctx.service.projectAccount.getAllDataByCondition({
  113. // where: { project_id: projectId },
  114. // columns: ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group'],
  115. // limit: (app.config.pageSize * (page - 1)),
  116. // offset: app.config.pageSize,
  117. // });
  118. // const columns = ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group', 'bind'];
  119. // const accountData = await ctx.service.projectAccount.getListByProjectId(columns, projectId);
  120. const accountData = await ctx.service.projectAccount.getListWithBuilder();
  121. // 获取账号个数
  122. const user_total = await ctx.service.projectAccount.count({ project_id: projectId });
  123. // 分页相关
  124. const pageInfo = {
  125. page,
  126. pageSizeSelect: 1,
  127. pageSize,
  128. total_num: total,
  129. total: Math.ceil(total / pageSize),
  130. queryData: JSON.stringify(ctx.urlInfo.query),
  131. };
  132. const renderData = {
  133. projectData,
  134. accountData,
  135. accountGroup,
  136. permission,
  137. pageInfo,
  138. keyword,
  139. user_total,
  140. // rule: JSON.stringify(frontRule),
  141. };
  142. await this.layout('setting/user.ejs', renderData, 'setting/user_modal.ejs');
  143. } catch (error) {
  144. console.log(error);
  145. ctx.redirect('/dashboard');
  146. }
  147. }
  148. /**
  149. * 项目设置 -- 账号解绑设置(Post)
  150. * @param ctx
  151. * @return {Promise<void>}
  152. */
  153. async userUnbind(ctx) {
  154. const projectData = ctx.session.sessionProject;
  155. try {
  156. if (projectData.id === undefined) {
  157. throw '不存在对应的项目数据';
  158. }
  159. if (ctx.session.sessionUser.is_admin === 0) {
  160. throw '非管理员无权解绑账号';
  161. }
  162. const accountId = parseInt(ctx.request.body.id);
  163. const result = await ctx.service.projectAccount.update({ bind: 0 }, { id: accountId });
  164. if (!result) {
  165. throw '解绑失败';
  166. }
  167. this.setMessage('解绑成功', this.messageType.SUCCESS);
  168. } catch (error) {
  169. console.log(error);
  170. this.setMessage(error.toString(), this.messageType.ERROR);
  171. }
  172. ctx.redirect(ctx.request.header.referer);
  173. }
  174. /**
  175. * 项目权限 -- 账号设置(Get)
  176. * @param ctx
  177. * @return {Promise<void>}
  178. */
  179. async userPermissionSet(ctx) {
  180. try {
  181. // 获取项目数据
  182. const projectId = ctx.session.sessionProject.id;
  183. const projectData = await ctx.service.project.getDataById(projectId);
  184. if (projectData === null) {
  185. throw '没有对应的项目数据';
  186. }
  187. if (ctx.session.sessionUser.is_admin === 0) {
  188. throw '没有访问权限';
  189. }
  190. // 获取数据规则
  191. // const rule = ctx.service.projectAccount.rule('updateUser');
  192. // const frontRule = ctx.helper.validateConvert(rule);
  193. const page = ctx.page;
  194. const pageSize = ctx.pageSize;
  195. ctx.sort = ['id', 'desc'];
  196. const total = await ctx.service.projectAccount.count({ project_id: projectId });
  197. // 获取项目用户列表
  198. // const accountData = await ctx.service.projectAccount.getAllDataByCondition({
  199. // where: { project_id: projectId },
  200. // columns: ['id', 'account', 'name', 'company', 'role', 'is_admin', 'account_group', 'permission', 'cooperation'],
  201. // });
  202. const columns = ['id', 'account', 'name', 'company', 'role', 'is_admin', 'account_group', 'permission', 'cooperation'];
  203. const accountData = await ctx.service.projectAccount.getListByProjectId(columns, projectId);
  204. // 分页相关
  205. const pageInfo = {
  206. page,
  207. pageSizeSelect: 1,
  208. pageSize,
  209. total_num: total,
  210. total: Math.ceil(total / pageSize),
  211. queryData: JSON.stringify(ctx.urlInfo.query),
  212. };
  213. const renderData = {
  214. projectData,
  215. accountData,
  216. accountGroup,
  217. permission,
  218. permissionStr: JSON.stringify(permission),
  219. pageInfo,
  220. // rule: JSON.stringify(frontRule),
  221. };
  222. await this.layout('setting/user_permission.ejs', renderData, 'setting/user_permission_modal.ejs');
  223. } catch (error) {
  224. console.log(error);
  225. ctx.redirect('/dashboard');
  226. }
  227. }
  228. /**
  229. * 项目设置 -- 账号启用和停用设置(Post)
  230. * @param ctx
  231. * @return {Promise<void>}
  232. */
  233. async userSwitch(ctx) {
  234. const responseData = {
  235. err: 0, msg: '', data: null,
  236. };
  237. try {
  238. // 获取项目数据
  239. const projectId = ctx.session.sessionProject.id;
  240. const projectData = await ctx.service.project.getDataById(projectId);
  241. if (projectData === null) {
  242. throw '没有对应的项目数据';
  243. }
  244. if (ctx.session.sessionUser.is_admin === 0) {
  245. throw '没有访问权限';
  246. }
  247. const data = JSON.parse(ctx.request.body.data);
  248. const result = await ctx.service.projectAccount.update(data, { id: data.id });
  249. if (!result) {
  250. throw '提交数据失败';
  251. }
  252. } catch (err) {
  253. this.log(err);
  254. responseData.err = 1;
  255. responseData.msg = err;
  256. }
  257. ctx.body = responseData;
  258. }
  259. /**
  260. * 项目设置 -- 账号添加(Post)
  261. * @param ctx
  262. * @return {Promise<void>}
  263. */
  264. async addUser(ctx) {
  265. const projectData = ctx.session.sessionProject;
  266. try {
  267. // 验证数据
  268. if (projectData.id === undefined) {
  269. throw '不存在对应的项目数据';
  270. }
  271. // 获取验证规则
  272. const rule = ctx.service.projectAccount.rule('add');
  273. ctx.validate(rule);
  274. ctx.request.body.project_id = projectData.id;
  275. const result = await ctx.service.projectAccount.save(ctx.request.body);
  276. if (!result) {
  277. throw '保存账号数据失败';
  278. }
  279. this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
  280. ctx.redirect('/' + ctx.controllerName + '/user');
  281. } catch (error) {
  282. console.log(error);
  283. this.setMessage(error.toString(), this.messageType.ERROR);
  284. ctx.redirect(ctx.request.header.referer);
  285. }
  286. }
  287. /**
  288. * 项目设置 -- 账号编辑(Post)
  289. * @param ctx
  290. * @return {Promise<void>}
  291. */
  292. async updateUser(ctx) {
  293. const projectData = ctx.session.sessionProject;
  294. try {
  295. // 验证数据
  296. if (projectData.id === undefined) {
  297. throw '不存在对应的项目数据';
  298. }
  299. // 获取验证规则
  300. const rule = ctx.service.projectAccount.rule('modify');
  301. ctx.validate(rule);
  302. const result = await ctx.service.projectAccount.save(ctx.request.body);
  303. if (!result) {
  304. throw '保存账号数据失败';
  305. }
  306. this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
  307. ctx.redirect(ctx.request.header.referer);
  308. } catch (error) {
  309. console.log(error);
  310. this.setMessage(error.toString(), this.messageType.ERROR);
  311. ctx.redirect(ctx.request.header.referer);
  312. }
  313. }
  314. async resetUserPassword(ctx) {
  315. try {
  316. // 获取项目数据
  317. const projectId = ctx.session.sessionProject.id;
  318. const projectData = await ctx.service.project.getDataById(projectId);
  319. if (projectData === null) {
  320. throw '没有对应的项目数据';
  321. }
  322. if (ctx.session.sessionUser.is_admin === 0) {
  323. throw '没有访问权限';
  324. }
  325. const accountId = parseInt(ctx.request.body.id);
  326. let password = ctx.request.body.reset_password;
  327. password = password.toString();
  328. const account = ctx.request.body.account !== undefined ? ctx.request.body.account : '';
  329. if (isNaN(accountId) || accountId <= 0 || password.length < 6) {
  330. throw '参数错误';
  331. }
  332. const result = await ctx.service.projectAccount.resetPassword(accountId, password, account);
  333. if (!result) {
  334. throw '重置密码失败!';
  335. }
  336. this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
  337. ctx.redirect(ctx.request.header.referer);
  338. } catch (error) {
  339. console.log(error);
  340. this.setMessage(error.toString(), this.messageType.ERROR);
  341. ctx.redirect(ctx.request.header.referer);
  342. }
  343. }
  344. /**
  345. * 项目设置 -- 账号权限编辑(Post)
  346. * @param ctx
  347. * @return {Promise<void>}
  348. */
  349. async permission(ctx) {
  350. const projectData = ctx.session.sessionProject;
  351. try {
  352. // 验证数据
  353. if (projectData.id === undefined) {
  354. throw '不存在对应的项目数据';
  355. }
  356. const accountId = parseInt(ctx.request.body.id);
  357. const accountInfo = await ctx.service.projectAccount.getDataById(accountId);
  358. if (accountInfo === null) {
  359. throw '不存在该客户';
  360. }
  361. const result = await ctx.service.projectAccount.permissionSave(accountId, ctx.request.body);
  362. if (!result) {
  363. throw '修改权限失败!';
  364. }
  365. this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
  366. ctx.redirect(ctx.request.header.referer);
  367. } catch (error) {
  368. console.log(error);
  369. this.setMessage(error.toString(), this.messageType.ERROR);
  370. ctx.redirect(ctx.request.header.referer);
  371. }
  372. }
  373. /**
  374. * 项目设置 -- 自定义标段分类(Get)
  375. *
  376. * @param ctx
  377. * @return {Promise<void>}
  378. */
  379. async category(ctx) {
  380. try {
  381. // 获取项目数据
  382. const projectId = ctx.session.sessionProject.id;
  383. await this._checkMenu(projectId);
  384. const projectData = await ctx.service.project.getDataById(projectId);
  385. if (projectData === null) {
  386. throw '没有对应的项目数据';
  387. }
  388. if (ctx.session.sessionUser.is_admin === 0) {
  389. throw '没有访问权限';
  390. }
  391. const categoryData = await ctx.service.category.getAllCategory(projectId);
  392. const tenderData = await ctx.service.tender.getList('', null, 1);
  393. const renderData = {
  394. projectData,
  395. categoryType: settingConst.cType,
  396. categoryData,
  397. tenderData,
  398. };
  399. await this.layout('setting/category.ejs', renderData, 'setting/category_modal.ejs');
  400. } catch (error) {
  401. console.log(error);
  402. ctx.redirect('/dashboard');
  403. }
  404. }
  405. /**
  406. * 新增分类(Ajax)
  407. *
  408. * @param ctx
  409. * @return {Promise<void>}
  410. */
  411. async addCategory(ctx) {
  412. try {
  413. const projectId = ctx.session.sessionProject.id;
  414. const responseData = {
  415. err: 0, msg: '', data: null,
  416. };
  417. const data = JSON.parse(ctx.request.body.data);
  418. if (!data.name) {
  419. throw '提交数据错误';
  420. }
  421. responseData.data = await ctx.service.category.addCategory(projectId, data.name, settingConst.cType.key.dropDown);
  422. ctx.body = responseData;
  423. } catch (err) {
  424. this.log(err);
  425. ctx.body = { err: 1, msg: err.toString(), data: null };
  426. }
  427. }
  428. /**
  429. * 编辑分类(Ajax)
  430. *
  431. * @param ctx
  432. * @return {Promise<void>}
  433. */
  434. async updateCategory(ctx) {
  435. try {
  436. const projectId = ctx.session.sessionProject.id;
  437. const responseData = { err: 0, msg: '', data: null };
  438. const data = JSON.parse(ctx.request.body.data);
  439. if (!data.id) {
  440. throw '提交数据错误';
  441. }
  442. if (data.name) {
  443. const count = await ctx.service.category.count({ pid: projectId, name: data.name });
  444. if (count >= 1) {
  445. throw '存在同名类别';
  446. }
  447. }
  448. const result = await ctx.service.category.update(data, { id: data.id });
  449. if (!result) {
  450. throw '提交数据失败';
  451. }
  452. responseData.data = await ctx.service.category.getCategory(data.id);
  453. ctx.body = responseData;
  454. } catch (err) {
  455. this.log(err);
  456. ctx.body = { err: 1, msg: err.toString(), data: null };
  457. }
  458. }
  459. async setCategoryValue(ctx) {
  460. try {
  461. const responseData = { err: 0, msg: '', data: {} };
  462. const data = JSON.parse(ctx.request.body.data);
  463. if (!data.id) {
  464. throw '提交数据错误';
  465. }
  466. await ctx.service.categoryValue.setCategoryValue(data.id, data.updateValue);
  467. responseData.data.category = await ctx.service.category.getCategory(data.id);
  468. responseData.data.tenders = await ctx.service.tender.getList('', null, 1);
  469. ctx.body = responseData;
  470. } catch (err) {
  471. this.log(err);
  472. ctx.body = { err: 1, msg: err instanceof String ? err : '提交数据失败', data: null };
  473. }
  474. }
  475. /**
  476. * 删除分类(Ajax)
  477. *
  478. * @param ctx
  479. * @return {Promise<void>}
  480. */
  481. async deleteCategory(ctx) {
  482. try {
  483. const projectId = ctx.session.sessionProject.id;
  484. const responseData = {
  485. err: 0, msg: '', data: null,
  486. };
  487. const data = JSON.parse(ctx.request.body.data);
  488. if (!data.id) {
  489. throw '提交数据错误';
  490. }
  491. await ctx.service.category.deleteCategory(projectId, data.id);
  492. ctx.body = responseData;
  493. } catch (err) {
  494. this.log(err);
  495. ctx.body = { err: 1, msg: err.toString(), data: null };
  496. }
  497. }
  498. /**
  499. * 调整分类层次排序(Ajax)
  500. *
  501. * @param ctx
  502. * @return {Promise<void>}
  503. */
  504. async resetCategoryLevel(ctx) {
  505. try {
  506. const projectId = ctx.session.sessionProject.id;
  507. const responseData = {
  508. err: 0, msg: '', data: null,
  509. };
  510. const data = JSON.parse(ctx.request.body.data);
  511. await ctx.service.category.resetCategoryLevel(data);
  512. responseData.data = await ctx.service.category.getAllCategory(projectId);
  513. ctx.body = responseData;
  514. } catch (err) {
  515. this.log(err);
  516. ctx.body = { err: 1, msg: err.toString(), data: null };
  517. }
  518. }
  519. /** update porject info
  520. * @author wangfeng
  521. * @date 2018-10-12 15:48:05
  522. * @param ctx
  523. * @return {Promise<void>}
  524. */
  525. async updateinfo(ctx) {
  526. try {
  527. const projectId = ctx.params.id;
  528. const responseData = {
  529. err: 0, msg: '', data: null,
  530. };
  531. const conditionData = {
  532. id: projectId,
  533. };
  534. const data = ctx.request.body;
  535. const result = await ctx.service.project.update(data, conditionData);
  536. if (!result) {
  537. throw '提交数据失败';
  538. }
  539. ctx.redirect('/setting/info');
  540. } catch (err) {
  541. this.log(err);
  542. ctx.body = { err: 1, msg: err.toString(), data: null };
  543. }
  544. }
  545. /**
  546. * 检测账户是否存在
  547. *
  548. * @param {Object} ctx -egg全局变量
  549. * @return {void}
  550. */
  551. async accountExist(ctx) {
  552. const projectData = ctx.session.sessionProject;
  553. const responseData = {
  554. err: 0, msg: '', data: null,
  555. };
  556. try {
  557. const data = JSON.parse(ctx.request.body.data);
  558. const account = data.account;
  559. if (projectData.id === undefined) {
  560. throw '不存在对应项目';
  561. }
  562. responseData.data = await ctx.service.projectAccount.isAccountExist(account, projectData.id);
  563. ctx.body = responseData;
  564. } catch (error) {
  565. ctx.body = { err: 1, msg: error.toString(), data: null };
  566. }
  567. }
  568. /**
  569. * 显示设置(Get)
  570. * @param {Object} ctx -egg全局变量
  571. * @return {void}
  572. */
  573. async show(ctx) {
  574. try {
  575. // 获取项目数据
  576. const projectId = ctx.session.sessionProject.id;
  577. await this._checkMenu(projectId);
  578. const projectData = await ctx.service.project.getDataById(projectId);
  579. if (!projectData) {
  580. throw '没有对应的项目数据';
  581. }
  582. if (ctx.session.sessionUser.is_admin === 0) {
  583. throw '没有访问权限';
  584. }
  585. const showList = await ctx.service.settingShow.getList(projectData.page_path);
  586. const renderData = { projectData, showList };
  587. await this.layout('setting/show.ejs', renderData);
  588. } catch (error) {
  589. this.log(error);
  590. ctx.redirect('/dashboard');
  591. }
  592. }
  593. /**
  594. * 更新显示设置列表
  595. * @param {Object} ctx -egg全局变量
  596. * @return {void}
  597. */
  598. async showListUpdate(ctx) {
  599. try {
  600. // 获取项目id
  601. const projectId = ctx.session.sessionProject.id;
  602. const { data } = ctx.request.body;
  603. const { id } = JSON.parse(data);
  604. const result = await ctx.service.settingShow.setDefaultLabel(id, projectId);
  605. const responseData = { err: 0, msg: '', data: result };
  606. ctx.body = responseData;
  607. } catch (error) {
  608. this.log(error);
  609. ctx.body = { err: 1, msg: error.toString(), data: null };
  610. }
  611. }
  612. async logs(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 settingType = ctx.params.type ? parseInt(ctx.params.type) : 0;
  625. const logs = await ctx.service.projectLog.getLogs(projectId, settingType);
  626. const renderData = {
  627. projectData,
  628. officeList,
  629. projectLog,
  630. settingType,
  631. logs,
  632. };
  633. await this.layout('setting/logs.ejs', renderData);
  634. } catch (error) {
  635. console.log(error);
  636. ctx.redirect('/dashboard');
  637. }
  638. }
  639. async fun(ctx) {
  640. try {
  641. const projectId = ctx.session.sessionProject.id;
  642. await this._checkMenu(projectId);
  643. const projectData = await ctx.service.project.getDataById(projectId);
  644. const funRela = await ctx.service.project.getFunRela(projectId);
  645. if (projectData === null) {
  646. throw '没有对应的项目数据';
  647. }
  648. if (ctx.session.sessionUser.is_admin === 0) {
  649. throw '没有访问权限';
  650. }
  651. await this.layout('setting/fun.ejs', {
  652. projectData,
  653. funRela,
  654. imType,
  655. })
  656. } catch (error) {
  657. ctx.helper.log(error);
  658. ctx.redirect('/dashboard');
  659. }
  660. }
  661. /**
  662. * 保存功能设置相关
  663. * @param ctx
  664. * @returns {Promise<void>}
  665. */
  666. async updateFun(ctx) {
  667. try {
  668. const projectId = ctx.session.sessionProject.id;
  669. const projectData = await ctx.service.project.getDataById(projectId);
  670. if (projectData === null) {
  671. throw '没有对应的项目数据';
  672. }
  673. if (ctx.session.sessionUser.is_admin === 0) {
  674. throw '没有访问权限';
  675. }
  676. const data = JSON.parse(ctx.request.body.data);
  677. if (data) ctx.request.body = data;
  678. const rule = ctx.service.project.rule('fun');
  679. ctx.validate(rule);
  680. const result = await ctx.service.project.updateFunRela(projectId, ctx.request.body);
  681. if (!result) throw '保存数据失败';
  682. ctx.body = {err: 0, msg: '', data: null};
  683. } catch(error) {
  684. ctx.helper.log(error);
  685. this.ajaxErrorBody(error, '保存数据失败');
  686. }
  687. }
  688. async s2b (ctx) {
  689. try {
  690. const projectId = ctx.session.sessionProject.id;
  691. await this._checkMenu(projectId);
  692. const projectData = await ctx.service.project.getDataById(projectId);
  693. if (projectData === null) throw '没有对应的项目数据';
  694. if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
  695. const tenders = await ctx.service.tender.getAllDataByCondition({ where: { project_id: projectId } });
  696. for (const t of tenders) {
  697. t.measure_type_str = t.measure_type === measureType.tz.value ? measureType.tz.title : measureType.gcl.title;
  698. const stages = await ctx.service.stage.getAllDataByCondition({ where: { tid: t.id } });
  699. t.stage_count_str = `第${stages.count || 0}期`;
  700. const user = await ctx.service.projectAccount.getAccountInfoById(t.user_id);
  701. t.user_str = user.name + '-' + user.company;
  702. t.show_time = stages.length > 0 ? stages[stages.length - 1].cache_time_r || stages[stages.length - 1].in_time : t.create_time;
  703. }
  704. await this.layout('setting/s2b.ejs', {
  705. projectData,
  706. tenders,
  707. })
  708. } catch (error) {
  709. ctx.helper.log(error);
  710. ctx.redirect('/dashboard');
  711. }
  712. }
  713. async s2bUpdate(ctx) {
  714. try {
  715. const projectId = ctx.session.sessionProject.id;
  716. const projectData = await ctx.service.project.getDataById(projectId);
  717. if (projectData === null) throw '没有对应的项目数据';
  718. if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
  719. const data = JSON.parse(ctx.request.body.data);
  720. if (!data.tid) throw '提交数据错误';
  721. const updateData = {};
  722. if (data.gxby_limit !== undefined) updateData.s2b_gxby_limit = data.gxby_limit;
  723. if (data.gxby_check !== undefined) updateData.s2b_gxby_check = data.gxby_check;
  724. if (data.dagl_limit !== undefined) updateData.s2b_dagl_limit = data.dagl_limit;
  725. if (data.dagl_check !== undefined) updateData.s2b_dagl_check = data.dagl_check;
  726. await ctx.service.tender.saveApiRela(data.tid, updateData);
  727. ctx.body = { err: 0, msg: '', data: null };
  728. } catch (error) {
  729. ctx.helper.log(error);
  730. ctx.ajaxError(error, '保存数据错误');
  731. }
  732. }
  733. }
  734. return SettingController;
  735. };