优化了论坛MarkDown组件

This commit is contained in:
曙光 2024-10-02 12:37:04 +08:00
parent 43767a8fb9
commit 72cdbae057
3 changed files with 216 additions and 6 deletions

View File

@ -123,6 +123,15 @@ export const routes: Array<RouteRecordRaw> = [
access: ACCESS_ENMU.NOT_LOGIN,
icon:IconUserGroup
},
},{
path: "/forum/post/:id",
name: "帖子详细",
component: () => import("../views/forum/PostDetail.vue"),
meta: {
hidden: true,
access: ACCESS_ENMU.NOT_LOGIN,
icon:IconUserGroup
},
},
{
path: "/noauth",

View File

@ -32,16 +32,16 @@
<template #item="{ item }">
<a-list-item class="list-demo-item" action-layout="vertical" style="display: flex;
flex-direction: row;justify-content: center;
align-items: center;">
align-items: center;" @click="PostDetail(item.id)">
<template #actions>
<span><icon-heart />{{ item.thumbNum }}</span>
<span><icon-star />{{ item.favourNum }}</span>
<span><icon-message />收藏</span>
</template>
<template #extra>
<div class="image-area">
<!-- <div class="image-area">
<img alt="arco-design" :src="item.imageSrc || `https://api.miaomc.cn/image/get?${Math.random()}`" />
</div>
</div> -->
</template>
<a-list-item-meta :title="item.title" :description="item.content">
<template #avatar>
@ -85,11 +85,12 @@
import { ref, reactive, onMounted } from 'vue';
import { PostControllerService } from '../../../generated'; //
import MdEditor from '@/components/MdEditor.vue'
import { useRouter } from 'vue-router';
//
const article = ref([]); //
//
const isSidebarHidden = ref(false); //
const router=useRouter()
// /
const toggleSidebar = () => {
isSidebarHidden.value = !isSidebarHidden.value;
@ -106,7 +107,7 @@ const selectedCategory = ref(null); // 当前选中的分类
//
const paginationProps = reactive({
defaultPageSize: 3,
defaultPageSize: 5,
total: 0,
current: 1,
onChange: (page) => fetchData(page), //
@ -133,7 +134,10 @@ const fetchData = async (page = 1) => {
console.error('数据获取失败:', error);
}
};
const PostDetail=(id)=>{
console.log(id)
router.push('/forum/post/'+id)
}
//
onMounted(() => {
fetchData();

View File

@ -0,0 +1,197 @@
<template>
<div class="post-detail-container">
<!-- 返回按钮 -->
<div class="back-button" @click="goBack">
<a-button type="text" icon="arrow-left">返回</a-button>
</div>
<!-- 帖子内容区域 -->
<section class="post-content">
<h2 class="post-title">{{ postDetail.title }}</h2>
<div class="post-info">
<a-avatar :src="postDetail.user?.userAvatar" size="small" />
<span class="author-name">{{ postDetail.user?.userName }}</span>
<span class="post-date">{{ formatDate(postDetail.createTime) }}</span>
</div>
<MdViewer :value="postDetail.content || ''" :mode="mode"></MdViewer>
</section>
<!-- 评论区域 -->
<section class="comments-section">
<h3>评论区</h3>
<a-list :data="comments" :bordered="false" class="comment-list">
<template #item="{ item }">
<a-list-item>
<a-list-item-meta
:title="item.userName"
:description="formatDate(item.createTime)"
>
<template #avatar>
<a-avatar :src="item.userAvatar" />
</template>
</a-list-item-meta>
<div>{{ item.content }}</div>
</a-list-item>
</template>
</a-list>
<!-- 发布评论 -->
<div class="add-comment">
<a-textarea
v-model="newComment"
placeholder="输入您的评论..."
:auto-size="{ minRows: 2, maxRows: 4 }"
/>
<a-button type="primary" @click="submitComment">发布评论</a-button>
</div>
</section>
</div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { PostControllerService } from '../../../generated'; //
import MdViewer from '@/components/MdViewer.vue';
const router = useRouter();
const route = useRoute();
// ID
const postId = route.params.id;
console.log(postId);
//
const postDetail = reactive({
title: '',
content: '',
user: {},
createTime: '',
thumbNum: 0,
favourNum: 0,
});
//
const comments = ref([]);
//
const newComment = ref('');
//
const formatDate = (dateStr) => {
const date = new Date(dateStr);
return date.toLocaleString();
};
//
const fetchPostDetail = async () => {
try {
const res = await PostControllerService.getPostVoByIdUsingGet(postId); //
if (res.code === 0) {
Object.assign(postDetail, res.data);
}
} catch (error) {
console.error('获取帖子详情失败:', error);
}
};
//
const fetchComments = async () => {
try {
const res = await PostControllerService.getCommentsByPostId({ postId }); //
if (res.code === 0) {
comments.value = res.data;
}
} catch (error) {
console.error('获取评论失败:', error);
}
};
//
const submitComment = async () => {
if (!newComment.value.trim()) {
alert('请填写评论内容');
return;
}
try {
const res = await PostControllerService.addCommentUsingPost({
postId,
content: newComment.value,
});
if (res.code === 0) {
alert('评论发布成功');
newComment.value = ''; //
fetchComments(); //
} else {
alert('评论发布失败: ' + res.message);
}
} catch (error) {
console.error('评论发布失败:', error);
}
};
//
onMounted(() => {
fetchPostDetail();
fetchComments();
});
//
const goBack = () => {
router.back();
};
</script>
<style scoped>
.post-detail-container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: var(--background-secondary-color);
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.back-button {
margin-bottom: 10px;
}
.post-title {
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
.post-info {
display: flex;
align-items: center;
gap: 10px;
color: var(--text-secondary-color);
margin-bottom: 20px;
}
.post-body {
padding: 10px;
border: 1px solid #e5e5e5;
border-radius: 4px;
background-color: var(--background-color);
white-space: pre-line;
}
.comments-section {
margin-top: 40px;
}
.comment-list {
margin-bottom: 20px;
}
.add-comment {
display: flex;
gap: 10px;
align-items: flex-start;
}
.add-comment textarea {
flex: 1;
}
</style>