lrf 2 år sedan
förälder
incheckning
a5ed69e0c9
2 ändrade filer med 52 tillägg och 7 borttagningar
  1. 8 5
      app/public/ts-template.js
  2. 44 2
      app/service/table.js

+ 8 - 5
app/public/ts-template.js

@@ -38,13 +38,14 @@ const ModelContext = (data) => {
 };
 const ServiceContext = (data) => {
   const fc = [];
-  const { name: table_name } = data;
+  const { name: table_name, getPath } = data;
   const prefix = _.upperFirst(table_name);
+  const entityPath = getPath('entity');
   fc.push(`import { Provide } from '@midwayjs/decorator';`);
   fc.push(`import { InjectEntityModel } from '@midwayjs/typegoose';`);
   fc.push(`import { ReturnModelType } from '@typegoose/typegoose';`);
   fc.push(`import { BaseService } from 'free-midway-component';`);
-  fc.push(`import { ${prefix} } from '../entity/${_.lowerFirst(table_name)}.entity';`);
+  fc.push(`import { ${prefix} } from '${entityPath}';`);
   fc.push(`type modelType = ReturnModelType<typeof ${prefix}>;`);
   fc.push(`@Provide()`);
   fc.push(`export class ${prefix}Service extends BaseService<modelType> {`);
@@ -55,12 +56,14 @@ const ServiceContext = (data) => {
 };
 const ControllerContext = (data) => {
   const fc = [];
-  const { name, name_zh } = data;
+  const { name, name_zh, getPath } = data;
   const prefix = _.upperFirst(name);
+  const servicePath = getPath('service');
+  const interfacePath = getPath('interface');
   fc.push(`import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayjs/decorator';`);
   fc.push(`import { BaseController } from 'free-midway-component';`);
-  fc.push(`import { ${prefix}Service } from '../service/${_.lowerFirst(name)}.service';`);
-  fc.push(`import { CDTO_${name}, CVO_${name}, FVO_${name},QDTO_${name}, QVO_${name}, UDTO_${name}, UVAO_${name} } from '../interface/${_.lowerFirst(name)}.interface';`);
+  fc.push(`import { ${prefix}Service } from '${servicePath}';`);
+  fc.push(`import { CDTO_${name}, CVO_${name}, FVO_${name},QDTO_${name}, QVO_${name}, UDTO_${name}, UVAO_${name} } from '${interfacePath}';`);
   fc.push(`import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';`);
   fc.push(`import { Validate } from '@midwayjs/validate';`);
   fc.push(`@ApiTags(['${name_zh}'])`);

+ 44 - 2
app/service/table.js

@@ -83,13 +83,55 @@ class TableService extends CrudService {
     }
     return data;
   }
+  /**
+   * 整理文件夹路径数组(一维)
+   * @param {Array} dirs 递归文件夹数据
+   * @param {String} prefix 前缀
+   */
+  getDirPath(dirs, prefix = '', idPath = []) {
+    const list = [];
+    for (const dir of dirs) {
+      const { children = [], name, _id } = dir;
+      let path = `${prefix}/${name}`;
+      let idp = [...idPath, ObjectId(_id).toString()];
+      let obj = { path, idPath: idp };
+      list.push(obj);
+      if (children.length > 0) {
+        let ml = this.getDirPath(children, path, idp);
+        list.push(...ml);
+      }
+    }
+    return list;
+  }
 
   async toExportTS({ ids }) {
     const res = await this.model.find({ _id: { $in: ids } }).lean();
+    const project = res.find((f) => f.project && f.dir);
+    let dirs = [];
+    let dirPaths = [];
+    if (project) {
+      dirs = await this.dirModel.find({ project: project.project }).lean();
+      const firstLevelDir = dirs.filter((f) => !f.super);
+      const inDirDirs = dirs.filter((f) => f.super);
+      let newDir = this.makeDir(firstLevelDir, inDirDirs);
+      dirPaths = this.getDirPath(newDir);
+    }
     const data = {};
     for (const i of res) {
-      const d = TSTemplate(i);
-      data[i.name_zh ? i.name_zh : i.name] = d;
+      const { dir, name } = i;
+      if (dir) {
+        const dirObj = dirPaths.find((f) => _.last(f.idPath) === dir);
+        if (dirObj) {
+          let str = '../';
+          const levelNumber = dirObj.idPath.length || 0;
+          for (let i = 0; i < levelNumber; i++) {
+            str = `${str}../`;
+          }
+          i.getPath = (type) => `${str}${type}${dirObj.path}/${_.lowerFirst(name)}.${type}`;
+        }
+      } else i.getPath = (type) => `../${type}/${_.lowerFirst(name)}.${type}`;
+        const d = TSTemplate(i);
+        data[i.name_zh ? i.name_zh : i.name] = d;
     }
     return data;
   }