file.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class Filing extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @param {String} tableName - 表名
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'file';
  21. }
  22. async getFileList(spid, filing_id) {
  23. const result = await this.getAllDataByCondition({
  24. where: { spid, filing_id },
  25. orders: [['create_time', 'asc']],
  26. limit: this.app.config.pageSize, offset: (this.ctx.page - 1) * this.app.config.pageSize
  27. });
  28. const helper = this.ctx.helper;
  29. result.forEach(x => {
  30. x.viewpath = helper.canPreview(x.fileext) ? ctx.app.config.fujianOssPath + x.filepath : '';
  31. x.fileext_str = helper.fileExtStr(x.fileext);
  32. });
  33. return result;
  34. }
  35. async addFile(filing_id, fileInfo, user) {
  36. const filing = await this.ctx.service.filing.getDataById(filing_id);
  37. if (!filing || filing.is_delete) throw '当前分类不存在';
  38. const insertData = {
  39. spid: filing.spid, filing_id: filing.id, filing_type: filing.filing_type,
  40. user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  41. filename: fileInfo.filename, fileext: fileInfo.fileext, filesize: fileInfo.fileSize, filepath: fileInfo.filepath,
  42. };
  43. const result = await this.db.insert(this.tableName, insertData);
  44. }
  45. }
  46. return Filing;
  47. };