setting_controller.js 32 KB

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