|
@@ -0,0 +1,228 @@
|
|
|
|
+<template>
|
|
|
|
+ <div class="createPost-container">
|
|
|
|
+ <el-form ref="postForm" :label-position="labelPosition" :model="postForm" :rules="rules" class="form-container">
|
|
|
|
+ <div class="createPost-main-container">
|
|
|
|
+ <el-form-item prop="question" style="" label="问题">
|
|
|
|
+ <el-input v-model="postForm.question" placeholder="" style="width: 300px;" />
|
|
|
|
+ </el-form-item>
|
|
|
|
+ <el-form-item prop="content" style="" label="回答">
|
|
|
|
+ <Tinymce ref="editor" v-model="postForm.answer" :height="400" />
|
|
|
|
+ </el-form-item>
|
|
|
|
+ <el-form-item prop="sort" style="" label="排序">
|
|
|
|
+ <el-input v-model="postForm.sort" placeholder="" style="width: 100px;" />
|
|
|
|
+ </el-form-item>
|
|
|
|
+ <el-row>
|
|
|
|
+ <el-button v-if="!isEdit" v-loading="loading" type="success" @click="submitForm">
|
|
|
|
+ 提交
|
|
|
|
+ </el-button>
|
|
|
|
+ <el-button v-if="isEdit" v-loading="loading" type="success" @click="updateArticle">
|
|
|
|
+ 修改
|
|
|
|
+ </el-button>
|
|
|
|
+ </el-row>
|
|
|
|
+ </div>
|
|
|
|
+ </el-form>
|
|
|
|
+ </div>
|
|
|
|
+</template>
|
|
|
|
+
|
|
|
|
+<script>
|
|
|
|
+import Tinymce from '@/components/Tinymce'
|
|
|
|
+
|
|
|
|
+// import Sticky from '@/components/Sticky' // 粘性header组件
|
|
|
|
+// import { validURL } from '@/utils/validate'
|
|
|
|
+import { fetchQuestion, createQuestion, updateQuestion } from '@/api/question'
|
|
|
|
+import { searchUser } from '@/api/remote-search'
|
|
|
|
+// import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from './Dropdown'
|
|
|
|
+
|
|
|
|
+const defaultForm = {
|
|
|
|
+ status: 'draft',
|
|
|
|
+ title: '', // 文章题目
|
|
|
|
+ content: '', // 文章内容
|
|
|
|
+ content_short: '', // 文章摘要
|
|
|
|
+ video_url: '', // 文章外链
|
|
|
|
+ cover_img: '', // 文章图片
|
|
|
|
+ display_time: undefined, // 前台展示时间
|
|
|
|
+ id: undefined,
|
|
|
|
+ platforms: ['a-platform'],
|
|
|
|
+ comment_disabled: false,
|
|
|
|
+ importance: 0
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export default {
|
|
|
|
+ name: 'ArticleDetail',
|
|
|
|
+ components: { Tinymce },
|
|
|
|
+ props: {
|
|
|
|
+ isEdit: {
|
|
|
|
+ type: Boolean,
|
|
|
|
+ default: false
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ data() {
|
|
|
|
+ const validateRequire = (rule, value, callback) => {
|
|
|
|
+ if (value === '') {
|
|
|
|
+ this.$message({
|
|
|
|
+ message: rule.field + '为必传项',
|
|
|
|
+ type: 'error'
|
|
|
|
+ })
|
|
|
|
+ callback(new Error(rule.field + '为必传项'))
|
|
|
|
+ } else {
|
|
|
|
+ callback()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return {
|
|
|
|
+ postForm: Object.assign({}, defaultForm),
|
|
|
|
+ loading: false,
|
|
|
|
+ userListOptions: [],
|
|
|
|
+ rules: {
|
|
|
|
+ sort: [{ message: '排序不为空', validator: validateRequire }],
|
|
|
|
+ question: [{ message: '问题不为空', validator: validateRequire }],
|
|
|
|
+ answer: [{ message: '回答不为空', validator: validateRequire }]
|
|
|
|
+ },
|
|
|
|
+ tempRoute: {},
|
|
|
|
+ labelPosition: 'top'
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ computed: {
|
|
|
|
+ contentShortLength() {
|
|
|
|
+ return this.postForm.content_short.length
|
|
|
|
+ },
|
|
|
|
+ displayTime: {
|
|
|
|
+ // set and get is useful when the data
|
|
|
|
+ // returned by the back end api is different from the front end
|
|
|
|
+ // back end return => "2013-06-25 06:59:25"
|
|
|
|
+ // front end need timestamp => 1372114765000
|
|
|
|
+ get() {
|
|
|
|
+ return (+new Date(this.postForm.display_time))
|
|
|
|
+ },
|
|
|
|
+ set(val) {
|
|
|
|
+ this.postForm.display_time = new Date(val)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ created() {
|
|
|
|
+ if (this.isEdit) {
|
|
|
|
+ const id = this.$route.params && this.$route.params.id
|
|
|
|
+ this.fetchData(id)
|
|
|
|
+ }
|
|
|
|
+ // Why need to make a copy of this.$route here?
|
|
|
|
+ // Because if you enter this page and quickly switch tag, may be in the execution of the setTagsViewTitle function, this.$route is no longer pointing to the current page
|
|
|
|
+ // https://github.com/PanJiaChen/vue-element-admin/issues/1221
|
|
|
|
+ this.tempRoute = Object.assign({}, this.$route)
|
|
|
|
+ },
|
|
|
|
+ methods: {
|
|
|
|
+ fetchData(id) {
|
|
|
|
+ fetchQuestion(id).then(response => {
|
|
|
|
+ this.postForm = response.data.info
|
|
|
|
+ // set tags view title
|
|
|
|
+ // this.setTagsViewTitle()
|
|
|
|
+ // set page title
|
|
|
|
+ // this.setPageTitle()
|
|
|
|
+ }).catch(err => {
|
|
|
|
+ console.log(err)
|
|
|
|
+ })
|
|
|
|
+ },
|
|
|
|
+ setPageTitle() {
|
|
|
|
+ const title = 'Edit Article'
|
|
|
|
+ document.title = `${title} - ${this.postForm.id}`
|
|
|
|
+ },
|
|
|
|
+ submitForm() {
|
|
|
|
+ this.$refs.postForm.validate(valid => {
|
|
|
|
+ if (valid) {
|
|
|
|
+ this.loading = true
|
|
|
|
+ console.log(this.postForm)
|
|
|
|
+ createQuestion(this.postForm).then(response => {
|
|
|
|
+ this.$notify({
|
|
|
|
+ title: '成功',
|
|
|
|
+ message: '发布成功',
|
|
|
|
+ type: 'success',
|
|
|
|
+ duration: 2000
|
|
|
|
+ })
|
|
|
|
+ this.postForm.status = 'published'
|
|
|
|
+ this.loading = false
|
|
|
|
+ this.listLoading = false
|
|
|
|
+ this.$router.push(`/question/list`)
|
|
|
|
+ })
|
|
|
|
+ } else {
|
|
|
|
+ console.log('error submit!!')
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ },
|
|
|
|
+ updateArticle() {
|
|
|
|
+ console.log(this.postForm)
|
|
|
|
+ updateQuestion(this.postForm).then(response => {
|
|
|
|
+ this.$notify({
|
|
|
|
+ title: '修改',
|
|
|
|
+ message: '修改成功',
|
|
|
|
+ type: 'success',
|
|
|
|
+ duration: 2000
|
|
|
|
+ })
|
|
|
|
+ this.postForm.status = 'published'
|
|
|
|
+ this.loading = false
|
|
|
|
+ this.listLoading = false
|
|
|
|
+ this.$router.push(`/question/list`)
|
|
|
|
+ })
|
|
|
|
+ },
|
|
|
|
+ draftForm() {
|
|
|
|
+ if (this.postForm.content.length === 0 || this.postForm.title.length === 0) {
|
|
|
|
+ this.$message({
|
|
|
|
+ message: '请填写必要的标题和内容',
|
|
|
|
+ type: 'warning'
|
|
|
|
+ })
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ this.$message({
|
|
|
|
+ message: '保存成功',
|
|
|
|
+ type: 'success',
|
|
|
|
+ showClose: true,
|
|
|
|
+ duration: 1000
|
|
|
|
+ })
|
|
|
|
+ this.postForm.status = 'draft'
|
|
|
|
+ },
|
|
|
|
+ getRemoteUserList(query) {
|
|
|
|
+ searchUser(query).then(response => {
|
|
|
|
+ if (!response.data.items) return
|
|
|
|
+ this.userListOptions = response.data.items.map(v => v.name)
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+</script>
|
|
|
|
+
|
|
|
|
+<style lang="scss" scoped>
|
|
|
|
+@import "~@/styles/mixin.scss";
|
|
|
|
+
|
|
|
|
+.createPost-container {
|
|
|
|
+ position: relative;
|
|
|
|
+
|
|
|
|
+ .createPost-main-container {
|
|
|
|
+ padding: 40px 45px 20px 50px;
|
|
|
|
+
|
|
|
|
+ .postInfo-container {
|
|
|
|
+ position: relative;
|
|
|
|
+ @include clearfix;
|
|
|
|
+ margin-bottom: 10px;
|
|
|
|
+
|
|
|
|
+ .postInfo-container-item {
|
|
|
|
+ float: left;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ .word-counter {
|
|
|
|
+ width: 40px;
|
|
|
|
+ position: absolute;
|
|
|
|
+ right: 10px;
|
|
|
|
+ top: 0px;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+.article-textarea ::v-deep {
|
|
|
|
+ textarea {
|
|
|
|
+ padding-right: 40px;
|
|
|
|
+ resize: none;
|
|
|
|
+ border: none;
|
|
|
|
+ border-radius: 0px;
|
|
|
|
+ border-bottom: 1px solid #bfcbd9;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+</style>
|