2025-10-11 08:45:18 +08:00
|
|
|
|
// 文章API服务
|
2025-10-13 09:32:00 +08:00
|
|
|
|
// 导入API配置
|
|
|
|
|
|
import {getApiUrl, API_CONFIG} from '~/config/api'
|
|
|
|
|
|
|
2025-10-11 08:45:18 +08:00
|
|
|
|
export interface Article {
|
2025-10-13 09:07:28 +08:00
|
|
|
|
id: string
|
|
|
|
|
|
title: string
|
|
|
|
|
|
brief: string | null
|
|
|
|
|
|
content: string | null
|
|
|
|
|
|
createTime: string
|
|
|
|
|
|
code: string | null
|
|
|
|
|
|
status: string | null
|
2025-10-11 08:45:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface ArticleListResponse {
|
2025-10-13 09:07:28 +08:00
|
|
|
|
msg: string
|
|
|
|
|
|
code: number
|
|
|
|
|
|
data: Article[]
|
2025-10-11 08:45:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface ArticleListParams {
|
2025-10-13 09:07:28 +08:00
|
|
|
|
code?: string // 文章类型:solution、developKnowledge、industryTrend
|
|
|
|
|
|
orderByColumn?: string
|
|
|
|
|
|
isAsc?: string
|
|
|
|
|
|
pageNum?: number
|
|
|
|
|
|
pageSize?: number
|
2025-10-11 08:45:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取文章列表
|
|
|
|
|
|
* @param params 查询参数
|
|
|
|
|
|
* @returns Promise<ArticleListResponse>
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const fetchArticleList = async (params: ArticleListParams = {}): Promise<ArticleListResponse> => {
|
2025-10-13 09:07:28 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// 构建查询参数
|
|
|
|
|
|
const queryParams = new URLSearchParams()
|
|
|
|
|
|
|
|
|
|
|
|
if (params.code) queryParams.append('code', params.code)
|
|
|
|
|
|
if (params.orderByColumn) queryParams.append('orderByColumn', params.orderByColumn)
|
|
|
|
|
|
if (params.isAsc) queryParams.append('isAsc', params.isAsc)
|
|
|
|
|
|
if (params.pageNum) queryParams.append('pageNum', params.pageNum.toString())
|
|
|
|
|
|
if (params.pageSize) queryParams.append('pageSize', params.pageSize.toString())
|
|
|
|
|
|
|
2025-10-13 09:32:00 +08:00
|
|
|
|
const url = `${getApiUrl(API_CONFIG.ENDPOINTS.ARTICLE.LIST)}?${queryParams.toString()}`
|
2025-10-11 08:45:18 +08:00
|
|
|
|
|
2025-10-13 09:07:28 +08:00
|
|
|
|
const response = await fetch(url, {
|
|
|
|
|
|
method: 'GET',
|
2025-10-13 09:32:00 +08:00
|
|
|
|
headers: API_CONFIG.REQUEST.HEADERS,
|
2025-10-13 09:07:28 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const result: ArticleListResponse = await response.json()
|
|
|
|
|
|
return result
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取文章列表失败:', error)
|
|
|
|
|
|
throw error
|
|
|
|
|
|
}
|
2025-10-11 08:45:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取推荐文章(按类型分组)
|
|
|
|
|
|
* @param types 文章类型数组
|
|
|
|
|
|
* @param pageSize 每页数量
|
|
|
|
|
|
* @returns Promise<Record<string, Article[]>>
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const fetchRecommendedArticles = async (
|
2025-10-13 09:07:28 +08:00
|
|
|
|
types: string[] = ['solution', 'developKnowledge', 'industryTrend'],
|
|
|
|
|
|
pageSize: number = 5
|
2025-10-11 08:45:18 +08:00
|
|
|
|
): Promise<Record<string, Article[]>> => {
|
2025-10-13 09:07:28 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const result: Record<string, Article[]> = {}
|
|
|
|
|
|
|
|
|
|
|
|
// 并发获取各类型文章
|
|
|
|
|
|
const promises = types.map(async (type) => {
|
|
|
|
|
|
const response = await fetchArticleList({
|
|
|
|
|
|
code: type,
|
|
|
|
|
|
orderByColumn: 'createTime',
|
|
|
|
|
|
isAsc: 'descending',
|
|
|
|
|
|
pageNum: 1,
|
|
|
|
|
|
pageSize: pageSize
|
|
|
|
|
|
})
|
|
|
|
|
|
return {type, articles: response.data}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const responses = await Promise.all(promises)
|
|
|
|
|
|
|
|
|
|
|
|
// 整理结果
|
|
|
|
|
|
responses.forEach(({type, articles}) => {
|
|
|
|
|
|
result[type] = articles
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取推荐文章失败:', error)
|
|
|
|
|
|
throw error
|
|
|
|
|
|
}
|
2025-10-11 08:45:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 文章类型映射
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const ARTICLE_TYPE_MAP: Record<string, string> = {
|
2025-10-13 09:07:28 +08:00
|
|
|
|
'solution': '解决方案',
|
|
|
|
|
|
'developKnowledge': '开发知识',
|
|
|
|
|
|
'industryTrend': '行业动态',
|
|
|
|
|
|
'aboutUs': '关于我们'
|
2025-10-11 08:45:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取文章类型的中文名称
|
|
|
|
|
|
* @param code 文章类型代码
|
|
|
|
|
|
* @returns 中文名称
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const getArticleTypeName = (code: string): string => {
|
2025-10-13 09:07:28 +08:00
|
|
|
|
return ARTICLE_TYPE_MAP[code] || '未知类型'
|
2025-10-11 08:45:18 +08:00
|
|
|
|
}
|
2025-10-11 09:42:31 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 文章API Composable
|
|
|
|
|
|
* @returns 文章API相关方法
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const useArticleApi = () => {
|
2025-10-13 09:07:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取文章列表
|
|
|
|
|
|
* @param params 查询参数
|
|
|
|
|
|
* @returns Promise<Article[]>
|
|
|
|
|
|
*/
|
|
|
|
|
|
const getArticles = async (params: ArticleListParams = {}): Promise<Article[]> => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await fetchArticleList(params)
|
|
|
|
|
|
return response.data || []
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取文章列表失败:', error)
|
|
|
|
|
|
return []
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取推荐文章(按类型分组)
|
|
|
|
|
|
* @param types 文章类型数组
|
|
|
|
|
|
* @param pageSize 每页数量
|
|
|
|
|
|
* @returns Promise<Record<string, Article[]>>
|
|
|
|
|
|
*/
|
|
|
|
|
|
const getRecommendedArticles = async (
|
|
|
|
|
|
types: string[] = ['solution', 'developKnowledge', 'industryTrend'],
|
|
|
|
|
|
pageSize: number = 5
|
|
|
|
|
|
): Promise<Record<string, Article[]>> => {
|
|
|
|
|
|
return await fetchRecommendedArticles(types, pageSize)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
getArticles,
|
|
|
|
|
|
getRecommendedArticles,
|
|
|
|
|
|
getArticleTypeName
|
2025-10-11 09:42:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|