123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <template>
- <div class="createPost-container">
- <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">
- <div class="createPost-main-container">
- <el-row>
- <Warning />
- <el-col :span="24">
- <el-form-item style="margin-bottom: 40px;" prop="title">
- <MDinput v-model="postForm.title" :maxlength="50" name="name" required>
- 文章标题
- </MDinput>
- </el-form-item>
- <div class="postInfo-container">
- <div style="margin-bottom: 10px;">封面图</div>
- <el-form-item prop="cover_img" style="margin-bottom: 30px;" label="">
- <Upload v-model="postForm.cover_img" />
- </el-form-item>
- </div>
- </el-col>
- </el-row>
- <el-form-item prop="content" style="margin-bottom: 30px;">
- <Tinymce ref="editor" v-model="postForm.content" :height="400" />
- </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 Upload from '@/components/Upload/SingleImage3'
- import MDinput from '@/components/MDinput'
- // import Sticky from '@/components/Sticky' // 粘性header组件
- // import { validURL } from '@/utils/validate'
- import { fetchArticle, createArticle, updateArticle } from '@/api/article'
- import { searchUser } from '@/api/remote-search'
- import Warning from './Warning'
- // 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, MDinput, Warning, Upload },
- 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: {
- cover_img: [{ message: '封面图不为空', validator: validateRequire }],
- title: [{ message: '标题不为空', validator: validateRequire }],
- content: [{ message: '内容不为空', validator: validateRequire }]
- },
- tempRoute: {}
- }
- },
- 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) {
- fetchArticle(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)
- createArticle(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(`/article/list`)
- })
- } else {
- console.log('error submit!!')
- return false
- }
- })
- },
- updateArticle() {
- console.log(this.postForm)
- updateArticle(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(`/article/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>
|