sub_proj_file.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. /**
  3. *
  4. * 附件
  5. * @author Ellisran
  6. * @date 2019/1/11
  7. * @version
  8. */
  9. const path = require('path');
  10. module.exports = app => {
  11. class SubProjFile extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'sub_project_file';
  21. }
  22. _analysisData(files) {
  23. const helper = this.ctx.helper;
  24. const userId = this.ctx.session.sessionUser.accountId;
  25. const ossPath = this.ctx.app.config.fujianOssPath;
  26. files.forEach(x => {
  27. x.filepath = ossPath + x.filepath;
  28. x.viewpath = helper.getPreviewPath(x.fileext, x.filepath);
  29. x.fileext_str = helper.fileExtStr(x.fileext);
  30. x.canEdit = x.user_id === userId;
  31. });
  32. }
  33. async getData(spid, type) {
  34. const data = await this.getAllDataByCondition({
  35. where: { type, spid, is_deleted: 0 },
  36. orders: [['create_time', 'desc']],
  37. });
  38. this._analysisData(data);
  39. return data;
  40. }
  41. async getFiles(condition) {
  42. condition.orders = [['create_time', 'desc']];
  43. const result = await this.getAllDataByCondition(condition);
  44. this._analysisData(result);
  45. return result;
  46. }
  47. async addFiles(spid, type, fileInfo, user) {
  48. const conn = await this.db.beginTransaction();
  49. const result = {};
  50. try {
  51. const insertData = fileInfo.map(x => {
  52. return {
  53. id: this.uuid.v4(), spid, type, rela_id: x.rela_id,
  54. user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  55. filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath,
  56. };
  57. });
  58. await conn.insert(this.tableName, insertData);
  59. await conn.commit();
  60. result.files = { id: insertData.map(x => { return x.id; })};
  61. } catch (err) {
  62. await conn.rollback();
  63. throw err;
  64. }
  65. return await this.getFiles({ where: result.files });
  66. }
  67. async delFiles(files) {
  68. if (files.length === 0) return;
  69. const fileDatas = await this.getAllDataByCondition({ where: { id: files } });
  70. if (this.ctx.subProject.permission.info_permission.indexOf(this.ctx.service.subProjPermission.PermissionConst.info.editfile.value) < 0) {
  71. for (const f of fileDatas) {
  72. if (f.user_id !== this.ctx.session.sessionUser.accountId) throw '您无权编辑该文件';
  73. }
  74. }
  75. const result = {};
  76. const conn = await this.db.beginTransaction();
  77. try {
  78. const updateData = fileDatas.map(x => { return { id: x.id, is_deleted: 1 }; });
  79. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  80. await conn.commit();
  81. result.del = files;
  82. } catch (err) {
  83. await conn.rollback();
  84. throw err;
  85. }
  86. return result;
  87. }
  88. async saveFile(id, filename){
  89. const file = await this.getDataById(id);
  90. if (!file) throw '文件不存在';
  91. if (file.user_id !== this.ctx.session.sessionUser.accountId && this.ctx.subProject.permission.info_permission.indexOf(this.ctx.service.subProjPermission.PermissionConst.info.editfile.value) < 0) throw '您无权编辑该文件';
  92. const info = path.parse(filename);
  93. const updateData = { id, filename: info.name, fileext: info.ext};
  94. await this.defaultUpdate(updateData);
  95. return updateData;
  96. }
  97. async moveFile(id, rela_id) {
  98. const file = await this.getDataById(id);
  99. if (!file) throw '文件不存在';
  100. if (file.user_id !== this.ctx.session.sessionUser.accountId && this.ctx.subProject.permission.info_permission.indexOf(this.ctx.service.subProjPermission.PermissionConst.info.editfile.value) < 0) throw '您无权移动该文件';
  101. const updateData = { id, rela_id };
  102. await this.defaultUpdate(updateData);
  103. return updateData;
  104. }
  105. }
  106. return SubProjFile;
  107. };