truthhun 1 year ago
parent b86a15cd44
commit bb9d145073

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

@ -48,4 +48,4 @@
"stylelint-config-prettier": "^8.0.2",
"stylelint-config-standard": "^21.0.0"
}
}
}

@ -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) {

Loading…
Cancel
Save