Files
Yi.Admin/Yi.BBS.Vue3/src/views/EditArticle.vue

211 lines
6.1 KiB
Vue
Raw Normal View History

2023-03-12 01:50:11 +08:00
<template>
2023-03-12 19:49:08 +08:00
<div style="width: 100%">
2023-03-12 01:50:11 +08:00
<div class="body-div">
2023-03-12 19:49:08 +08:00
<el-form
label-width="120px"
:model="editForm"
label-position="left"
:rules="rules"
ref="ruleFormRef"
>
2023-03-12 01:50:11 +08:00
<el-form-item label="分类:">
<el-radio-group v-model="radio">
2023-03-12 19:49:08 +08:00
<el-radio-button label="discuss">主题</el-radio-button>
<el-radio-button label="article">文章</el-radio-button>
<el-radio-button label="plate">板块</el-radio-button>
<el-radio-button label="orther">其他</el-radio-button>
2023-03-12 01:50:11 +08:00
</el-radio-group>
</el-form-item>
2023-03-12 19:49:08 +08:00
<el-form-item
v-if="route.query.artType == 'article'"
label="名称:"
prop="name"
>
<el-input placeholder="请输入" v-model="editForm.name" />
</el-form-item>
2023-03-14 22:58:35 +08:00
<el-form-item v-else label="标题:" prop="title">
2023-03-12 19:49:08 +08:00
<el-input placeholder="请输入" v-model="editForm.title" />
2023-03-12 01:50:11 +08:00
</el-form-item>
<el-form-item label="描述:" prop="introduction">
2023-03-12 19:49:08 +08:00
<el-input placeholder="请输入" v-model="editForm.introduction" />
2023-03-12 01:50:11 +08:00
</el-form-item>
<el-form-item label="内容:" prop="content">
2023-03-12 19:49:08 +08:00
<MavonEdit
height="30rem"
v-model="editForm.content"
:codeStyle="codeStyle"
/>
2023-03-12 01:50:11 +08:00
</el-form-item>
<el-form-item label="封面:">
<el-input placeholder="请输入" />
</el-form-item>
<el-form-item label="标签:" prop="types">
2023-03-12 19:49:08 +08:00
<el-input placeholder="请输入" v-model="editForm.types" />
2023-03-12 01:50:11 +08:00
</el-form-item>
2023-03-12 19:49:08 +08:00
<el-form-item>
<el-button
@click="submit(ruleFormRef)"
class="submit-btn"
type="primary"
>提交</el-button
></el-form-item
>
2023-03-12 01:50:11 +08:00
</el-form>
</div>
</div>
</template>
<script setup>
2023-03-12 19:49:08 +08:00
import MavonEdit from "@/components/MavonEdit.vue";
import { ref, reactive, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
add as discussAdd,
update as discussUpdate,
get as discussGet,
} from "@/apis/discussApi.js";
2023-03-12 01:50:11 +08:00
2023-03-12 19:49:08 +08:00
import {
2023-03-14 22:58:35 +08:00
add as articleAdd,
update as articleUpdate,
get as articleGet,
2023-03-12 19:49:08 +08:00
} from "@/apis/articleApi.js";
2023-03-12 01:50:11 +08:00
//数据定义
2023-03-12 19:49:08 +08:00
const route = useRoute();
const router = useRouter();
const radio = ref(route.query.artType);
const codeStyle = "atom-one-dark";
//整个页面上的表单
const editForm = reactive({
2023-03-12 01:50:11 +08:00
title: "",
types: "",
introduction: "",
content: "",
2023-03-12 19:49:08 +08:00
name: ""
2023-03-12 01:50:11 +08:00
});
2023-03-12 19:49:08 +08:00
//组装主题内容: 需要更新主题信息
const discuss = {
};
//组装文章内容:需要添加的文章信息
const article = {
};
2023-03-12 01:50:11 +08:00
//定义效验规则
2023-03-12 19:49:08 +08:00
const ruleFormRef = ref(null);
2023-03-12 01:50:11 +08:00
const rules = reactive({
title: [
2023-03-12 19:49:08 +08:00
{ required: true, message: "请输入标题", trigger: "blur" },
{ min: 3, max: 20, message: "长度 3 到 20", trigger: "blur" },
2023-03-12 01:50:11 +08:00
],
2023-03-12 19:49:08 +08:00
content: [
{ required: true, message: "请输入内容", trigger: "blur" },
{ min: 10, max: 4000, message: "长度 10 到4000", trigger: "blur" },
],
});
2023-03-12 01:50:11 +08:00
//提交按钮,需要区分操作类型
const submit = async (formEl) => {
2023-03-12 19:49:08 +08:00
if (!formEl) return;
2023-03-12 01:50:11 +08:00
await formEl.validate(async (valid, fields) => {
if (valid) {
2023-03-12 19:49:08 +08:00
//dicuss主题处理
if (route.query.artType == "discuss") {
2023-03-12 01:50:11 +08:00
2023-03-12 19:49:08 +08:00
discuss.title=editForm.title;
discuss.types= editForm.types;
discuss.introduction= editForm.introduction;
discuss.content= editForm.content;
discuss.plateId= discuss.plateId??route.query.plateId
//主题创建
if (route.query.operType == "create") {
const response= await discussAdd(discuss);
var routerPer = { path: `/article/${response.id}` };
router.push(routerPer);
2023-03-12 19:49:08 +08:00
}
//主题更新
else if (route.query.operType == "update") {
await discussUpdate(route.query.discussId, discuss);
var routerPer = { path: `/article/${route.query.discussId}` };
router.push(routerPer);
2023-03-12 19:49:08 +08:00
}
2023-03-12 01:50:11 +08:00
}
2023-03-12 19:49:08 +08:00
//artcle文章处理
else if (route.query.artType == "article") {
//组装文章内容:需要添加的文章信息
2023-03-14 22:58:35 +08:00
article.content= editForm.content;
2023-03-12 19:49:08 +08:00
article.name= editForm.name;
article.discussId=route.query.discussId;
2023-03-14 22:58:35 +08:00
article.parentId=route.query.parentArticleId
2023-03-12 19:49:08 +08:00
//文章创建
if (route.query.operType == "create") {
const response= await articleAdd(article);
var routerPer = { path: `/article/${route.query.discussId}/${response.id}` };
router.push(routerPer);
2023-03-12 19:49:08 +08:00
}
//文章更新
else if (route.query.operType == "update") {
await articleUpdate(route.query.articleId, article);
var routerPer = { path: `/article/${route.query.discussId}/${route.query.articleId}` };
router.push(routerPer);
2023-03-12 19:49:08 +08:00
}
2023-03-12 01:50:11 +08:00
}
2023-03-12 19:49:08 +08:00
//添加成功后跳转到该页面
// var routerPer = { path: `/discuss/${discuss.plateId}` };
// router.push(routerPer);
2023-03-12 19:49:08 +08:00
// ruleFormRef.value.resetFields();
// discuss.plateId = route.query.plateId;
2023-03-12 01:50:11 +08:00
}
2023-03-12 19:49:08 +08:00
});
};
2023-03-12 01:50:11 +08:00
2023-03-12 19:49:08 +08:00
onMounted(async () => {
2023-03-12 01:50:11 +08:00
//如果是更新操作,需要先查询
2023-03-12 19:49:08 +08:00
if (route.query.operType == "update") {
//更新主题
if (route.query.artType == "discuss") {
await loadDiscuss();
2023-03-12 01:50:11 +08:00
2023-03-12 19:49:08 +08:00
//更新文章
2023-03-14 22:58:35 +08:00
} else if (route.query.artType == "article") {
await loadArticle();
2023-03-12 19:49:08 +08:00
}
}
});
//加载主题
const loadDiscuss = async () => {
const response = await discussGet(route.query.discussId);
editForm.content = response.content;
editForm.title = response.title;
editForm.types = response.types;
editForm.introduction = response.introduction;
discuss.plateId=response.plateId;
};
//加载文章
2023-03-14 22:58:35 +08:00
const loadArticle = async () => {
const response = await articleGet(route.query.articleId);
2023-03-12 19:49:08 +08:00
editForm.content = response.content;
editForm.name = response.name;
2023-03-14 22:58:35 +08:00
editForm.discussId = response.discussId;
2023-03-12 19:49:08 +08:00
};
2023-03-12 01:50:11 +08:00
</script>
<style scoped>
.submit-btn {
width: 40%;
}
.body-div {
min-height: 1000px;
background-color: #fff;
margin: 1.5rem;
padding: 1.5rem;
}
</style>