lrf 2 năm trước cách đây
mục cha
commit
46606e4a7b
2 tập tin đã thay đổi với 37 bổ sung1 xóa
  1. 6 0
      app/public/options.js
  2. 31 1
      app/public/table-template.js

+ 6 - 0
app/public/options.js

@@ -14,6 +14,12 @@ module.exports = {
       label: '数字',
       value: 'Number',
     },
+    {
+      label: '金额',
+      value: 'Money',
+      plugins: true,
+      path: 'naf-framework-mongoose-free/lib/model/type-money-plugin',
+    },
     {
       label: '对象',
       value: 'Object',

+ 31 - 1
app/public/table-template.js

@@ -1,8 +1,36 @@
 'use strict';
 const _ = require('lodash');
+const { columnType } = require('./options');
 
 module.exports = (data) => {
-  const { name, name_zh, columns } = data;
+  let { name, name_zh, columns } = data;
+
+  // 将插件和正常字段分开
+  let pluginColumns = columns.filter((f) => {
+    const r = columnType.find((cf) => cf.value === f.type && cf.plugins);
+    if (r) return true;
+    return false;
+  });
+  console.log(pluginColumns);
+  let pluginHead = '';
+  let pluginArea = '';
+  for (const p of pluginColumns) {
+    const { title, type, required = false, remark, index, def, zh, ref, getProp } = p;
+    const params = _.omit(p, ['type']);
+    const r = columnType.find((cf) => cf.value === type);
+    if (!r) continue;
+    const { path } = r;
+    if (path) {
+      pluginHead += `const ${type}Plugin = require('${path}');\n`;
+      pluginArea += `schema.plugin(${type}Plugin(${params}));\n`;
+    }
+  }
+
+  columns = columns.filter((f) => {
+    const r = columnType.find((cf) => cf.value === f.type && cf.plugins);
+    if (r) return false;
+    return true;
+  });
   let columnStr = `const ${name} = { \n`;
   let indexStr = '';
   let has_ObjectId = false;
@@ -47,6 +75,7 @@ const Schema = require('mongoose').Schema;
 const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
 ${has_ObjectId ? "const { ObjectId } = require('mongoose').Types;" : ''}
 ${has_Secret ? "const { Secret } = require('naf-framework-mongoose-free/lib/model/schema');" : ''}
+${pluginHead}
 // ${name_zh || ''}
 ${columnStr}
 const schema = new Schema(${name}, { toJSON: { virtuals: true } });
@@ -54,6 +83,7 @@ schema.index({ id: 1 });
 schema.index({ 'meta.createdAt': 1 });
 ${indexStr}
 schema.plugin(metaPlugin);
+${pluginArea}
 module.exports = app => {
   const { mongoose } = app;
   return mongoose.model('${modelName}', schema, '${name}');