message_att.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. module.exports = app => {
  7. class MessageAtt extends BaseService {
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} ctx - egg全局变量
  12. * @return {void}
  13. */
  14. constructor(ctx) {
  15. super(ctx);
  16. this.tableName = 'message_attachment';
  17. this.dataId = 'id';
  18. }
  19. async getAtt(mid = 0) {
  20. let userSql = '';
  21. if (mid === 0) {
  22. userSql += ' AND a.`uid` = ' + this.ctx.session.sessionUser.accountId;
  23. }
  24. const sql = 'SELECT a.*, b.name as username FROM ?? as a LEFT JOIN ?? as b ON a.`uid` = b.`id` WHERE a.`mid` = ?' + userSql + ' ORDER BY `upload_time` DESC';
  25. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, mid];
  26. const result = await this.db.query(sql, sqlParam);
  27. return result.map(item => {
  28. item.orginpath = this.ctx.app.config.fujianOssPath + item.filepath;
  29. if (!this.ctx.helper.canPreview(item.fileext)) {
  30. item.filepath = `/wap/message/download/file/file/${item.id}/download`;
  31. } else {
  32. item.filepath = this.ctx.app.config.fujianOssPath + item.filepath;
  33. item.viewpath = item.filepath;
  34. }
  35. return item;
  36. });
  37. }
  38. /**
  39. * 存储上传的文件信息至数据库
  40. * @param {Array} payload 载荷
  41. * @return {Promise<void>} 数据库插入执行实例
  42. */
  43. async saveFileMsgToDb(payload) {
  44. return await this.db.insert(this.tableName, payload);
  45. }
  46. /**
  47. * 删除附件
  48. * @param {Number} id - 附件id
  49. * @return {void}
  50. */
  51. async delete(id) {
  52. return await this.deleteById(id);
  53. }
  54. }
  55. return MessageAtt;
  56. };