| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 'use strict';
- /**
- *
- * 附件
- * @author Ellisran
- * @date 2019/1/11
- * @version
- */
- const path = require('path');
- module.exports = app => {
- class SubProjFile extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'sub_project_file';
- }
- _analysisData(files) {
- const helper = this.ctx.helper;
- const userId = this.ctx.session.sessionUser.accountId;
- const ossPath = this.ctx.app.config.fujianOssPath;
- files.forEach(x => {
- x.filepath = ossPath + x.filepath;
- x.viewpath = helper.getPreviewPath(x.fileext, x.filepath);
- x.fileext_str = helper.fileExtStr(x.fileext);
- x.canEdit = x.user_id === userId;
- });
- }
- async getData(spid, type) {
- const data = await this.getAllDataByCondition({
- where: { type, spid, is_deleted: 0 },
- orders: [['create_time', 'desc']],
- });
- this._analysisData(data);
- return data;
- }
- async getFiles(condition) {
- condition.orders = [['create_time', 'desc']];
- const result = await this.getAllDataByCondition(condition);
- this._analysisData(result);
- return result;
- }
- async addFiles(spid, type, fileInfo, user) {
- const conn = await this.db.beginTransaction();
- const result = {};
- try {
- const insertData = fileInfo.map(x => {
- return {
- id: this.uuid.v4(), spid, type, rela_id: x.rela_id,
- user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
- filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath,
- };
- });
- await conn.insert(this.tableName, insertData);
- await conn.commit();
- result.files = { id: insertData.map(x => { return x.id; })};
- } catch (err) {
- await conn.rollback();
- throw err;
- }
- return await this.getFiles({ where: result.files });
- }
- async delFiles(files) {
- if (files.length === 0) return;
- const fileDatas = await this.getAllDataByCondition({ where: { id: files } });
- if (this.ctx.subProject.permission.info_permission.indexOf(this.ctx.service.subProjPermission.PermissionConst.info.editfile.value) < 0) {
- for (const f of fileDatas) {
- if (f.user_id !== this.ctx.session.sessionUser.accountId) throw '您无权编辑该文件';
- }
- }
- const result = {};
- const conn = await this.db.beginTransaction();
- try {
- const updateData = fileDatas.map(x => { return { id: x.id, is_deleted: 1 }; });
- if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
- await conn.commit();
- result.del = files;
- } catch (err) {
- await conn.rollback();
- throw err;
- }
- return result;
- }
- async saveFile(id, filename){
- const file = await this.getDataById(id);
- if (!file) throw '文件不存在';
- 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 '您无权编辑该文件';
- const info = path.parse(filename);
- const updateData = { id, filename: info.name, fileext: info.ext};
- await this.defaultUpdate(updateData);
- return updateData;
- }
- async moveFile(id, rela_id) {
- const file = await this.getDataById(id);
- if (!file) throw '文件不存在';
- 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 '您无权移动该文件';
- const updateData = { id, rela_id };
- await this.defaultUpdate(updateData);
- return updateData;
- }
- }
- return SubProjFile;
- };
|