forked from halo-dev/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory.js
85 lines (73 loc) · 1.59 KB
/
category.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import service from '@/utils/service'
const baseUrl = '/api/admin/categories'
const categoryApi = {}
categoryApi.listAll = (more = false) => {
return service({
url: `${baseUrl}`,
params: {
more: more
},
method: 'get'
})
}
categoryApi.listTree = () => {
return service({
url: `${baseUrl}/tree_view`,
method: 'get'
})
}
categoryApi.create = category => {
return service({
url: baseUrl,
data: category,
method: 'post'
})
}
categoryApi.delete = categoryId => {
return service({
url: `${baseUrl}/${categoryId}`,
method: 'delete'
})
}
categoryApi.get = categoryId => {
return service({
url: `${baseUrl}/${categoryId}`,
method: 'get'
})
}
categoryApi.update = (categoryId, category) => {
return service({
url: `${baseUrl}/${categoryId}`,
data: category,
method: 'put'
})
}
function concreteTree(parentCategory, categories) {
categories.forEach(category => {
if (parentCategory.key === category.parentId) {
if (!parentCategory.children) {
parentCategory.children = []
}
parentCategory.children.push({
key: category.id,
title: category.name,
isLeaf: false
})
}
})
if (parentCategory.children) {
parentCategory.children.forEach(category => concreteTree(category, categories))
} else {
parentCategory.isLeaf = true
}
}
categoryApi.concreteTree = categories => {
const topCategoryNode = {
key: 0,
title: 'top',
children: []
}
concreteTree(topCategoryNode, categories)
return topCategoryNode.children
}
export default categoryApi