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

P
Pan 已提交
5
      <sticky :className="'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 />

P
Pan 已提交
19 20 21 22 23 24 25 26 27 28 29
          <el-col :span="21">
            <el-form-item style="margin-bottom: 40px;" prop="title">
              <MDinput name="name" v-model="postForm.title" required :maxlength="100">
                标题
              </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 32 33
                    <el-select v-model="postForm.author" filterable remote placeholder="搜索用户" :remote-method="getRemoteUserList">
                      <el-option v-for="(item,index) in userListOptions" :key="item+index" :label="item" :value="item">
                      </el-option>
                    </el-select>
P
Pan 已提交
34 35 36 37 38 39 40 41 42
                  </el-form-item>
                </el-col>

                <el-col :span="8">
                  <el-form-item label-width="80px" label="发布时间:" class="postInfo-container-item">
                    <el-date-picker v-model="postForm.display_time" type="datetime" format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
花裤衩 已提交
43 44 45 46 47 48 49 50

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

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

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

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

  </div>
</template>

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

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

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

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