|
@@ -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;
|