This commit is contained in:
WindowBird 2025-10-17 15:25:14 +08:00
parent be75c9effd
commit ad3a166f4f
4 changed files with 129 additions and 20 deletions

View File

@ -61,7 +61,10 @@ const loadRecommendedArticles = async () => {
//
onMounted(() => {
loadRecommendedArticles()
//
if (typeof window !== 'undefined') {
loadRecommendedArticles()
}
})
</script>

View File

@ -22,7 +22,24 @@ interface Props {
error: string
}
const props = defineProps<Props>()
const props = withDefaults(defineProps<Props>(), {
articleData: () => ({
title: '',
publishDate: '',
category: '',
content: '',
prevArticle: {
title: '',
url: '#'
},
nextArticle: {
title: '',
url: '#'
}
}),
loading: true,
error: ''
})
// 使
const articleData = computed(() => props.articleData)
@ -44,7 +61,7 @@ const reloadPage = () => {
// 使 composable
const {initImageStyles} = useImageStyles(
() => props.articleData.content,
() => props.articleData?.content || '',
'.article-content',
{
maxWidth: '100%',
@ -55,17 +72,22 @@ const {initImageStyles} = useImageStyles(
)
onMounted(() => {
// CSS
loadCSSFiles()
//
if (typeof window !== 'undefined') {
// CSS
loadCSSFiles()
// JavaScript
loadJSFiles()
// JavaScript
loadJSFiles()
}
})
//
initImageStyles()
const loadJSFiles = () => {
if (typeof document === 'undefined') return
const jsFiles = [
'/news/jquery.1.11.3.min.js',
'/news/bootstrap.min.js',
@ -85,6 +107,8 @@ const loadJSFiles = () => {
}
const loadCSSFiles = () => {
if (typeof document === 'undefined') return
const cssFiles = [
'/news/bootstrap.min.css',
'/news/main22.css',
@ -189,22 +213,22 @@ const loadCSSFiles = () => {
<!-- 文章内容 -->
<div v-else class="row">
<div class="col-xs-12">
<h2>{{ articleData.title }}</h2>
<h2>{{ articleData?.title || '暂无标题' }}</h2>
<p id="label" style="border-bottom: 1px solid #ddd; margin-top: 20px;">
<span>{{ articleData.publishDate }}</span>
<span class="pull-right">分类{{ articleData.category }}</span>
<span>{{ articleData?.publishDate || '暂无日期' }}</span>
<span class="pull-right">分类{{ articleData?.category || '暂无分类' }}</span>
</p>
<div class="article-content" v-html="articleData.content"/>
<div class="article-content" v-html="articleData?.content || '暂无内容'"/>
</div>
</div>
<!-- 上一篇/下一篇 -->
<div class="nextorpre" style="margin-top: 38px; border-top: 1px solid #ddd; padding-top: 8px;">
<li class="pull-left">上一篇
<a :href="articleData.prevArticle.url">{{ articleData.prevArticle.title }}</a>
<a :href="articleData?.prevArticle?.url || '#'">{{ articleData?.prevArticle?.title || '暂无上一篇' }}</a>
</li>
<li class="pull-right">下一篇
<a :href="articleData.nextArticle.url">{{ articleData.nextArticle.title }}</a>
<a :href="articleData?.nextArticle?.url || '#'">{{ articleData?.nextArticle?.title || '暂无下一篇' }}</a>
</li>
</div>
<br><br><br><br>

View File

@ -1,4 +1,4 @@
import {nextTick, watch, onMounted, type Ref} from 'vue'
import {nextTick, watch, watchEffect, onMounted, type Ref} from 'vue'
/**
* Composable
@ -61,10 +61,11 @@ export const useImageStyles = (
// 监听内容变化
if (typeof contentRef === 'function') {
// 如果是函数,直接监听
watch(contentRef, () => {
// 如果是函数,使用 watchEffect 监听
watchEffect(() => {
contentRef() // 触发函数执行
processImageStyles()
}, {deep: true})
})
} else {
// 如果是 Ref监听其值
watch(contentRef, () => {
@ -76,7 +77,6 @@ export const useImageStyles = (
return {
forceImageStyles,
processImageStyles,
initImageStyles,
initImageStyles
}
}

View File

@ -3,11 +3,93 @@
definePageMeta({
layout: 'no-navigation'
})
// API
import {getArticleApiUrl} from '~/config/api'
//
const articleData = ref({
title: '',
publishDate: '',
category: '',
content: '',
prevArticle: {
title: '',
url: '#'
},
nextArticle: {
title: '',
url: '#'
}
})
//
const loading = ref(true)
const error = ref('')
//
const fetchArticle = async (id: string = '437') => {
try {
loading.value = true
error.value = ''
const response = await fetch(getArticleApiUrl('GET', id))
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const res = await response.json()
const data = res.data
console.log('api请求数据', data)
//
const categoryMapping: Record<string, string> = {
'solution': '解决方案',
'developKnowledge': '开发知识',
'industryTrend': '行业动态',
'aboutUs': '关于我们'
}
data.code = categoryMapping[data.code] || '暂无分类'
//
articleData.value = {
title: data.title || '暂无标题',
publishDate: data.createTime || '暂无日期',
category: data.code || '暂无分类',
content: data.content || '暂无内容',
prevArticle: data.prevArticle || {
title: '暂无上一篇',
url: '#'
},
nextArticle: data.nextArticle || {
title: '暂无下一篇',
url: '#'
}
}
} catch (err) {
console.error('获取文章失败:', err)
error.value = '获取文章失败,请稍后重试'
} finally {
loading.value = false
}
}
//
onMounted(() => {
fetchArticle()
})
</script>
<template>
<view>
<NewsNew/>
<NewsNew
:article-data="articleData"
:loading="loading"
:error="error"
/>
</view>
</template>