Browse Source

1.删除controller多 增删改 的抽象方法;2添加默认字段:is_del,虚拟删除标识,需要就用;3.修改返回处理中间件,没有数据时返回报错;4.service添加函数,noDealQuery,与query实现一致.不过filter不再限制为SearchBase类.5.添加utils,randomStr,随机字符串函数

lrf 1 year ago
parent
commit
76595f1de9

+ 7 - 1
src/configuration.ts

@@ -67,10 +67,15 @@ export class FreeConfiguration {
     const port = this.app.getConfig()?.koa?.port;
     if (path) console.log(`api文档: http://127.0.0.1:${port}${path}/index.html`);
   }
+  getReqUri() {
+    const path = this.app.getConfig()?.koa?.globalPrefix;
+    const port = this.app.getConfig()?.koa?.port;
+    if (path) console.log(`api文档: http://127.0.0.1:${port}${path}`);
+  }
   async onReady(container: IMidwayContainer) {
     // TODO something
     this.app.getMiddleware().insertLast(ResponseMiddleware);
-    this.app.useFilter([CustomErrorFilter])
+    this.app.useFilter([CustomErrorFilter]);
     // typegoose设置
     Typegoose.setGlobalOptions({
       schemaOptions: {
@@ -81,6 +86,7 @@ export class FreeConfiguration {
       options: { allowMixed: Typegoose.Severity.ALLOW },
     });
     this.getApiUrl();
+    this.getReqUri();
     // axios设置
     const httpServiceFactory = await container.getAsync(axios.HttpServiceFactory);
     const aos: object = this.app.getConfig('axios.clients');

+ 0 - 15
src/controller/BaseController.ts

@@ -33,19 +33,4 @@ export abstract class BaseController {
    * @param args
    */
   abstract delete(...args);
-  /**
-   * 多修改
-   * @param args
-   */
-  abstract updateMany(...args);
-  /**
-   * 多删除
-   * @param args
-   */
-  abstract deleteMany(...args);
-  /**
-   * 多创建
-   * @param args
-   */
-  abstract createMany(...args);
 }

+ 1 - 0
src/entity/meta.ts

@@ -13,6 +13,7 @@ export default schema => {
       state: { type: Number, default: 0 }, // 数据状态: 0-正常;1-标记删除
       comment: String,
     },
+    is_del: { type: Number, default: 0 }, // 是否删除: 0-使用;1-删除
   });
   schema.set('timestamps', {
     createdAt: 'meta.createdAt',

+ 1 - 0
src/index.ts

@@ -31,3 +31,4 @@ export { PageOptions, ResultOptions } from './service/options';
 // export { FileClearProcessor } from './queue/FileClear.queue';
 /**框架默认设置 */
 export * as defaultConfig from './config/config.default';
+export * as BaseUtil from './util/utils';

+ 2 - 1
src/middleware/response.middleware.ts

@@ -24,7 +24,8 @@ export class ResponseMiddleware implements IMiddleware<Context, NextFunction> {
         //检查返回头,如果返回头中有/files,那就说明是读取文件.不能更改body
         const response = ctx.response;
         const resHeaderContentType = response.header['content-type'] as string;
-        if (!resHeaderContentType.includes('/files')) {
+        // 没有数据的情况,保证也可以返回
+        if (!resHeaderContentType || !resHeaderContentType.includes('/files')) {
           const body = response.body;
           if (body instanceof ServiceError) {
             const r = new ErrorVO(body);

+ 19 - 0
src/service/BaseService.ts

@@ -39,6 +39,25 @@ export abstract class BaseService<T extends AnyParamConstructor<any>> {
       .lean(lean);
     return data;
   }
+  /**
+   * 列表查询-无查询条件处理
+   * @param {SearchBase} filter 查询条件
+   * @param {object} pageOptions 分页条件
+   * @param {Boolean} lean 是否使用JavaScript形式数据;false为mongoose的模型实例数据
+   * @param {Boolean} populate 是否进行ref关联数据
+   * @returns {Promise<object>} 返回列表
+   */
+  async noDealQuery(filter: object, pageOptions: PageOptions = {}, resultOptions: ResultOptions = { lean: true, populate: true }): Promise<Array<any>> {
+    const dup = cloneDeep(filter);
+    const { lean, populate } = resultOptions;
+    let refs = [];
+    if (populate) refs = this.getRefs();
+    const data = await this.model
+      .find(dup, {}, { ...pageOptions })
+      .populate(refs)
+      .lean(lean);
+    return data;
+  }
 
   /**
    * 数据总数查询

+ 3 - 0
src/util/utils.ts

@@ -0,0 +1,3 @@
+export function randomStr(len = 6) {
+  return Math.random().toString(36).slice(-len);
+}