lrf 3 سال پیش
والد
کامیت
64adc906a4
2فایلهای تغییر یافته به همراه34 افزوده شده و 12 حذف شده
  1. 10 12
      app/middleware/password.js
  2. 24 0
      lib/service/crud-service.js

+ 10 - 12
app/middleware/password.js

@@ -1,16 +1,14 @@
 'use strict';
-module.exports = ({ enable = false }) => {
+module.exports = () => {
   return async function password(ctx, next) {
-    if (enable) {
-      // mongodb中secret转换为密码类型
-      const method = ctx.request.method;
-      const isLoginEnd = ctx.request.url.includes('login');
-      const passwordNoDeal = ctx.request.headers.passwordnodeal;
-      if (method !== 'GET' && !isLoginEnd && !passwordNoDeal) {
-        const body = ctx.request.body;
-        if (body && body.password) body.password = { secret: body.password };
-      }
-      await next();
-    } else await next();
+    // mongodb中secret转换为密码类型
+    const method = ctx.request.method;
+    const isLoginEnd = ctx.request.url.includes('login');
+    const passwordNoDeal = ctx.request.headers.passwordnodeal;
+    if (method !== 'GET' && !isLoginEnd && !passwordNoDeal) {
+      const body = ctx.request.body;
+      if (body && body.password) body.password = { secret: body.password };
+    }
+    await next();
   };
 };

+ 24 - 0
lib/service/crud-service.js

@@ -81,6 +81,10 @@ class CrudService extends NafService {
     }
     // 再将数据过滤
     if (projection) pipeline.push({ $project: projection });
+    else {
+      const defaultProject = this.getSelectFalse();
+      if (defaultProject)pipeline.push({ $project: defaultProject });
+    }
     const rs = await this.model.aggregate(pipeline);
     // const rs = await this.model.find(trimData(condition), projection, { skip, limit, sort }).exec();
     return rs;
@@ -179,6 +183,26 @@ class CrudService extends NafService {
     }
     return filter;
   }
+
+  /**
+   * 获取model的配置
+   */
+  async getModel() {
+    const obj = this.model.prototype.schema.obj;
+    return obj;
+  }
+  /**
+   * 读取model中不显示的字段
+   */
+  async getSelectFalse() {
+    const obj = await this.getModel();
+    const project = {};
+    for (const k in obj) {
+      const v = obj[k];
+      if (v && v.select === false) project[k] = 0;
+    }
+    if (Object.keys(project).length > 0) return project;
+  }
 }
 
 module.exports.CrudService = CrudService;