construction_controller.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. // 获取用户新建标段权利
  28. const accountInfo = await this.ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  29. const userPermission = accountInfo !== undefined && accountInfo.permission !== ''
  30. ? JSON.parse(accountInfo.permission) : null;
  31. const tenderList = await ctx.service.tender.getConstructionList('', userPermission, ctx.session.sessionUser.is_admin);
  32. const categoryData = await ctx.service.category.getAllCategory(ctx.session.sessionProject.id);
  33. const renderData = {
  34. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.construction.index),
  35. tenderList,
  36. categoryData,
  37. // selfCategoryLevel: accountInfo ? accountInfo.self_category_level : '',
  38. selfCategoryLevel: '',
  39. pid: ctx.session.sessionProject.id,
  40. uid: ctx.session.sessionUser.accountId,
  41. };
  42. if (ctx.session.sessionUser.is_admin) {
  43. const projectId = ctx.session.sessionProject.id;
  44. // 获取所有项目参与者
  45. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  46. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  47. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  48. });
  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. });
  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 : '/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.getAllDataByCondition({
  273. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  274. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  275. });
  276. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: projectId } });
  277. const accountGroupList = unitList.map(item => {
  278. const groupList = accountList.filter(item1 => item1.company === item.name);
  279. return { groupName: item.name, groupList };
  280. });
  281. renderData.accountList = accountList;
  282. renderData.accountGroup = accountGroupList;
  283. }
  284. await this.layout('construction/info.ejs', renderData, 'construction/info_modal.ejs');
  285. } catch (err) {
  286. console.log(err);
  287. this.log(err);
  288. ctx.session.postError = err.toString();
  289. ctx.redirect(ctx.constructionTender ? '/constrution/' + ctx.constructionTender.id + '/list' : '/construction');
  290. }
  291. }
  292. async logSave(ctx) {
  293. try {
  294. const id = parseInt(ctx.params.id);
  295. if (!id) throw '参数有误';
  296. const logInfo = await ctx.service.constructionLog.getDataById(id);
  297. if (!logInfo) throw '该日志不存在';
  298. const data = JSON.parse(ctx.request.body.data);
  299. // 检查权限等
  300. if (logInfo.status === constructionConst.status.checked) {
  301. throw '该日志已审签,无法再操作';
  302. }
  303. if (logInfo.report_uid !== ctx.session.sessionUser.accountId && !(logInfo.shenpi_uid === ctx.session.sessionUser.accountId && data.type === 'checked')) {
  304. throw '您无权操作';
  305. }
  306. const responseData = {
  307. err: 0, msg: '', data: {},
  308. };
  309. switch (data.type) {
  310. case 'update_json':
  311. logInfo.log_json = logInfo.log_json ? JSON.parse(logInfo.log_json) : ctx.helper._.cloneDeep(constructionConst.logJson);
  312. responseData.data = await ctx.service.constructionLog.updateLogJson(logInfo.id, logInfo.log_json, data.updateData);
  313. break;
  314. case 'set_shenpi':
  315. if (data.uid === logInfo.report_uid) {
  316. throw '审签人不能同时为填报人';
  317. }
  318. responseData.data = await ctx.service.constructionLog.setShenpiUser(ctx.constructionTender.id, logInfo.id, data.uid);
  319. break;
  320. case 'remove_shenpi':
  321. responseData.data = await ctx.service.constructionLog.removeShenpiUser(logInfo.id);
  322. break;
  323. case 'checked':
  324. if (logInfo.shenpi_uid !== ctx.session.sessionUser.accountId) {
  325. throw '非当前审签人无法审签日志';
  326. }
  327. responseData.data = await ctx.service.constructionLog.checked(logInfo.id);
  328. break;
  329. case 'start':
  330. const reportUser = await ctx.service.constructionAudit.getDataByCondition({ tid: ctx.constructionTender.id, uid: ctx.session.sessionUser.accountId });
  331. if (!reportUser) {
  332. throw '非填报人无法提交日志';
  333. }
  334. if (!logInfo.shenpi_uid) throw '请选择审签人再提交';
  335. responseData.data = await ctx.service.constructionLog.start(logInfo.id);
  336. break;
  337. case 'checkNo':
  338. if (logInfo.report_uid !== ctx.session.sessionUser.accountId) {
  339. throw '非当前填报人无法提交日志';
  340. }
  341. responseData.data = await ctx.service.constructionLog.checkNo(logInfo.id);
  342. break;
  343. default: throw '参数有误';
  344. }
  345. ctx.body = responseData;
  346. } catch (err) {
  347. this.log(err);
  348. ctx.body = { err: 1, msg: err.toString(), data: null };
  349. }
  350. }
  351. /**
  352. * 上传附件
  353. * @param {Object} ctx - egg全局变量
  354. * @return {void}
  355. */
  356. async uploadFile(ctx) {
  357. const responseData = {
  358. err: 0,
  359. msg: '',
  360. data: [],
  361. };
  362. let stream;
  363. try {
  364. const id = parseInt(ctx.params.id);
  365. if (!id) throw '参数有误';
  366. const logInfo = await ctx.service.constructionLog.getDataById(id);
  367. if (!logInfo) throw '该日志不存在';
  368. const parts = ctx.multipart({ autoFields: true });
  369. const files = [];
  370. let index = 0;
  371. const extra_upload = logInfo.status === constructionConst.status.checked;
  372. const original_data = {
  373. tid: ctx.constructionTender.id,
  374. log_id: logInfo.id,
  375. };
  376. while ((stream = await parts()) !== undefined) {
  377. // 判断用户是否选择上传文件
  378. if (!stream.filename) {
  379. throw '请选择上传的文件!';
  380. }
  381. const fileInfo = path.parse(stream.filename);
  382. const create_time = Date.parse(new Date()) / 1000;
  383. const filepath = `app/public/upload/${original_data.tid}/construction/fujian_${create_time + index.toString() + fileInfo.ext}`;
  384. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  385. await sendToWormhole(stream);
  386. // 保存数据到att表
  387. const fileData = {
  388. upload_time: new Date(),
  389. filename: fileInfo.name,
  390. fileext: fileInfo.ext,
  391. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  392. filepath,
  393. extra_upload,
  394. };
  395. const result = await ctx.service.constructionAtt.save(original_data, fileData, ctx.session.sessionUser.accountId);
  396. if (!result) {
  397. throw '导入数据库保存失败';
  398. }
  399. fileData.uid = ctx.session.sessionUser.accountId;
  400. fileData.u_name = ctx.session.sessionUser.name;
  401. fileData.id = result.insertId;
  402. fileData.orginpath = ctx.app.config.fujianOssPath + filepath;
  403. delete fileData.filepath;
  404. if (!ctx.helper.canPreview(fileData.fileext)) {
  405. fileData.filepath = `/construction/${original_data.tid}/log/${original_data.log_id}/file/${fileData.id}/download`;
  406. } else {
  407. fileData.filepath = ctx.app.config.fujianOssPath + filepath;
  408. fileData.viewpath = ctx.app.config.fujianOssPath + filepath;
  409. }
  410. files.push(fileData);
  411. ++index;
  412. }
  413. responseData.data = files;
  414. } catch (err) {
  415. this.log(err);
  416. // 失败需要消耗掉stream 以防卡死
  417. if (stream) {
  418. await sendToWormhole(stream);
  419. }
  420. this.setMessage(err.toString(), this.messageType.ERROR);
  421. }
  422. ctx.body = responseData;
  423. }
  424. /**
  425. * 下载附件
  426. * @param {Object} ctx - egg全局变量
  427. * @return {void}
  428. */
  429. async downloadFile(ctx) {
  430. const id = ctx.params.fid;
  431. if (id) {
  432. try {
  433. const fileInfo = await ctx.service.constructionAtt.getDataById(id);
  434. if (fileInfo !== undefined && fileInfo !== '') {
  435. // 解决中文无法下载问题
  436. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  437. let disposition = '';
  438. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  439. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
  440. } else if (userAgent.indexOf('firefox') >= 0) {
  441. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
  442. } else {
  443. /* safari等其他非主流浏览器只能自求多福了 */
  444. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
  445. }
  446. ctx.response.set({
  447. 'Content-Type': 'application/octet-stream',
  448. 'Content-Disposition': disposition,
  449. 'Content-Length': fileInfo.filesize,
  450. });
  451. // ctx.body = await fs.createReadStream(fileName);
  452. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  453. } else {
  454. throw '不存在该文件';
  455. }
  456. } catch (err) {
  457. this.log(err);
  458. this.setMessage(err.toString(), this.messageType.ERROR);
  459. }
  460. }
  461. }
  462. /**
  463. * 删除附件
  464. * @param {Object} ctx - egg全局变量
  465. * @return {void}
  466. */
  467. async deleteFile(ctx) {
  468. const responseData = {
  469. err: 0,
  470. msg: '',
  471. data: '',
  472. };
  473. try {
  474. const id = parseInt(ctx.params.id);
  475. if (!id) throw '参数有误';
  476. const logInfo = await ctx.service.constructionLog.getDataById(id);
  477. if (!logInfo) throw '该日志不存在';
  478. const data = JSON.parse(ctx.request.body.data);
  479. const fileInfo = await ctx.service.constructionAtt.getDataById(data.id);
  480. if (!fileInfo || !Object.keys(fileInfo).length) {
  481. throw '该文件不存在';
  482. }
  483. if (!fileInfo.extra_upload && logInfo.status === constructionConst.status.checked) {
  484. throw '无权限删除';
  485. }
  486. if (fileInfo !== undefined && fileInfo !== '') {
  487. // 先删除文件
  488. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  489. // 再删除数据库
  490. await ctx.service.constructionAtt.deleteById(data.id);
  491. responseData.data = '';
  492. } else {
  493. throw '不存在该文件';
  494. }
  495. } catch (err) {
  496. responseData.err = 1;
  497. responseData.msg = err;
  498. }
  499. ctx.body = responseData;
  500. }
  501. }
  502. return ConstructionController;
  503. };