'use strict'; /** * Created by EllisRan on 2020/3/3. */ const BaseService = require('../base/base_service'); module.exports = app => { class MessageAtt extends BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'message_attachment'; this.dataId = 'id'; } async getAtt(mid = 0) { let userSql = ''; if (mid === 0) { userSql += ' AND a.`uid` = ' + this.ctx.session.sessionUser.accountId; } 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'; const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, mid]; const result = await this.db.query(sql, sqlParam); return result.map(item => { item.orginpath = this.ctx.app.config.fujianOssPath + item.filepath; if (!this.ctx.helper.canPreview(item.fileext)) { item.filepath = `/wap/message/download/file/file/${item.id}/download`; } else { item.filepath = this.ctx.app.config.fujianOssPath + item.filepath; item.viewpath = item.filepath; } return item; }); } /** * 存储上传的文件信息至数据库 * @param {Array} payload 载荷 * @return {Promise} 数据库插入执行实例 */ async saveFileMsgToDb(payload) { return await this.db.insert(this.tableName, payload); } /** * 删除附件 * @param {Number} id - 附件id * @return {void} */ async delete(id) { return await this.deleteById(id); } } return MessageAtt; };