From bb9d1450730d57fa0afd067fdcb6d6e9ba9f6b63 Mon Sep 17 00:00:00 2001 From: truthhun <1272881215@qq.com> Date: Mon, 28 Nov 2022 14:39:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- web/package.json | 2 +- web/utils/utils.js | 12 +++++++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 46d7bca..c67b28b 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/spf13/cobra v1.3.0 github.com/spf13/viper v1.10.1 + github.com/yanyiwu/gojieba v1.2.0 go.uber.org/zap v1.21.0 golang.org/x/net v0.1.0 google.golang.org/genproto v0.0.0-20220228195345-15d65a4533f7 @@ -29,7 +30,6 @@ require ( github.com/goccy/go-json v0.9.7 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/pelletier/go-toml/v2 v2.0.1 // indirect - github.com/yanyiwu/gojieba v1.2.0 // indirect golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect ) diff --git a/web/package.json b/web/package.json index 54d3d2f..5467bbe 100644 --- a/web/package.json +++ b/web/package.json @@ -48,4 +48,4 @@ "stylelint-config-prettier": "^8.0.2", "stylelint-config-standard": "^21.0.0" } -} \ No newline at end of file +} diff --git a/web/utils/utils.js b/web/utils/utils.js index 6654d2e..64fefa7 100644 --- a/web/utils/utils.js +++ b/web/utils/utils.js @@ -118,19 +118,25 @@ export function categoryToTrees(categories, withDisabled = true) { const result = [] const map = {} try { - categories.forEach((item) => { + // 避免修改原对象。比如第一次调用,categories加了children属性,第二次,同样继续追加children属性的值 + const cates = JSON.parse(JSON.stringify(categories)) + cates.forEach((item) => { if (withDisabled) { item.disabled = !item.enable } map[item.id] = item }) - categories.forEach((item) => { + cates.forEach((item) => { const parent = map[item.parent_id] if (parent) { if (parent.disabled) item.disabled = true ;(parent.children || (parent.children = [])).push(item) } else { - result.push(item) + // 如果父级ID不存在,不能直接判断为根节点,还要确认下其父级ID是否存在,存在父级ID的不是根节点 + // eslint-disable-next-line no-lonely-if + if (!item.parent_id) { + result.push(item) + } } }) } catch (error) {