construction_controller.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. 'use strict';
  2. const moment = require('moment');
  3. const path = require('path');
  4. const sendToWormhole = require('stream-wormhole');
  5. const constructionConst = require('../const/construction');
  6. module.exports = app => {
  7. class ConstructionController extends app.BaseController {
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} ctx - egg全局变量
  12. * @return {void}
  13. */
  14. constructor(ctx) {
  15. super(ctx);
  16. ctx.showProject = true;
  17. // ctx.showTitle = true;
  18. }
  19. /**
  20. * 支付审批列表页
  21. *
  22. * @param {Object} ctx - egg全局页面
  23. * @return {void}
  24. */
  25. async index(ctx) {
  26. try {
  27. if (!ctx.subProject.page_show.openConstruction) {
  28. throw '该功能已关闭或无法查看';
  29. }
  30. // 获取用户新建标段权利
  31. const accountInfo = await this.ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  32. const userPermission = accountInfo !== undefined && accountInfo.permission !== ''
  33. ? JSON.parse(accountInfo.permission) : null;
  34. const tenderList = await ctx.service.tender.getConstructionList('', userPermission, ctx.session.sessionUser.is_admin);
  35. const categoryData = await ctx.service.category.getAllCategory(ctx.subProject);
  36. const renderData = {
  37. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.construction.index),
  38. tenderList,
  39. categoryData,
  40. // selfCategoryLevel: accountInfo ? accountInfo.self_category_level : '',
  41. selfCategoryLevel: '',
  42. pid: ctx.session.sessionProject.id,
  43. uid: ctx.session.sessionUser.accountId,
  44. };
  45. if (ctx.session.sessionUser.is_admin) {
  46. const projectId = ctx.session.sessionProject.id;
  47. // 获取所有项目参与者
  48. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  49. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: projectId } });
  50. const accountGroupList = unitList.map(item => {
  51. const groupList = accountList.filter(item1 => item1.company === item.name);
  52. return { groupName: item.name, groupList };
  53. }).filter(x => { return x.groupList.length > 0; });
  54. renderData.accountList = accountList;
  55. renderData.accountGroup = accountGroupList;
  56. }
  57. await this.layout('construction/index.ejs', renderData, 'construction/modal.ejs');
  58. } catch (err) {
  59. console.log(err);
  60. this.log(err);
  61. ctx.session.postError = err.toString();
  62. ctx.redirect(this.menu.menu.dashboard.url);
  63. }
  64. }
  65. async auditSave(ctx) {
  66. try {
  67. if (ctx.session.sessionUser.is_admin === 0) throw '没有设置权限';
  68. const tid = parseInt(ctx.params.tid);
  69. const responseData = {
  70. err: 0, msg: '', data: null,
  71. };
  72. const tenderInfo = await ctx.service.tender.getDataById(tid);
  73. if (!tenderInfo) throw '标段不存在';
  74. const data = JSON.parse(ctx.request.body.data);
  75. if (!data.type) {
  76. throw '提交数据错误';
  77. }
  78. let uids;
  79. let result = false;
  80. let auditList = [];
  81. switch (data.type) {
  82. case 'add-audit':
  83. // // 判断用户是单个还是数组
  84. uids = data.id instanceof Array ? data.id : [data.id];
  85. // // 判断该用户的组是否已加入到表中,已加入则提示无需添加
  86. auditList = await ctx.service.constructionAudit.getAllDataByCondition({ where: { tid } });
  87. const addAidList = ctx.helper._.difference(uids, ctx.helper._.map(auditList, 'uid'));
  88. if (addAidList.length === 0) {
  89. throw '用户已存在成员管理中,无需重复添加';
  90. }
  91. const accountList = await ctx.service.projectAccount.getAllDataByCondition({ where: { id: addAidList } });
  92. await ctx.service.constructionAudit.saveAudits(tid, accountList);
  93. responseData.data = await ctx.service.constructionAudit.getList(tid);
  94. break;
  95. case 'del-audit':
  96. uids = data.id instanceof Array ? data.id : [data.id];
  97. auditList = await ctx.service.constructionAudit.getAllDataByCondition({ where: { tid, id: uids } });
  98. if (auditList.length !== uids.length) {
  99. throw '该用户已不存在成员管理中,移除失败';
  100. }
  101. await ctx.service.constructionAudit.delAudit(uids);
  102. responseData.data = await ctx.service.constructionAudit.getList(tid);
  103. break;
  104. case 'save-report':
  105. result = await ctx.service.constructionAudit.updateReport(data.updateData);
  106. if (!result) {
  107. throw '修改权限失败';
  108. }
  109. break;
  110. case 'list':
  111. responseData.data = await ctx.service.constructionAudit.getList(tid);
  112. break;
  113. default: throw '参数有误';
  114. }
  115. ctx.body = responseData;
  116. } catch (err) {
  117. this.log(err);
  118. ctx.body = { err: 1, msg: err.toString(), data: null };
  119. }
  120. }
  121. async list(ctx) {
  122. try {
  123. // 获取上报人列表
  124. const reportUserList = await ctx.service.constructionAudit.getUserList(ctx.constructionTender.id, 1);
  125. // 判断是否有状态及上报人选中
  126. const params = { tid: ctx.constructionTender.id };
  127. let curReportUser = null;
  128. if (ctx.query.uid) {
  129. curReportUser = ctx.helper._.find(reportUserList, { uid: parseInt(ctx.query.uid) });
  130. if (curReportUser) {
  131. params.report_uid = curReportUser.uid;
  132. }
  133. }
  134. const statusList = [];
  135. for (const s in constructionConst.status) {
  136. const findObject = { tid: ctx.constructionTender.id, status: constructionConst.status[s] };
  137. if (params.report_uid) findObject.report_uid = params.report_uid;
  138. const num = await ctx.service.constructionLog.count(findObject);
  139. statusList.push({
  140. statusName: constructionConst.statusString[constructionConst.status[s]],
  141. status: constructionConst.status[s],
  142. num,
  143. });
  144. }
  145. let curStatus = null;
  146. if (ctx.query.status) {
  147. curStatus = ctx.helper._.find(statusList, { status: parseInt(ctx.query.status) });
  148. if (curStatus) {
  149. params.status = curStatus.status;
  150. }
  151. }
  152. const page = ctx.page;
  153. const total = await ctx.service.constructionLog.getCount(params);
  154. ctx.sort = ['id', 'desc'];
  155. const logList = await ctx.service.constructionLog.getLogList(params);
  156. // 分页相关
  157. const pageInfo = {
  158. page,
  159. total: Math.ceil(total / app.config.pageSize),
  160. queryData: JSON.stringify(ctx.urlInfo.query),
  161. };
  162. const renderData = {
  163. reportUserList,
  164. statusList,
  165. curStatus,
  166. curReportUser,
  167. pageInfo,
  168. logList,
  169. constructionConst,
  170. moment,
  171. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.construction.list),
  172. };
  173. // 获取自己所属的待提交状态,且shenpi_uid不为空的日志列表
  174. let reportFlag = false;
  175. if (ctx.helper._.findIndex(reportUserList, { uid: ctx.session.sessionUser.accountId }) !== -1) {
  176. reportFlag = true;
  177. renderData.uncheckLogList = await ctx.service.constructionLog.getUncheckedLogList(ctx.constructionTender.id, ctx.session.sessionUser.accountId);
  178. renderData.dateCodeList = await ctx.service.constructionLog.getDateCodeList(ctx.constructionTender.id);
  179. }
  180. renderData.reportFlag = reportFlag;
  181. await this.layout('construction/list.ejs', renderData, 'construction/list_modal.ejs');
  182. } catch (err) {
  183. console.log(err);
  184. this.log(err);
  185. ctx.session.postError = err.toString();
  186. ctx.redirect(this.request && this.request.headers && this.request.headers.referer ? this.request.headers.referer : `/sp/${ctx.subProject.id}/construction`);
  187. }
  188. }
  189. async addLog(ctx) {
  190. try {
  191. const responseData = {
  192. err: 0, msg: '', data: null,
  193. };
  194. const data = JSON.parse(ctx.request.body.data);
  195. const reportUser = await ctx.service.constructionAudit.getDataByCondition({ tid: ctx.constructionTender.id, uid: ctx.session.sessionUser.accountId });
  196. if (!reportUser) {
  197. throw '非填报人无法新建日志';
  198. }
  199. responseData.data = await ctx.service.constructionLog.addLog(ctx.constructionTender.id, ctx.session.sessionUser.accountId, data);
  200. ctx.body = responseData;
  201. } catch (error) {
  202. this.log(error);
  203. ctx.body = { err: 1, msg: error.toString(), data: null };
  204. }
  205. }
  206. // 批量提交
  207. async startMulti(ctx) {
  208. try {
  209. const responseData = {
  210. err: 0, msg: '', data: null,
  211. };
  212. const data = JSON.parse(ctx.request.body.data);
  213. const reportUser = await ctx.service.constructionAudit.getDataByCondition({ tid: ctx.constructionTender.id, uid: ctx.session.sessionUser.accountId });
  214. if (!reportUser) {
  215. throw '非填报人无法提交日志';
  216. }
  217. responseData.data = await ctx.service.constructionLog.startMulti(data.ids, ctx.session.sessionUser.accountId);
  218. ctx.body = responseData;
  219. } catch (error) {
  220. this.log(error);
  221. ctx.body = { err: 1, msg: error.toString(), data: null };
  222. }
  223. }
  224. async deleteLog(ctx) {
  225. try {
  226. const responseData = {
  227. err: 0, msg: '', data: null,
  228. };
  229. const id = parseInt(ctx.params.id);
  230. if (!id) throw '参数有误';
  231. const logInfo = await ctx.service.constructionLog.getDataById(id);
  232. if (!logInfo) throw '该日志不存在';
  233. if (logInfo.report_uid !== ctx.session.sessionUser.accountId) {
  234. throw '非该日志填报人无法删除日志';
  235. }
  236. responseData.data = await ctx.service.constructionLog.deleteLog(logInfo.id);
  237. ctx.body = responseData;
  238. } catch (error) {
  239. this.log(error);
  240. ctx.body = { err: 1, msg: error.toString(), data: null };
  241. }
  242. }
  243. async logInfo(ctx) {
  244. try {
  245. const id = parseInt(ctx.params.id);
  246. if (!id) throw '参数有误';
  247. const logInfo = await ctx.service.constructionLog.getDataById(id);
  248. if (!logInfo) throw '该日志不存在';
  249. logInfo.log_json = logInfo.log_json ? JSON.parse(logInfo.log_json) : ctx.helper._.cloneDeep(constructionConst.logJson);
  250. const report_user = await ctx.service.projectAccount.getDataById(logInfo.report_uid);
  251. logInfo.report_username = report_user ? report_user.name : '';
  252. const shenpi_user = logInfo.shenpi_uid ? await ctx.service.projectAccount.getDataById(logInfo.shenpi_uid) : null;
  253. logInfo.shenpi_username = shenpi_user ? shenpi_user.name : '';
  254. let filePermission = false;
  255. if (ctx.session.sessionUser.accountId === logInfo.report_uid || ctx.session.sessionUser.accountId === logInfo.shenpi_uid) {
  256. filePermission = true;
  257. }
  258. // 获取附件列表
  259. const attList = await ctx.service.constructionAtt.getConstructionAttachment(logInfo.id, 'desc');
  260. const renderData = {
  261. logInfo,
  262. constructionConst,
  263. attList,
  264. filePermission,
  265. whiteList: ctx.app.config.multipart.whitelist,
  266. OSS_PATH: ctx.app.config.fujianOssPath,
  267. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.construction.info),
  268. };
  269. if (ctx.session.sessionUser.accountId === logInfo.report_uid && logInfo.status === constructionConst.status.uncheck) {
  270. const projectId = ctx.session.sessionProject.id;
  271. // 获取所有项目参与者
  272. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  273. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: projectId } });
  274. const accountGroupList = unitList.map(item => {
  275. const groupList = accountList.filter(item1 => item1.company === item.name);
  276. return { groupName: item.name, groupList };
  277. }).filter(x => { return x.groupList.length > 0; });
  278. renderData.accountList = accountList;
  279. renderData.accountGroup = accountGroupList;
  280. }
  281. await this.layout('construction/info.ejs', renderData, 'construction/info_modal.ejs');
  282. } catch (err) {
  283. console.log(err);
  284. this.log(err);
  285. ctx.session.postError = err.toString();
  286. ctx.redirect(`/sp/${ctx.subProject.id}` + (ctx.constructionTender ? '/constrution/' + ctx.constructionTender.id + '/list' : '/construction'));
  287. }
  288. }
  289. async logSave(ctx) {
  290. try {
  291. const id = parseInt(ctx.params.id);
  292. if (!id) throw '参数有误';
  293. const logInfo = await ctx.service.constructionLog.getDataById(id);
  294. if (!logInfo) throw '该日志不存在';
  295. const data = JSON.parse(ctx.request.body.data);
  296. // 检查权限等
  297. if (logInfo.status === constructionConst.status.checked) {
  298. throw '该日志已审签,无法再操作';
  299. }
  300. if (logInfo.report_uid !== ctx.session.sessionUser.accountId && !(logInfo.shenpi_uid === ctx.session.sessionUser.accountId && data.type === 'checked')) {
  301. throw '您无权操作';
  302. }
  303. const responseData = {
  304. err: 0, msg: '', data: {},
  305. };
  306. switch (data.type) {
  307. case 'update_json':
  308. logInfo.log_json = logInfo.log_json ? JSON.parse(logInfo.log_json) : ctx.helper._.cloneDeep(constructionConst.logJson);
  309. responseData.data = await ctx.service.constructionLog.updateLogJson(logInfo.id, logInfo.log_json, data.updateData);
  310. break;
  311. case 'set_shenpi':
  312. if (data.uid === logInfo.report_uid) {
  313. throw '审签人不能同时为填报人';
  314. }
  315. responseData.data = await ctx.service.constructionLog.setShenpiUser(ctx.constructionTender.id, logInfo.id, data.uid);
  316. break;
  317. case 'remove_shenpi':
  318. responseData.data = await ctx.service.constructionLog.removeShenpiUser(logInfo.id);
  319. break;
  320. case 'checked':
  321. if (logInfo.shenpi_uid !== ctx.session.sessionUser.accountId) {
  322. throw '非当前审签人无法审签日志';
  323. }
  324. responseData.data = await ctx.service.constructionLog.checked(logInfo.id);
  325. break;
  326. case 'start':
  327. const reportUser = await ctx.service.constructionAudit.getDataByCondition({ tid: ctx.constructionTender.id, uid: ctx.session.sessionUser.accountId });
  328. if (!reportUser) {
  329. throw '非填报人无法提交日志';
  330. }
  331. if (!logInfo.shenpi_uid) throw '请选择审签人再提交';
  332. responseData.data = await ctx.service.constructionLog.start(logInfo.id);
  333. break;
  334. case 'checkNo':
  335. if (logInfo.report_uid !== ctx.session.sessionUser.accountId) {
  336. throw '非当前填报人无法提交日志';
  337. }
  338. responseData.data = await ctx.service.constructionLog.checkNo(logInfo.id);
  339. break;
  340. default: throw '参数有误';
  341. }
  342. ctx.body = responseData;
  343. } catch (err) {
  344. this.log(err);
  345. ctx.body = { err: 1, msg: err.toString(), data: null };
  346. }
  347. }
  348. /**
  349. * 上传附件
  350. * @param {Object} ctx - egg全局变量
  351. * @return {void}
  352. */
  353. async uploadFile(ctx) {
  354. const responseData = {
  355. err: 0,
  356. msg: '',
  357. data: [],
  358. };
  359. let stream;
  360. try {
  361. const id = parseInt(ctx.params.id);
  362. if (!id) throw '参数有误';
  363. const logInfo = await ctx.service.constructionLog.getDataById(id);
  364. if (!logInfo) throw '该日志不存在';
  365. const parts = ctx.multipart({ autoFields: true });
  366. const files = [];
  367. let index = 0;
  368. const extra_upload = logInfo.status === constructionConst.status.checked;
  369. const original_data = {
  370. tid: ctx.constructionTender.id,
  371. log_id: logInfo.id,
  372. };
  373. while ((stream = await parts()) !== undefined) {
  374. // 判断用户是否选择上传文件
  375. if (!stream.filename) {
  376. throw '请选择上传的文件!';
  377. }
  378. const fileInfo = path.parse(stream.filename);
  379. const create_time = Date.parse(new Date()) / 1000;
  380. const filepath = `app/public/upload/${original_data.tid}/construction/fujian_${create_time + index.toString() + fileInfo.ext}`;
  381. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  382. await sendToWormhole(stream);
  383. // 保存数据到att表
  384. const fileData = {
  385. upload_time: new Date(),
  386. filename: fileInfo.name,
  387. fileext: fileInfo.ext,
  388. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  389. filepath,
  390. extra_upload,
  391. };
  392. const result = await ctx.service.constructionAtt.save(original_data, fileData, ctx.session.sessionUser.accountId);
  393. if (!result) {
  394. throw '导入数据库保存失败';
  395. }
  396. fileData.uid = ctx.session.sessionUser.accountId;
  397. fileData.u_name = ctx.session.sessionUser.name;
  398. fileData.id = result.insertId;
  399. fileData.orginpath = ctx.app.config.fujianOssPath + filepath;
  400. delete fileData.filepath;
  401. if (!ctx.helper.canPreview(fileData.fileext)) {
  402. fileData.filepath = `/sp/${ctx.subProject.id}/construction/${original_data.tid}/log/${original_data.log_id}/file/${fileData.id}/download`;
  403. } else {
  404. fileData.filepath = ctx.app.config.fujianOssPath + filepath;
  405. fileData.viewpath = ctx.app.config.fujianOssPath + filepath;
  406. }
  407. files.push(fileData);
  408. ++index;
  409. }
  410. responseData.data = files;
  411. } catch (err) {
  412. this.log(err);
  413. // 失败需要消耗掉stream 以防卡死
  414. if (stream) {
  415. await sendToWormhole(stream);
  416. }
  417. this.setMessage(err.toString(), this.messageType.ERROR);
  418. }
  419. ctx.body = responseData;
  420. }
  421. /**
  422. * 下载附件
  423. * @param {Object} ctx - egg全局变量
  424. * @return {void}
  425. */
  426. async downloadFile(ctx) {
  427. const id = ctx.params.fid;
  428. if (id) {
  429. try {
  430. const fileInfo = await ctx.service.constructionAtt.getDataById(id);
  431. if (fileInfo !== undefined && fileInfo !== '') {
  432. // 解决中文无法下载问题
  433. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  434. let disposition = '';
  435. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  436. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
  437. } else if (userAgent.indexOf('firefox') >= 0) {
  438. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
  439. } else {
  440. /* safari等其他非主流浏览器只能自求多福了 */
  441. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
  442. }
  443. ctx.response.set({
  444. 'Content-Type': 'application/octet-stream',
  445. 'Content-Disposition': disposition,
  446. 'Content-Length': fileInfo.filesize,
  447. });
  448. // ctx.body = await fs.createReadStream(fileName);
  449. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  450. } else {
  451. throw '不存在该文件';
  452. }
  453. } catch (err) {
  454. this.log(err);
  455. this.setMessage(err.toString(), this.messageType.ERROR);
  456. }
  457. }
  458. }
  459. /**
  460. * 删除附件
  461. * @param {Object} ctx - egg全局变量
  462. * @return {void}
  463. */
  464. async deleteFile(ctx) {
  465. const responseData = {
  466. err: 0,
  467. msg: '',
  468. data: '',
  469. };
  470. try {
  471. const id = parseInt(ctx.params.id);
  472. if (!id) throw '参数有误';
  473. const logInfo = await ctx.service.constructionLog.getDataById(id);
  474. if (!logInfo) throw '该日志不存在';
  475. const data = JSON.parse(ctx.request.body.data);
  476. const fileInfo = await ctx.service.constructionAtt.getDataById(data.id);
  477. if (!fileInfo || !Object.keys(fileInfo).length) {
  478. throw '该文件不存在';
  479. }
  480. if (!fileInfo.extra_upload && logInfo.status === constructionConst.status.checked) {
  481. throw '无权限删除';
  482. }
  483. if (fileInfo !== undefined && fileInfo !== '') {
  484. // 先删除文件
  485. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  486. // 再删除数据库
  487. await ctx.service.constructionAtt.deleteById(data.id);
  488. responseData.data = '';
  489. } else {
  490. throw '不存在该文件';
  491. }
  492. } catch (err) {
  493. responseData.err = 1;
  494. responseData.msg = err;
  495. }
  496. ctx.body = responseData;
  497. }
  498. }
  499. return ConstructionController;
  500. };