tanghaolin
7 天以前 2d1e485d56fbc736b2156347a0e6d602cd048547
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<template>
    <div class="container mx-auto px-4 py-8 bg-white" v-loading="loading_frm">
        <div class=" mx-auto">
            <!-- 面包屑导航 -->
            <div class="mb-6 text-gray-500">
                <el-breadcrumb separator="/">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item :to="{ path:'/policy-info'}">政策文件</el-breadcrumb-item>
                    <el-breadcrumb-item class="text-ellipsis">{{ newsDetail.title }}</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
 
            <!-- 文章内容 -->
            <div class="article-content">
                <div class="prose max-w-none" v-html="newsDetail.content"></div>
            </div>
 
            <!-- 上一篇/下一篇 -->
            <div class="mt-8 pt-4 border-t">
                <div class="flex flex-col gap-2">
                    <div v-if="newsDetail.prevID"
                        class="text-[22px] line-height-[32px] text-gray-600 hover:text-blue-500 cursor-pointer">
                        上一篇:<span @click="prevNews(newsDetail.prevID)">{{ getPrveTitle(newsDetail.id)
                            }}</span>
                    </div>
                    <div v-if="newsDetail.nextID" class="text-gray-600 hover:text-blue-500 cursor-pointer">
                        下一篇:<span @click="nextNews(newsDetail.nextID)">{{ getNextTitle(newsDetail.id)
                            }}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
 
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { useRoute,useRouter } from 'vue-router';
import axios from 'axios';
import { marked } from 'marked';
 
const route = useRoute();
const router = useRouter();
 
const newsDetail = ref([]);
const newsId = ref(0)
const loading_frm = ref(false)
const m_allData = ref([])
 
onMounted(() => {
    // 这里可以根据路由参数获取新闻详情
    newsId.value = route.params.id;
    initNewsList();
});
const initNewsList = () => {
    loading_frm.value = true
 
    axios({
        method: 'get',
        url: "static/Data/newsData/list.json"
    }).then(res => {
        const result = res.data
        const { LegalRelatedList, StandardDynamicsList } = result
        const allData = LegalRelatedList.concat(StandardDynamicsList)
        const foundNews = allData.find(item => item.id === Number(newsId.value));
        newsDetail.value = foundNews as any
        m_allData.value = allData
        initNewContentByMarked()
    }).catch(err => {
        console.log(err)
        loading_frm.value = false
    })
}
 
const initNewContentByMarked = () => {
    let fileID = newsDetail.value.id
    let filePath = `static/Data/newsData/${fileID}.md`;
    // 读取文件内容并转换为HTML
    fetch(filePath).then(response => {
        return response.text();
    }).then(content => {
        // 使用marked库将Markdown内容转换为HTML
        const htmlContent = marked(content);
        newsDetail.value.content = htmlContent;
        loading_frm.value = false;
    }).catch(error => {
        console.error('Error reading file:', error);
    });
}
 
const getPrveTitle = (id: number) => {
    //根据id获取当前索引
    let index = m_allData.value.findIndex(item => item.id === id)
    if (index > 0) {
        return m_allData.value[index - 1].title
    }
    return ''
}
const prevNews = (id) => {
    router.push({ path: `/news-detail/${id}` })
}
const nextNews = (id) => {
    router.push({ path: `/news-detail/${id}` })
}
 
const getNextTitle = (id: number) => {
    //根据id获取当前索引
    let cur_index = 0
    m_allData.value.forEach((item, index) => {
        if (item.id === id) {
            cur_index = index
        }
    })
    if (cur_index < m_allData.value.length - 1) {
        return m_allData.value[cur_index + 1].title
    }
    else return ''
}
 
</script>
 
<style lang="scss" scoped>
.prose {
    font-size: 16px;
    line-height: 1.75;
    color: #374151;
}
 
.prose h2 {
    margin-top: 2em;
    margin-bottom: 1em;
}
 
.prose p {
    margin-bottom: 1.25em;
}
 
.news-title {
    font-family: Microsoft YaHei;
    font-size: 36px;
    color: #1d1d1f;
    font-weight: bold;
    line-height: 36px;
}
:deep(.el-breadcrumb__inner){
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
 
.news-info {
    height: 16px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    font-size: 16px;
    color: #777777;
    line-height: 32px;
}
</style>