lrf 2 éve
szülő
commit
828700869a

+ 8 - 2
app/controller/system/config/.goodsTags.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['label', 'code', 'pid', 'status'],
+    requestBody: ['label', 'code', 'pid', 'level', 'status'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['label', 'code', 'pid', 'status'],
+    requestBody: ['label', 'code', 'pid', 'level', 'status'],
   },
   show: {
     parameters: {
@@ -24,6 +24,7 @@ module.exports = {
         label: 'label',
         code: 'code',
         pid: 'pid',
+        level: 'level',
         status: 'status',
       },
       // options: {
@@ -38,4 +39,9 @@ module.exports = {
       count: true,
     },
   },
+  tree: {
+    parameters: {
+      query: {},
+    },
+  },
 };

+ 2 - 0
app/model/system/goodsTags.js

@@ -7,6 +7,7 @@ const goodsTags = {
   label: { type: String, required: false, zh: '显示名称' }, //
   code: { type: String, required: false, zh: '标签编码' }, //
   pid: { type: String, required: false, zh: '上级标签' }, //
+  level: { type: Number, required: false, zh: '层级' }, //
   status: { type: String, required: false, default: '0', zh: '状态' }, // 字典:use
 };
 const schema = new Schema(goodsTags, { toJSON: { getters: true, virtuals: true } });
@@ -15,6 +16,7 @@ schema.index({ 'meta.createdAt': 1 });
 schema.index({ label: 1 });
 schema.index({ code: 1 });
 schema.index({ pid: 1 });
+schema.index({ level: 1 });
 schema.index({ status: 1 });
 
 schema.plugin(metaPlugin);

+ 34 - 0
app/public/defaultData/goodsTags.js

@@ -13,6 +13,40 @@ module.exports = [
           },
         ],
       },
+      {
+        label: '糖果|巧克力',
+        code: 'tg|qkl',
+        children: [
+          {
+            label: '糖果',
+            code: 'tg',
+          },
+          {
+            label: '巧克力',
+            code: 'qkl',
+          },
+        ],
+      },
+    ],
+  },
+  {
+    label: '进口食品',
+    code: 'jksp',
+    children: [
+      {
+        label: '进口坚果蜜饯',
+        code: 'jkjgmj',
+        children: [
+          {
+            label: '进口坚果',
+            code: 'jkjg',
+          },
+          {
+            label: '进口蜜饯干果',
+            code: 'jkmjgg',
+          },
+        ],
+      },
     ],
   },
 ];

+ 21 - 0
app/service/system/goodsTags.js

@@ -3,6 +3,7 @@ const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
+const { ObjectId } = require('mongoose').Types;
 
 //
 class GoodsTagsService extends CrudService {
@@ -15,6 +16,26 @@ class GoodsTagsService extends CrudService {
     if (!pid) filter.pid = { $exists: false };
     return filter;
   }
+
+  async tree() {
+    let list = await this.model.find({ status: '0' });
+    list = JSON.parse(JSON.stringify(list));
+    list = list.map(i => _.omit(i, [ 'meta', '__v' ]));
+    const level1 = list.filter(f => !f.pid);
+    const loop = (list, parents) => {
+      for (const p of parents) {
+        const pid = _.get(p, '_id');
+        if (!pid) continue;
+        const children = list.filter(f => ObjectId(pid).equals(f.pid));
+        if (children.length <= 0) continue;
+        const newChildren = loop(list, children);
+        p.children = newChildren;
+      }
+      return parents;
+    };
+    const tree = loop(list, level1);
+    return tree;
+  }
 }
 
 module.exports = GoodsTagsService;

+ 3 - 3
app/service/util/install.js

@@ -24,13 +24,13 @@ class InstallService extends CrudService {
     const model = this.ctx.model.System.GoodsTags;
     const p = path.resolve(this.dataIndex, 'goodsTags.js');
     const data = require(p);
-    const loop = async (list, pid) => {
+    const loop = async (list, pid, level = 1) => {
       for (const i of list) {
         const { children = [], label, code } = i;
         let pdata;
         pdata = await model.findOne({ code });
-        if (!pdata) pdata = await model.create({ label, code, pid });
-        if (children.length > 0) loop(children, pdata._id);
+        if (!pdata) pdata = await model.create({ label, code, pid, level });
+        if (children.length > 0) loop(children, pdata._id, level + 1);
       }
     };
     await loop(data);

+ 1 - 0
app/z_router/system/goodsTags.js

@@ -7,6 +7,7 @@ const rkey = 'goodsTags';
 const ckey = 'system.goodsTags';
 const keyZh = '商品标签';
 const routes = [
+  { method: 'get', path: `${rkey}/tree`, controller: `${ckey}.tree`, name: `${ckey}Tree`, zh: `${keyZh}树结构查询` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
   { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },