2019-04-05 05:05:12 +00:00
|
|
|
import service from '@/utils/service'
|
|
|
|
|
2019-04-16 15:40:22 +00:00
|
|
|
const baseUrl = '/api/admin/categories'
|
2019-04-05 05:05:12 +00:00
|
|
|
|
|
|
|
const categoryApi = {}
|
|
|
|
|
2019-04-23 10:38:31 +00:00
|
|
|
categoryApi.listAll = (more = false) => {
|
2019-04-05 05:05:12 +00:00
|
|
|
return service({
|
|
|
|
url: `${baseUrl}`,
|
2019-04-23 10:38:31 +00:00
|
|
|
params: {
|
|
|
|
more: more
|
|
|
|
},
|
2019-04-05 05:05:12 +00:00
|
|
|
method: 'get'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-10 16:09:00 +00:00
|
|
|
categoryApi.listTree = () => {
|
|
|
|
return service({
|
|
|
|
url: `${baseUrl}/tree_view`,
|
|
|
|
method: 'get'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-11 11:44:33 +00:00
|
|
|
categoryApi.create = category => {
|
2019-04-08 16:02:57 +00:00
|
|
|
return service({
|
|
|
|
url: baseUrl,
|
|
|
|
data: category,
|
|
|
|
method: 'post'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
categoryApi.delete = categoryId => {
|
|
|
|
return service({
|
|
|
|
url: `${baseUrl}/${categoryId}`,
|
|
|
|
method: 'delete'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-27 16:14:50 +00:00
|
|
|
categoryApi.get = categoryId => {
|
|
|
|
return service({
|
|
|
|
url: `${baseUrl}/${categoryId}`,
|
|
|
|
method: 'get'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
categoryApi.update = (categoryId, category) => {
|
|
|
|
return service({
|
|
|
|
url: `${baseUrl}/${categoryId}`,
|
|
|
|
data: category,
|
|
|
|
method: 'put'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-11 11:44:33 +00:00
|
|
|
function concreteTree(parentCategory, categories) {
|
|
|
|
categories.forEach(category => {
|
|
|
|
if (parentCategory.key === category.parentId) {
|
|
|
|
if (!parentCategory.children) {
|
|
|
|
parentCategory.children = []
|
|
|
|
}
|
|
|
|
parentCategory.children.push({
|
|
|
|
key: category.id,
|
2019-04-12 04:12:31 +00:00
|
|
|
title: category.name,
|
|
|
|
isLeaf: false
|
2019-04-11 11:44:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (parentCategory.children) {
|
|
|
|
parentCategory.children.forEach(category => concreteTree(category, categories))
|
2019-04-12 04:12:31 +00:00
|
|
|
} else {
|
|
|
|
parentCategory.isLeaf = true
|
2019-04-11 11:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
categoryApi.concreteTree = categories => {
|
|
|
|
const topCategoryNode = {
|
|
|
|
key: 0,
|
|
|
|
title: 'top',
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
concreteTree(topCategoryNode, categories)
|
|
|
|
return topCategoryNode.children
|
|
|
|
}
|
|
|
|
|
2019-04-05 05:05:12 +00:00
|
|
|
export default categoryApi
|