diff --git a/src/api/category.js b/src/api/category.js index 62f47873..6f928463 100644 --- a/src/api/category.js +++ b/src/api/category.js @@ -36,6 +36,21 @@ categoryApi.delete = categoryId => { }) } +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) { diff --git a/src/views/interface/MenuList.vue b/src/views/interface/MenuList.vue index 5f6fd28f..c60c7564 100644 --- a/src/views/interface/MenuList.vue +++ b/src/views/interface/MenuList.vue @@ -160,7 +160,6 @@ export default { data() { return { title: '添加菜单', - data: [], formType: 'create', loading: false, columns, diff --git a/src/views/post/CategoryList.vue b/src/views/post/CategoryList.vue index f3f4e45e..be311785 100644 --- a/src/views/post/CategoryList.vue +++ b/src/views/post/CategoryList.vue @@ -9,7 +9,7 @@ :xs="24" :style="{ 'padding-bottom': '12px' }" > - + - + 保存 + + 更新 + 返回添加 + @@ -134,6 +146,8 @@ export default { components: { CategorySelectTree, CategoryTree }, data() { return { + title: '添加分类', + formType: 'create', categories: [], categoryToCreate: {}, loading: false, @@ -145,25 +159,48 @@ export default { }, methods: { loadCategories() { + this.loading = true categoryApi.listAll(true).then(response => { this.categories = response.data.data + this.loading = false }) }, - handleCreateCategory() { - categoryApi.create(this.categoryToCreate).then(response => { - this.$message.success('添加成功!') - this.loadCategories() - this.categoryToCreate = {} - }) + handleSaveClick() { + this.createOrUpdateCategory() + }, + handleAddCategory() { + this.title = '添加分类' + this.formType = 'create' + this.categoryToCreate = {} }, handleEditCategory(id) { - this.$message.success('编辑' + id) + categoryApi.get(id).then(response => { + this.categoryToCreate = response.data.data + this.title = '编辑分类' + this.formType = 'update' + }) }, handleDeleteCategory(id) { categoryApi.delete(id).then(response => { this.$message.success('删除成功!') this.loadCategories() }) + }, + createOrUpdateCategory() { + if (this.categoryToCreate.id) { + categoryApi.update(this.categoryToCreate.id, this.categoryToCreate).then(response => { + this.$message.success('更新成功!') + this.loadCategories() + this.categoryToCreate = {} + }) + } else { + categoryApi.create(this.categoryToCreate).then(response => { + this.$message.success('保存成功!') + this.loadCategories() + this.categoryToCreate = {} + }) + } + this.handleAddCategory() } } }