From 9c18f528894d2e6e591e9549785085c34d7ca943 Mon Sep 17 00:00:00 2001 From: johnniang Date: Fri, 12 Apr 2019 12:12:31 +0800 Subject: [PATCH] Complete post creation feature --- src/api/category.js | 5 +- src/api/post.js | 16 +++++ src/views/post/PostEdit.vue | 81 +++++++++++++++++----- src/views/post/components/CategoryTree.vue | 25 +++++++ 4 files changed, 110 insertions(+), 17 deletions(-) diff --git a/src/api/category.js b/src/api/category.js index 6f5bfff1..4669f92a 100644 --- a/src/api/category.js +++ b/src/api/category.js @@ -41,13 +41,16 @@ function concreteTree(parentCategory, categories) { } parentCategory.children.push({ key: category.id, - title: category.name + title: category.name, + isLeaf: false }) } }) if (parentCategory.children) { parentCategory.children.forEach(category => concreteTree(category, categories)) + } else { + parentCategory.isLeaf = true } } diff --git a/src/api/post.js b/src/api/post.js index ffe2a6b5..b580ef10 100644 --- a/src/api/post.js +++ b/src/api/post.js @@ -19,6 +19,22 @@ postApi.query = params => { }) } +postApi.create = postToCreate => { + return service({ + url: baseUrl, + method: 'post', + data: postToCreate + }) +} + +postApi.update = (postId, postToUpdate) => { + return service({ + url: `${baseUrl}/${postId}`, + method: 'put', + data: postToUpdate + }) +} + postApi.postStatus = { PUBLISHED: { color: 'green', diff --git a/src/views/post/PostEdit.vue b/src/views/post/PostEdit.vue index 18b8441e..f8822132 100644 --- a/src/views/post/PostEdit.vue +++ b/src/views/post/PostEdit.vue @@ -11,6 +11,7 @@
@@ -55,18 +56,21 @@ - + - - - - - + + + 开启 + 关闭 +
@@ -76,7 +80,10 @@

分类目录

- +
@@ -87,13 +94,15 @@ {{ tag.name }} @@ -118,12 +127,12 @@
保存草稿 发布 + >{{ publishText }}
@@ -138,6 +147,7 @@ import { mixin, mixinDevice } from '@/utils/mixin.js' import 'mavon-editor/dist/css/index.css' import tagApi from '@/api/tag' import categoryApi from '@/api/category' +import postApi from '@/api/post' const toolbars = { bold: true, // 粗体 @@ -177,10 +187,20 @@ export default { value: 'Hello World', visible: false, drawerWidth: '460', - postUrl: 'hello-world', tags: [], categories: [], - markdownOption: toolbars + selectedCategoryIds: [], + selectedTagIds: [], + markdownOption: toolbars, + postToStage: {} + } + }, + computed: { + publishText() { + if (this.postToStage.id) { + return '更新' + } + return '创建并发布' } }, mounted() { @@ -205,9 +225,38 @@ export default { this.categories = response.data.data }) }, + createOrUpdatePost() { + // Set category ids + this.postToStage.categoryIds = this.selectedCategoryIds + // Set tag ids + this.postToStage.tagIds = this.selectedTagIds + + if (this.postToStage.id) { + // Update the post + postApi.update(this.postToStage.id, this.postToStage).then(response => { + this.$log.debug('Updated post', response.data.data) + this.$message.success('文章更新成功') + }) + } else { + // Create the post + postApi.create(this.postToStage).then(response => { + this.$log.debug('Created post', response.data.data) + this.$message.success('文章创建成功') + this.postToStage.id = response.data.data.id + }) + } + }, showDrawer() { this.visible = true }, + handlePublishClick() { + this.postToStage.status = 'PUBLISHED' + this.createOrUpdatePost() + }, + handleDraftClick() { + this.postToStage.status = 'DRAFT' + this.createOrUpdatePost() + }, onClose() { this.visible = false } diff --git a/src/views/post/components/CategoryTree.vue b/src/views/post/components/CategoryTree.vue index 94151c06..5fa5b2d6 100644 --- a/src/views/post/components/CategoryTree.vue +++ b/src/views/post/components/CategoryTree.vue @@ -3,6 +3,7 @@ checkable :treeData="categoryTree" :defaultExpandAll="true" + @check="onCheck" > [] + }, categories: { type: Array, required: false, @@ -27,6 +37,21 @@ export default { categoryTree() { return categoryApi.concreteTree(this.categories) } + }, + methods: { + onCheck(checkedKeys, e) { + this.$log.debug('Chekced keys', checkedKeys) + this.$log.debug('e', e) + const categoryIds = e.checkedNodes + .filter(node => { + return node.data.props.isLeaf + }) + .map(node => node.key) + + this.$log.debug('Effectively selected category ids', categoryIds) + + this.$emit('check', categoryIds) + } } }