ArticleDetail.vue 8.0 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 63
        </el-form-item>

        <div class="editor-container">
64
          <Tinymce ref="editor" :height="400" v-model="postForm.content" />
P
Pan 已提交
65 66 67
        </div>

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

  </div>
</template>

<script>
P
Pan 已提交
77 78 79 80 81 82 83
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import { validateURL } from '@/utils/validate'
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 124 125 126
      } else {
        callback()
      }
    }
    const validateSourceUri = (rule, value, callback) => {
      if (value) {
        if (validateURL(value)) {
          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' }]
P
Pan 已提交
146
      }
P
Pan 已提交
147 148 149 150 151
    }
  },
  computed: {
    contentShortLength() {
      return this.postForm.content_short.length
花裤衩 已提交
152 153 154
    },
    lang() {
      return this.$store.getters.language
P
Pan 已提交
155 156 157 158
    }
  },
  created() {
    if (this.isEdit) {
花裤衩 已提交
159 160
      const id = this.$route.params && this.$route.params.id
      this.fetchData(id)
161 162
    } else {
      this.postForm = Object.assign({}, defaultForm)
P
Pan 已提交
163 164
    }
  },
P
Pan 已提交
165
  methods: {
花裤衩 已提交
166 167
    fetchData(id) {
      fetchArticle(id).then(response => {
P
Pan 已提交
168
        this.postForm = response.data
花裤衩 已提交
169 170 171
        // Just for test
        this.postForm.title += `   Article Id:${this.postForm.id}`
        this.postForm.content_short += `   Article Id:${this.postForm.id}`
花裤衩 已提交
172 173 174

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

P
Pan 已提交
230
<style rel="stylesheet/scss" lang="scss" scoped>
花裤衩 已提交
231 232 233 234 235 236 237 238 239 240 241
@import "src/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;
P
Pan 已提交
242
      }
花裤衩 已提交
243 244 245 246 247 248 249 250 251
    }
    .editor-container {
      min-height: 500px;
      margin: 0 0 30px;
      .editor-upload-btn-container {
        text-align: right;
        margin-right: 10px;
        .editor-upload-btn {
          display: inline-block;
P
Pan 已提交
252 253 254 255
        }
      }
    }
  }
花裤衩 已提交
256 257 258 259 260 261 262
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
P
Pan 已提交
263
</style>