新增加文章发布

This commit is contained in:
不做码农 2022-04-23 21:52:06 +08:00
parent face320c24
commit 763dc329fc
2 changed files with 62 additions and 97 deletions

View File

@ -20,7 +20,7 @@
<el-row :gutter="10" class="mb8"> <el-row :gutter="10" class="mb8">
<el-col :span="1.5"> <el-col :span="1.5">
<!-- <el-button type="primary" plain icon="plus" v-hasPermi="['system:article:add']" @click="handleAdd">发布文章</el-button> --> <el-button type="primary" plain icon="plus" v-hasPermi="['system:article:add']" @click="handleAdd">发布文章</el-button>
</el-col> </el-col>
<right-toolbar :showSearch="showSearch"></right-toolbar> <right-toolbar :showSearch="showSearch"></right-toolbar>
</el-row> </el-row>
@ -44,7 +44,7 @@
<el-table-column label="操作" align="center" width="190"> <el-table-column label="操作" align="center" width="190">
<template #default="scope"> <template #default="scope">
<el-button type="text" icon="view" @click="handleView(scope.row)">查看</el-button> <el-button type="text" icon="view" @click="handleView(scope.row)">查看</el-button>
<!-- <el-button type="text" icon="edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:article:update']">编辑</el-button> --> <el-button type="text" icon="edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:article:update']">编辑</el-button>
<el-popconfirm title="确定删除吗?" @onConfirm="handleDelete(scope.row)" style="margin-left:10px"> <el-popconfirm title="确定删除吗?" @onConfirm="handleDelete(scope.row)" style="margin-left:10px">
<template #reference> <template #reference>
<el-button type="text" icon="delete" v-hasPermi="['system:article:delete']">删除</el-button> <el-button type="text" icon="delete" v-hasPermi="['system:article:delete']">删除</el-button>
@ -59,26 +59,16 @@
</template> </template>
<script setup name="articleindex"> <script setup name="articleindex">
import { listArticle, delArticle } from '@/api/system/article.js' import { listArticle, delArticle } from '@/api/system/article.js'
import { getCurrentInstance, reactive, toRefs } from 'vue-demi'
import { useRoute, useRouter } from 'vue-router'
const { proxy } = getCurrentInstance() const { proxy } = getCurrentInstance()
const router = useRouter() const router = useRouter()
//
const loading = ref(true)
// //
const showSearch = ref(true) const showSearch = ref(true)
//
const title = ref('')
//
const open = ref(false)
// //
const statusOptions = ref([]) const statusOptions = ref([])
// //
const dataList = ref([]) const dataList = ref([])
// //
const total = ref(0) const total = ref(0)
//
const btnSubmitVisible = ref(true)
// //
const previewUrl = ref('') const previewUrl = ref('')
@ -87,7 +77,7 @@ const data = reactive({
queryParams: {}, queryParams: {},
}) })
const { form, queryParams } = toRefs(data) const { queryParams } = toRefs(data)
proxy.getDicts('sys_article_status').then((response) => { proxy.getDicts('sys_article_status').then((response) => {
statusOptions.value = response.data statusOptions.value = response.data

View File

@ -2,69 +2,59 @@
<div class="app-container"> <div class="app-container">
<el-row :gutter="24"> <el-row :gutter="24">
<!-- :model属性用于表单验证使用 比如下面的el-form-item prop属性用于对表单值进行验证操作 --> <!-- :model属性用于表单验证使用 比如下面的el-form-item prop属性用于对表单值进行验证操作 -->
<el-form :model="form" ref="form" label-width="100px" :rules="rules" @submit.prevent> <el-form :model="form" ref="formRef" label-width="100px" :rules="rules" @submit.prevent>
<el-col :lg="12"> <el-form-item label="文章标题" prop="title">
<el-form-item label="文章标题" prop="title"> <el-input v-model="form.title" placeholder="请输入文章标题(必须)" />
<el-input v-model="form.title" placeholder="请输入文章标题(必须)" /> </el-form-item>
</el-form-item>
</el-col> <el-form-item label="文章分类" prop="category_id">
<el-col :lg="12"> <el-cascader
<el-form-item label="文章分类" prop="category_id"> class="w100"
<!-- <treeselect v-model="form.category_id" :options="categoryOptions" :normalizer="normalizer" :show-count="true" /> --> :options="categoryOptions"
<tree-select v-model:value="form.category_id" :options="categoryOptions" :props="{ checkStrictly: true, value: 'category_Id', label: 'name', emitPath: false }"
:objMap="{ value: 'category_id', label: 'name', children: 'children' }" /> placeholder="请选择文章分类"
</el-form-item> clearable
</el-col> v-model="form.category_Id"
/>
</el-form-item>
<el-form-item label="文章标签">
<el-tag v-for="tag in form.dynamicTags" :key="tag" class="mr10" closable :disable-transitions="false" @close="handleCloseTag(tag)">
{{ tag }}
</el-tag>
<el-input v-if="inputVisible" ref="inputRef" v-model="inputValue" class="w20" @keyup.enter="handleInputConfirm" @blur="handleInputConfirm" />
<el-button v-else class="button-new-tag" size="small" @click="showInput">+ 文章标签</el-button>
</el-form-item>
<el-form-item prop="content" label="文章内容">
<MdEditor v-model="form.content" :onUploadImg="onUploadImg" />
</el-form-item>
<el-col :lg="24"> <el-col :lg="24">
<el-form-item label="文章标签"> <el-form-item style="text-align: right">
<el-tag size="large" :key="tag" v-for="tag in form.dynamicTags" closable :disable-transitions="false" @close="handleCloseTag(tag)">
{{tag}}
</el-tag>
<el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small" @keyup.enter="handleInputConfirm"
@blur="handleInputConfirm">
</el-input>
<el-button v-else class="button-new-tag" size="small" @click="showInput">+ 文章标签</el-button>
</el-form-item>
</el-col>
<el-col :lg="24">
<el-form-item prop="content" label="文章内容">
<!-- <mavon-editor v-model="form.content" ref=md /> -->
</el-form-item>
</el-col>
<el-col :lg="24">
<el-form-item label="" style="text-align:right;">
<el-button @click="handlePublish('1')">发布文章</el-button> <el-button @click="handlePublish('1')">发布文章</el-button>
<el-button type="success" @click="handlePublish('2')">存为草稿</el-button> <el-button type="success" @click="handlePublish('2')">存为草稿</el-button>
</el-form-item> </el-form-item>
</el-col> </el-col>
</el-form> </el-form>
</el-row> </el-row>
</div> </div>
</template> </template>
<script setup name="articlepublish"> <script setup name="articlepublish">
import { import { addArticle, updateArticle, listArticleCategoryTree, getArticle } from '@/api/system/article.js'
addArticle,
updateArticle,
listArticleCategoryTree,
getArticle,
} from '@/api/system/article.js'
import { upload } from '@/api/common.js' import { upload } from '@/api/common.js'
// import mavonEditor from 'mavon-editor' import MdEditor from 'md-editor-v3';
// import 'mavon-editor/dist/css/index.css' import 'md-editor-v3/lib/style.css'
import { getCurrentInstance, reactive, toRefs, watch, nextTick } from 'vue-demi'
import { useRoute } from 'vue-router'
const { proxy } = getCurrentInstance() const { proxy } = getCurrentInstance()
const route = useRoute() const route = useRoute()
// //
const categoryOptions = ref([]) const categoryOptions = ref([])
//
const btnSubmitVisible = ref(true)
const inputVisible = ref(false) const inputVisible = ref(false)
const inputValue = ref('') const inputValue = ref('')
const html = ref('') const formRef = ref()
const inputRef = ref()
const data = reactive({ const data = reactive({
form: { form: {
dynamicTags: [], dynamicTags: [],
@ -95,18 +85,21 @@ function getCategoryTreeselect() {
} }
// md // md
function $imgAdd(pos, $file) { async function onUploadImg(files, callback) {
var formdata = new FormData() const res = await Promise.all(
formdata.append('file', $file) Array.from(files).map((file) => {
upload(formdata).then((res) => { return new Promise((rev, rej) => {
console.log(JSON.stringify(res)) const form = new FormData()
proxy.$refs.md.$img2Url(pos, res.data.url) form.append('file', file)
})
}
function change(value, render) { upload(form)
// render markdown .then((res) => rev(res))
html.value = render .catch((error) => rej(error))
})
}),
)
callback(res.map((item) => item.data.url))
} }
/** 提交按钮 */ /** 提交按钮 */
@ -114,7 +107,7 @@ function handlePublish(status) {
form.value.status = status form.value.status = status
form.value.tags = form.value.dynamicTags.toString() form.value.tags = form.value.dynamicTags.toString()
proxy.$refs['form'].validate((valid) => { proxy.$refs['formRef'].validate((valid) => {
if (valid) { if (valid) {
if (form.value.cid != undefined) { if (form.value.cid != undefined) {
updateArticle(form.value).then((res) => { updateArticle(form.value).then((res) => {
@ -142,27 +135,21 @@ function handleCloseTag(tag) {
form.value.dynamicTags.splice(form.value.dynamicTags.indexOf(tag), 1) form.value.dynamicTags.splice(form.value.dynamicTags.indexOf(tag), 1)
} }
function showInput() { const showInput = () => {
inputVisible.value = true inputVisible.value = true
proxy.nextTick((_) => { nextTick(() => {
proxy.$refs.saveTagInput.$refs.input.focus() inputRef.value.input.focus()
}) })
} }
//
function handleInputConfirm() { function handleInputConfirm() {
let inputValue = form.value.inputValue.trim() if (inputValue.value) {
form.value.dynamicTags.push(inputValue.value)
if (
inputValue &&
inputValue.length > 0 &&
form.value.dynamicTags.length < 4
) {
form.value.dynamicTags.push(inputValue)
} }
inputVisible.value = false inputVisible.value = false
inputValue.value = '' inputValue.value = ''
} }
getInfo(cid) getInfo(cid)
function handleTreeSelect() {}
function getInfo(cid) { function getInfo(cid) {
if (!cid || cid == undefined) return if (!cid || cid == undefined) return
getArticle(cid).then((res) => { getArticle(cid).then((res) => {
@ -173,9 +160,8 @@ function getInfo(cid) {
cid: parseInt(cid), cid: parseInt(cid),
title: data.title, title: data.title,
content: data.content, content: data.content,
category_id: data.category_id, category_Id: data.category_Id,
dynamicTags: dynamicTags: data.tags != null && data.tags.length > 0 ? data.tags.split(',') : [],
data.tags != null && data.tags.length > 0 ? data.tags.split(',') : [],
} }
} }
}) })
@ -183,28 +169,17 @@ function getInfo(cid) {
getCategoryTreeselect() getCategoryTreeselect()
</script> </script>
<style scoped> <style scoped>
.el-tag + .el-tag {
margin-right: 10px;
}
.el-tag {
margin-right: 10px;
}
.button-new-tag { .button-new-tag {
/* margin-left: 10px; */ /* margin-left: 10px; */
height: 32px; height: 32px;
line-height: 30px; line-height: 30px;
padding-top: 0; padding-top: 0;
padding-bottom: 0; padding-bottom: 0;
}
.input-new-tag {
width: 90px; width: 90px;
margin-right: 10px; margin-right: 10px;
vertical-align: bottom; vertical-align: bottom;
} }
.el-col {
border-radius: 4px;
margin-bottom: 10px;
}
.vue-treeselect { .vue-treeselect {
z-index: 1501; z-index: 1501;
} }