ArticleDetail.vue 7.8 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
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
P
Pan 已提交
80 81
import Multiselect from 'vue-multiselect'// 使用的一个多选框组件,element-ui的select不能满足所有需求
import 'vue-multiselect/dist/vue-multiselect.min.css'// 多选框组件css
P
Pan 已提交
82 83 84 85
import Sticky from '@/components/Sticky' // 粘性header组件
import { validateURL } from '@/utils/validate'
import { fetchArticle } from '@/api/article'
import { userSearch } from '@/api/remoteSearch'
花裤衩 已提交
86 87
import Warning from './Warning'
import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from './Dropdown'
P
Pan 已提交
88

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

P
Pan 已提交
103
export default {
104
  name: 'ArticleDetail',
花裤衩 已提交
105
  components: { Tinymce, MDinput, Upload, Multiselect, Sticky, Warning, CommentDropdown, PlatformDropdown, SourceUrlDropdown },
106 107 108 109 110 111
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
P
Pan 已提交
112 113 114 115 116 117 118
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: rule.field + '为必传项',
          type: 'error'
        })
119
        callback(new Error(rule.field + '为必传项'))
P
Pan 已提交
120 121 122 123 124 125 126 127 128
      } else {
        callback()
      }
    }
    const validateSourceUri = (rule, value, callback) => {
      if (value) {
        if (validateURL(value)) {
          callback()
        } else {
P
Pan 已提交
129
          this.$message({
P
Pan 已提交
130
            message: '外链url填写不正确',
P
Pan 已提交
131
            type: 'error'
P
Pan 已提交
132
          })
133
          callback(new Error('外链url填写不正确'))
P
Pan 已提交
134
        }
P
Pan 已提交
135 136 137 138 139
      } else {
        callback()
      }
    }
    return {
P
Pan 已提交
140
      postForm: Object.assign({}, defaultForm),
P
Pan 已提交
141
      loading: false,
花裤衩 已提交
142
      userListOptions: [],
P
Pan 已提交
143 144 145 146 147
      rules: {
        image_uri: [{ validator: validateRequire }],
        title: [{ validator: validateRequire }],
        content: [{ validator: validateRequire }],
        source_uri: [{ validator: validateSourceUri, trigger: 'blur' }]
P
Pan 已提交
148
      }
P
Pan 已提交
149 150 151 152 153 154 155 156 157
    }
  },
  computed: {
    contentShortLength() {
      return this.postForm.content_short.length
    }
  },
  created() {
    if (this.isEdit) {
花裤衩 已提交
158 159
      const id = this.$route.params && this.$route.params.id
      this.fetchData(id)
160 161
    } else {
      this.postForm = Object.assign({}, defaultForm)
P
Pan 已提交
162 163
    }
  },
P
Pan 已提交
164
  methods: {
花裤衩 已提交
165 166
    fetchData(id) {
      fetchArticle(id).then(response => {
P
Pan 已提交
167
        this.postForm = response.data
花裤衩 已提交
168 169 170
        // Just for test
        this.postForm.title += `   Article Id:${this.postForm.id}`
        this.postForm.content_short += `   Article Id:${this.postForm.id}`
P
Pan 已提交
171 172 173
      }).catch(err => {
        console.log(err)
      })
P
Pan 已提交
174
    },
P
Pan 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    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 已提交
192
        }
P
Pan 已提交
193 194 195 196
      })
    },
    draftForm() {
      if (this.postForm.content.length === 0 || this.postForm.title.length === 0) {
P
Pan 已提交
197
        this.$message({
P
Pan 已提交
198 199
          message: '请填写必要的标题和内容',
          type: 'warning'
P
Pan 已提交
200
        })
P
Pan 已提交
201
        return
P
Pan 已提交
202
      }
P
Pan 已提交
203 204 205 206 207 208 209 210 211 212 213
      this.$message({
        message: '保存成功',
        type: 'success',
        showClose: true,
        duration: 1000
      })
      this.postForm.status = 'draft'
    },
    getRemoteUserList(query) {
      userSearch(query).then(response => {
        if (!response.data.items) return
花裤衩 已提交
214
        this.userListOptions = response.data.items.map(v => v.name)
P
Pan 已提交
215
      })
P
Pan 已提交
216
    }
P
Pan 已提交
217
  }
P
Pan 已提交
218
}
P
Pan 已提交
219
</script>
P
Pan 已提交
220

P
Pan 已提交
221
<style rel="stylesheet/scss" lang="scss" scoped>
花裤衩 已提交
222 223 224 225 226 227 228 229 230 231 232
@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 已提交
233
      }
花裤衩 已提交
234 235 236 237 238 239 240 241 242
    }
    .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 已提交
243 244 245 246
        }
      }
    }
  }
花裤衩 已提交
247 248 249 250 251 252 253
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
P
Pan 已提交
254
</style>