construction_controller.js 26 KB

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