接口
API 模块结构
API 按业务领域划分为多个模块,位于 web/app/api/ 目录下:
├── app/
│ ├── api/
│ │ ├── article.ts # 文章相关接口
│ │ ├── auth.ts # 认证相关接口
│ │ ├── member.ts # 会员相关接口
│ │ ├── ...
│ │ └── system.ts # 系统相关接口文章接口模块 (article.ts)
提供文章相关的数据获取功能:
typescript
/**
* 获取文章栏目
*/
export function getArticleCategory() {
return request.get(`article/category`)
}
/**
* 文章列表
*/
export function getArticleList(params: Record<string, any>) {
return request.get('article/list', params)
}
/**
* 获取文章热门推荐
*/
export function getArticleHot() {
return request.get(`article/hot`)
}
/**
* 获取文章详情
*/
export function getArticleInfo(id: number) {
return request.get(`article/${ id }`)
}
/**
* 文章列表
*/
export function getArticleAll(params: Record<string, any>) {
return request.get('article/all', params)
}
/**
* 增加文章访问量
*/
export function incVisit(id: any) {
return request.put(`article/incVisit/${ id }`, {}, { showErrorMessage: false, showSuccessMessage: false })
}接口使用示例
导入并使用 API:
typescript
// 在组件中导入并使用API
import { getArticleList, getArticleDetail } from '@/app/api/article'
export default {
setup() {
// 获取文章列表
const fetchArticles = async () => {
try {
const params = { page: 1, size: 10, category_id: 1 }
const result = await getArticleList(params)
console.log('文章列表:', result)
} catch (error) {
console.error('获取文章列表失败:', error)
}
}
// 获取文章详情
const fetchArticleDetail = async (id: number) => {
try {
const result = await getArticleDetail(id)
console.log('文章详情:', result)
} catch (error) {
console.error('获取文章详情失败:', error)
}
}
return {
fetchArticles,
fetchArticleDetail
}
}
}