ArticleDetail.vue 8.2 KB
Newer Older
P
Pan 已提交
1 2
<template>
  <div class="createPost-container">
3
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">
P
Pan 已提交
4

5
      <sticky :class-name="'sub-navbar '+postForm.status">
花裤衩 已提交
6 7 8 9 10 11
        <CommentDropdown v-model="postForm.comment_disabled" />
        <PlatformDropdown v-model="postForm.platforms" />
        <SourceUrlDropdown v-model="postForm.source_uri" />
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">发布
        </el-button>
        <el-button v-loading="loading" type="warning" @click="draftForm">草稿</el-button>
P
Pan 已提交
12
      </sticky>
P
Pan 已提交
13 14 15

      <div class="createPost-main-container">
        <el-row>
花裤衩 已提交
16 17 18

          <Warning />

19
          <el-col :span="24">
P
Pan 已提交
20
            <el-form-item style="margin-bottom: 40px;" prop="title">
21
              <MDinput v-model="postForm.title" :maxlength="100" name="name" required>
P
Pan 已提交
22 23 24 25 26 27 28 29
                标题
              </MDinput>
            </el-form-item>

            <div class="postInfo-container">
              <el-row>
                <el-col :span="8">
                  <el-form-item label-width="45px" label="作者:" class="postInfo-container-item">
30 31
                    <el-select v-model="postForm.author" :remote-method="getRemoteUserList" filterable remote placeholder="搜索用户">
                      <el-option v-for="(item,index) in userListOptions" :key="item+index" :label="item" :value="item"/>
花裤衩 已提交
32
                    </el-select>
P
Pan 已提交
33 34 35
                  </el-form-item>
                </el-col>

36
                <el-col :span="10">
P
Pan 已提交
37
                  <el-form-item label-width="80px" label="发布时间:" class="postInfo-container-item">
38
                    <el-date-picker v-model="postForm.display_time" type="datetime" format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"/>
P
Pan 已提交
39 40
                  </el-form-item>
                </el-col>
花裤衩 已提交
41

42
                <el-col :span="6">
花裤衩 已提交
43
                  <el-form-item label-width="60px" label="重要性:" class="postInfo-container-item">
44 45 46 47 48 49 50
                    <el-rate
                      v-model="postForm.importance"
                      :max="3"
                      :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
                      :low-threshold="1"
                      :high-threshold="3"
                      style="margin-top:8px;"/>
花裤衩 已提交
51 52
                  </el-form-item>
                </el-col>
P
Pan 已提交
53 54 55 56 57 58
              </el-row>
            </div>
          </el-col>
        </el-row>

        <el-form-item style="margin-bottom: 40px;" label-width="45px" label="摘要:">
59 60
          <el-input :rows="1" v-model="postForm.content_short" type="textarea" class="article-textarea" autosize placeholder="请输入内容"/>
          <span v-show="contentShortLength" class="word-counter">{{ contentShortLength }}</span>
P
Pan 已提交
61 62
        </el-form-item>

P
Pan 已提交
63 64
        <el-form-item prop="content" style="margin-bottom: 30px;">
          <Tinymce ref="editor" :height="400" v-model="postForm.content" />
65
        </el-form-item>
P
Pan 已提交
66 67 68

        <el-form-item prop="image_uri" style="margin-bottom: 30px;">
          <Upload v-model="postForm.image_uri" />
69
        </el-form-item>
P
Pan 已提交
70 71 72 73 74 75 76
      </div>
    </el-form>

  </div>
</template>

<script>
P
Pan 已提交
77 78 79 80
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
P
Pan 已提交
81
import { validURL } from '@/utils/validate'
P
Pan 已提交
82 83
import { fetchArticle } from '@/api/article'
import { userSearch } from '@/api/remoteSearch'
花裤衩 已提交
84 85
import Warning from './Warning'
import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from './Dropdown'
P
Pan 已提交
86

P
Pan 已提交
87
const defaultForm = {
P
Pan 已提交
88
  status: 'draft',
P
Pan 已提交
89 90 91 92 93 94 95 96
  title: '', // 文章题目
  content: '', // 文章内容
  content_short: '', // 文章摘要
  source_uri: '', // 文章外链
  image_uri: '', // 文章图片
  display_time: undefined, // 前台展示时间
  id: undefined,
  platforms: ['a-platform'],
花裤衩 已提交
97 98
  comment_disabled: false,
  importance: 0
P
Pan 已提交
99 100
}

P
Pan 已提交
101
export default {
102
  name: 'ArticleDetail',
Z
ZYSzys 已提交
103
  components: { Tinymce, MDinput, Upload, Sticky, Warning, CommentDropdown, PlatformDropdown, SourceUrlDropdown },
104 105 106 107 108 109
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
P
Pan 已提交
110 111 112 113 114 115 116
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: rule.field + '为必传项',
          type: 'error'
        })
117
        callback(new Error(rule.field + '为必传项'))
P
Pan 已提交
118 119 120 121 122 123
      } else {
        callback()
      }
    }
    const validateSourceUri = (rule, value, callback) => {
      if (value) {
P
Pan 已提交
124
        if (validURL(value)) {
P
Pan 已提交
125 126
          callback()
        } else {
P
Pan 已提交
127
          this.$message({
P
Pan 已提交
128
            message: '外链url填写不正确',
P
Pan 已提交
129
            type: 'error'
P
Pan 已提交
130
          })
131
          callback(new Error('外链url填写不正确'))
P
Pan 已提交
132
        }
P
Pan 已提交
133 134 135 136 137
      } else {
        callback()
      }
    }
    return {
P
Pan 已提交
138
      postForm: Object.assign({}, defaultForm),
P
Pan 已提交
139
      loading: false,
花裤衩 已提交
140
      userListOptions: [],
P
Pan 已提交
141 142 143 144 145
      rules: {
        image_uri: [{ validator: validateRequire }],
        title: [{ validator: validateRequire }],
        content: [{ validator: validateRequire }],
        source_uri: [{ validator: validateSourceUri, trigger: 'blur' }]
146 147
      },
      tempRoute: {}
P
Pan 已提交
148 149 150 151 152
    }
  },
  computed: {
    contentShortLength() {
      return this.postForm.content_short.length
花裤衩 已提交
153 154 155
    },
    lang() {
      return this.$store.getters.language
P
Pan 已提交
156 157 158 159
    }
  },
  created() {
    if (this.isEdit) {
花裤衩 已提交
160 161
      const id = this.$route.params && this.$route.params.id
      this.fetchData(id)
162 163
    } else {
      this.postForm = Object.assign({}, defaultForm)
P
Pan 已提交
164
    }
165 166 167 168 169

    // 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)
P
Pan 已提交
170
  },
P
Pan 已提交
171
  methods: {
花裤衩 已提交
172 173
    fetchData(id) {
      fetchArticle(id).then(response => {
P
Pan 已提交
174
        this.postForm = response.data
花裤衩 已提交
175 176 177
        // Just for test
        this.postForm.title += `   Article Id:${this.postForm.id}`
        this.postForm.content_short += `   Article Id:${this.postForm.id}`
花裤衩 已提交
178 179 180

        // Set tagsview title
        this.setTagsViewTitle()
P
Pan 已提交
181 182 183
      }).catch(err => {
        console.log(err)
      })
P
Pan 已提交
184
    },
花裤衩 已提交
185 186
    setTagsViewTitle() {
      const title = this.lang === 'zh' ? '编辑文章' : 'Edit Article'
187
      const route = Object.assign({}, this.tempRoute, { title: `${title}-${this.postForm.id}` })
花裤衩 已提交
188 189
      this.$store.dispatch('updateVisitedView', route)
    },
P
Pan 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    submitForm() {
      this.postForm.display_time = parseInt(this.display_time / 1000)
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$notify({
            title: '成功',
            message: '发布文章成功',
            type: 'success',
            duration: 2000
          })
          this.postForm.status = 'published'
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
P
Pan 已提交
207
        }
P
Pan 已提交
208 209 210 211
      })
    },
    draftForm() {
      if (this.postForm.content.length === 0 || this.postForm.title.length === 0) {
P
Pan 已提交
212
        this.$message({
P
Pan 已提交
213 214
          message: '请填写必要的标题和内容',
          type: 'warning'
P
Pan 已提交
215
        })
P
Pan 已提交
216
        return
P
Pan 已提交
217
      }
P
Pan 已提交
218 219 220 221 222 223 224 225 226 227 228
      this.$message({
        message: '保存成功',
        type: 'success',
        showClose: true,
        duration: 1000
      })
      this.postForm.status = 'draft'
    },
    getRemoteUserList(query) {
      userSearch(query).then(response => {
        if (!response.data.items) return
花裤衩 已提交
229
        this.userListOptions = response.data.items.map(v => v.name)
P
Pan 已提交
230
      })
P
Pan 已提交
231
    }
P
Pan 已提交
232
  }
P
Pan 已提交
233
}
P
Pan 已提交
234
</script>
P
Pan 已提交
235

P
Pan 已提交
236
<style rel="stylesheet/scss" lang="scss" scoped>
237
@import "~@/styles/mixin.scss";
花裤衩 已提交
238 239 240 241 242 243 244 245 246 247
.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;
P
Pan 已提交
248
      }
花裤衩 已提交
249
    }
P
Pan 已提交
250
  }
花裤衩 已提交
251 252 253 254 255 256 257
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
P
Pan 已提交
258
</style>