change_att.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 'use strict';
  2. /**
  3. *
  4. * 附件
  5. * @author Ellisran
  6. * @date 2019/1/11
  7. * @version
  8. */
  9. const archiver = require('archiver');
  10. const path = require('path');
  11. const fs = require('fs');
  12. module.exports = app => {
  13. class ChangeAtt extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'change_attachment';
  23. }
  24. /**
  25. * 添加附件
  26. * @param {Object} postData - 表单信息
  27. * @param {Object} fileData - 文件信息
  28. * @param {int} uid - 上传者id
  29. * @return {void}
  30. */
  31. async save(postData, fileData, uid) {
  32. const data = {
  33. tid: postData.tid,
  34. cid: postData.cid,
  35. uid,
  36. };
  37. Object.assign(data, fileData);
  38. const result = await this.db.insert(this.tableName, data);
  39. return result;
  40. }
  41. /**
  42. * 获取 变更令 所有附件
  43. * @param {uuid} cid - 变更令id
  44. * @return {Promise<void>}
  45. */
  46. async getChangeAttachment(cid, sort = 'asc') {
  47. const sql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
  48. ' FROM ?? As ca ' +
  49. ' Left Join ?? As pa ' +
  50. ' On ca.uid = pa.id ' +
  51. ' Where ca.cid = ? order by id ' + sort;
  52. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid];
  53. const result = await this.db.query(sql, sqlParam);
  54. return result.map(item => {
  55. item.orginpath = this.ctx.app.config.fujianOssPath + item.filepath;
  56. if (!this.ctx.helper.canPreview(item.fileext)) {
  57. item.filepath = `/change/download/file/${item.id}`;
  58. } else {
  59. item.filepath = this.ctx.app.config.fujianOssPath + item.filepath;
  60. }
  61. return item;
  62. });
  63. }
  64. /**
  65. * 获取所有附件
  66. * @param {String} cid 变更令id
  67. */
  68. async getAllChangeFiles(cid) {
  69. const { ctx } = this;
  70. const result = await this.db.select(this.tableName, { where: { cid } });
  71. return result.map(item => {
  72. item.orginpath = this.ctx.app.config.fujianOssPath + item.filepath;
  73. if (!ctx.helper.canPreview(item.fileext)) {
  74. item.filepath = `/change/download/file/${item.id}`;
  75. } else {
  76. // item.filepath = item.filepath.replace(/^app|\/app/, '');
  77. item.filepath = this.ctx.config.ossPath + item.filepath;
  78. }
  79. return item;
  80. });
  81. }
  82. /**
  83. * 将文件压缩成zip,并返回zip文件的路径
  84. * @param {array} fileIds - 文件数组id
  85. * @param {string} zipPath - 压缩文件存储路径
  86. * @return {string} 压缩后的zip文件路径
  87. */
  88. async compressedFile(fileIds, zipPath) {
  89. this.initSqlBuilder();
  90. this.sqlBuilder.setAndWhere('id', {
  91. value: fileIds,
  92. operate: 'in',
  93. });
  94. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  95. const files = await this.db.query(sql, sqlParam);
  96. return new Promise((resolve, reject) => {
  97. // 每次开一个新的archiver
  98. const ziparchiver = archiver('zip');
  99. const outputPath = fs.createWriteStream(path.resolve(this.app.baseDir, zipPath));
  100. outputPath.on('error', err => {
  101. return reject(err);
  102. });
  103. ziparchiver.pipe(outputPath);
  104. files.forEach(item => {
  105. ziparchiver.file(path.resolve(this.app.baseDir, item.filepath), { name: item.filename + item.fileext });
  106. });
  107. // 存档警告
  108. ziparchiver.on('warning', function(err) {
  109. // if (err.code === 'ENOENT') {
  110. // console.warn('stat故障和其他非阻塞错误');
  111. // }
  112. return reject(err);
  113. });
  114. // 存档出错
  115. ziparchiver.on('error', function(err) {
  116. // console.log(err);
  117. return reject(err);
  118. });
  119. ziparchiver.finalize();
  120. outputPath.on('close', () => {
  121. return resolve(ziparchiver.pointer());
  122. });
  123. });
  124. }
  125. /**
  126. * 返回所查询的变更令的名称
  127. * @param {string} cid - 变更令id
  128. */
  129. async getChangeName(cid) {
  130. const sql = 'SELECT name FROM ?? WHERE cid = ?';
  131. const sqlParam = [this.ctx.service.change.tableName, cid];
  132. return await this.db.queryOne(sql, sqlParam);
  133. }
  134. }
  135. return ChangeAtt;
  136. };