You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

355 lines
12 KiB

<template>
<el-dialog :visible.sync="visible"
:title="!dataForm.id ? $t('add') : $t('update')"
:close-on-click-modal="false"
:close-on-press-escape="false"
@close="visible= false">
<el-form :model="dataForm"
:rules="dataRule"
ref="dataForm"
:label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
<el-form-item label="所属机构">
<el-cascader v-model="dataForm.allDeptIdsShow"
:options="options"
:props="{ multiple: true }"
clearable
collapse-tags></el-cascader>
</el-form-item>
<el-form-item label="类别"
prop="newsCateroryId">
<el-select v-model="dataForm.newsCateroryId"
@change="changeCaterory"
placeholder="所属网格">
<el-option v-for="item in categorys"
:key="item.id"
:label="item.name"
:value="item.id">
</el-option>
</el-select>
</el-form-item>
<el-form-item label="新闻标题"
prop="newsTitle">
<el-form-item>
<el-input v-model="dataForm.newsTitle"
maxlength="50"
show-word-limit
placeholder="请输入标题"
clearable></el-input>
</el-form-item>
</el-form-item>
<el-form-item prop="newsContent"
label="正文编辑">
<!-- 富文本编辑器, 容器 -->
<div id="J_quillEditor"></div>
<!-- 自定义上传图片功能 (使用element upload组件) -->
<el-upload :action="uploadUrl"
:show-file-list="false"
:before-upload="uploadBeforeUploadHandle"
:on-success="uploadSuccessHandle"
style="display: none;">
<el-button ref="uploadBtn"
type="primary"
size="small">{{ $t('upload.button') }}</el-button>
</el-upload>
</el-form-item>
<el-form-item label="新闻首图"
v-loading="loading"
prop="newsImageUrl">
<el-upload class="avatar-uploader"
:action="uploadUrl"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload">
<img v-if="dataForm.newsImageUrl"
:src="dataForm.newsImageUrl"
class="avatar">
<i v-else
class="el-icon-plus avatar-uploader-icon"></i>
<div slot="tip"
class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
</el-form-item>
<el-form-item label="显示时长"
prop="newsReleaseStartTime">
<el-date-picker @change='setRegistTime'
v-model="time"
value-format="yyyy-MM-dd"
type="daterange"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期">
</el-date-picker>
</el-form-item>
<el-form-item label="是否上线" prop="newsUpDownState">
<el-radio-group v-model="dataForm.newsUpDownState">
<el-radio :label="'0'">上线</el-radio>
<el-radio :label="'1'">下线</el-radio>
</el-radio-group>
</el-form-item>
</el-form>
<template slot="footer">
<el-button @click="visible = false">{{ $t('cancel') }}</el-button>
<el-button type="primary"
@click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
</template>
</el-dialog>
</template>
<script>
import Cookies from 'js-cookie'
import debounce from 'lodash/debounce'
import 'quill/dist/quill.snow.css'
import Quill from 'quill'
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './news-add-or-update'
export default {
mixins: [mixinViewModule],
data () {
return {
mixinViewModuleOptions: {
getDataListURL: '/news/news/page',
getDataListIsPage: true,
deleteURL: '/news/news',
deleteIsBatch: true
},
dataForm: {
id: '',
newsCateroryId: '',
newsProperty: '',
newsTitle: '',
newsReleaseStartTime: '',
newsReleaseEndTime: '',
newsContent: '',
newsImageUrl: '',
sectionCode: '',
allDeptIdsShow: [],
newsUpDownState: ''
},
newsUpDownStateOld: '',
time: [],
options: [],
categorys: [],
loading: false,
// 富文本
quillEditor: null,
quillEditorToolbarOptions: [
['bold', 'italic', 'underline', 'strike'],
['blockquote', 'code-block', 'image'],
[{ 'header': 1 }, { 'header': 2 }],
[{ 'list': 'ordered' }, { 'list': 'bullet' }],
[{ 'script': 'sub' }, { 'script': 'super' }],
[{ 'indent': '-1' }, { 'indent': '+1' }],
[{ 'direction': 'rtl' }],
[{ 'size': ['small', false, 'large', 'huge'] }],
[{ 'header': [1, 2, 3, 4, 5, 6, false] }],
[{ 'color': [] }, { 'background': [] }],
[{ 'font': [] }],
[{ 'align': [] }],
['clean']
],
uploadUrl: '',
// 富文本end
fileList: [],
visible: false
}
},
created () {
this.$http
.get(`/sys/user/deptOptions/getAllByLoginUser`)
.then(({ data: res }) => {
if (res.code !== 0) {
return this.$message.error(res.msg)
}
this.options = res.data.options
})
.catch(() => { })
this.visible = true
this.$nextTick(() => {
this.dataForm.communityId = this.dataForm.streetId = this.dataForm.gridId = null
// 富文本
this.hideUpload = false
this.uploadUrl = `${window.SITE_CONFIG['apiURL']}/oss/file/upload?token=${Cookies.get('token')}`
if (this.quillEditor) {
this.quillEditor.deleteText(0, this.quillEditor.getLength())
} else {
this.quillEditorHandle()
}
// 富文本end
})
},
computed: {
dataRule () {
return {
streetId: [
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
],
newsTitle: [
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
],
newsReleaseStartTime: [
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
],
newsImageUrl: [
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
],
newsCateroryId: [
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
],
newsContent: [
{ required: true, message: this.$t('validate.required'), trigger: 'blur' }
]
}
}
},
methods: {
init () {
this.visible = true
this.$nextTick(() => {
this.$refs['dataForm'].resetFields()
if (this.dataForm.id) {
this.getInfo()
}
})
},
// 获取信息
getInfo () {
this.$http.get(`/news/news/${this.dataForm.id}`).then(({ data: res }) => {
if (res.code !== 0) {
return this.$message.error(res.msg)
}
this.dataForm = { ...this.dataForm, ...res.data }
this.newsUpDownStateOld = this.dataForm.newsUpDownState
if (new Date(this.dataForm.newsReleaseEndTime) < new Date()) {
this.dataForm.newsUpDownState = '1'
}
this.getListCategory(this.dataForm.sectionCode)
this.resetRegistTime()
this.quillEditor.root.innerHTML = res.data.newsContent
}).catch(() => { })
},
changeCaterory () {
// 给新闻类别变量赋值
let choosenItem = this.categorys.filter(item => item.id === this.dataForm.newsCateroryId)[0]
this.dataForm.newsProperty = choosenItem.name
},
getListCategory (sectionCode) {
this.$http.get(`/news/newscategory/category/${sectionCode}`).then(({ data: res }) => {
if (res.code !== 0) {
return this.$message.error(res.msg)
}
this.categorys = res.data
}).catch(() => { })
},
// 富文本编辑器
quillEditorHandle () {
this.quillEditor = new Quill('#J_quillEditor', {
modules: {
toolbar: this.quillEditorToolbarOptions
},
theme: 'snow'
})
this.quillEditor.container.style.height = `${300}px`
// // 自定义上传图片功能 (使用element upload组件)
this.quillEditor.getModule('toolbar').addHandler('image', () => {
this.$refs.uploadBtn.$el.click()
})
// 监听内容变化,动态赋值
this.quillEditor.on('text-change', () => {
this.dataForm.newsContent = this.quillEditor.root.innerHTML
if ((this.dataForm.newsContent).length > 10000) {
return this.$message.error('您输入的的内容已超过字数不能提交')
}
})
},
// 上传图片之前 (富文本)
uploadBeforeUploadHandle (file) {
if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
this.$message.error(this.$t('upload.tip', { 'format': 'jpg、png、gif' }))
return false
}
},
// 上传图片成功 (富文本)
uploadSuccessHandle (res, file, fileList) {
if (res.code !== 0) {
return this.$message.error(res.msg)
}
this.quillEditor.insertEmbed(this.quillEditor.getSelection().index, 'image', res.data.url)
},
// 上传图片
handleAvatarSuccess (res, file) {
this.loading = false
this.dataForm.newsImageUrl = res.data.url
},
beforeAvatarUpload (file) {
this.loading = true
},
setRegistTime () {
this.dataForm.newsReleaseStartTime = this.time[0]
this.dataForm.newsReleaseEndTime = this.time[1]
if (new Date(this.dataForm.newsReleaseEndTime) < new Date()) {
this.dataForm.newsUpDownState = '1'
} else if (this.newsUpDownStateOld === '0') {
this.dataForm.newsUpDownState = '0'
}
},
resetRegistTime () {
this.time = [this.dataForm.newsReleaseStartTime.substr(0, 10), this.dataForm.newsReleaseEndTime.substr(0, 10)]
},
// 上传图片end
// 表单提交
dataFormSubmitHandle: debounce(function () {
if ((this.dataForm.newsContent).length > 10000) {
return this.$message.error('您输入的的内容已超过字数不能提交')
}
this.$refs['dataForm'].validate((valid) => {
if (!valid) {
return false
}
this.$http['put']('/news/news/', this.dataForm).then(({ data: res }) => {
if (res.code !== 0) {
return this.$message.error(res.msg)
}
this.$message({
message: this.$t('prompt.success'),
type: 'success',
duration: 500,
onClose: () => {
this.visible = false
this.$emit('refreshDataList')
this.dataForm = {}
}
})
}).catch(() => { })
})
}, 1000, { 'leading': true, 'trailing': false })
},
components: {
AddOrUpdate
}
}
</script>
<style>
.avatar-uploader .el-upload {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
.avatar-uploader .el-upload:hover {
border-color: #409eff;
}
.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
line-height: 178px;
text-align: center;
}
.avatar {
width: 178px;
height: 178px;
display: block;
}
</style>