xlqx/app/config/api.ts
2025-10-31 17:09:26 +08:00

192 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* API 配置和接口函数
* 用于统一管理项目的 API 请求
*/
// API 基础地址
export const API_BASE_URL = 'https://ele.ccttiot.com/prod-api'
/**
* Banner 数据类型定义
*/
export interface BannerItem {
id: string
imgUrl: string
status: string
orderNum: string
createBy?: string
createTime?: string
updateBy?: string
updateTime?: string
remark?: string | null
scope?: string | null
deleted?: string | null
areaPermissions?: string | null
createId?: string
updateId?: string
}
/**
* API 响应数据结构
*/
export interface ApiResponse<T> {
msg: string
code: number
data: T
}
/**
* 文章数据类型定义
*/
export interface Article {
id: string
title: string
brief: string | null
content: string | null
createTime: string
createBy?: string | null
updateTime?: string | null
updateBy?: string | null
remark?: string | null
code?: string | null
status?: string | null
scope?: string | null
deleted?: string | null
areaPermissions?: string | null
}
/**
* 分页响应数据结构
*/
export interface PageResponse<T> {
total: number
rows: T[]
}
/**
* 获取文章列表的查询参数
*/
export interface GetArticleListParams {
pageNum?: number
pageSize?: number
code?: string
}
/**
* 获取轮播图列表
* @returns Promise<BannerItem[]> 返回轮播图数据数组
*/
export async function getBannerList(): Promise<BannerItem[]> {
try {
const response = await $fetch<ApiResponse<BannerItem[]>>(
`${API_BASE_URL}/app/owBanner/list`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
)
// 检查响应状态
if (response.code === 200 && Array.isArray(response.data)) {
// 过滤出状态为启用的 banner并按 orderNum 排序
return response.data
.filter((item) => item.status === '1') // 只返回启用状态的 banner
.sort((a, b) => {
const orderA = parseInt(a.orderNum || '0', 10)
const orderB = parseInt(b.orderNum || '0', 10)
return orderB - orderA // 降序排列,数字越大越靠前
})
}
console.warn('获取轮播图列表失败,返回空数组', response)
return []
} catch (error) {
console.error('获取轮播图列表时发生错误:', error)
// 发生错误时返回空数组,避免页面崩溃
return []
}
}
/**
* 获取文章列表
* @param params 查询参数pageNum, pageSize, code等
* @returns Promise<PageResponse<Article>> 返回分页的文章数据
*/
export async function getArticleList(
params: GetArticleListParams = {}
): Promise<PageResponse<Article>> {
try {
const { pageNum = 1, pageSize = 10, code } = params
// 构建查询参数
const queryParams: Record<string, string> = {
pageNum: pageNum.toString(),
pageSize: pageSize.toString(),
}
if (code) {
queryParams.code = code
}
const response = await $fetch<PageResponse<Article>>(
`${API_BASE_URL}/app/owArticle/list`,
{
method: 'GET',
params: queryParams,
headers: {
'Content-Type': 'application/json',
},
}
)
// 检查响应数据
if (response && Array.isArray(response.rows)) {
return {
total: response.total || 0,
rows: response.rows || [],
}
}
console.warn('获取文章列表失败,返回空数据', response)
return { total: 0, rows: [] }
} catch (error) {
console.error('获取文章列表时发生错误:', error)
// 发生错误时返回空数据,避免页面崩溃
return { total: 0, rows: [] }
}
}
/**
* 获取文章详情
* @param id 文章ID
* @returns Promise<Article> 返回文章详情数据
*/
export async function getArticleDetail(id: string): Promise<Article | null> {
try {
const response = await $fetch<ApiResponse<Article>>(
`${API_BASE_URL}/app/owArticle/detail`,
{
method: 'GET',
params: { id },
headers: {
'Content-Type': 'application/json',
},
}
)
// 检查响应状态
if (response.code === 200 && response.data) {
return response.data
}
console.warn('获取文章详情失败', response)
return null
} catch (error) {
console.error('获取文章详情时发生错误:', error)
return null
}
}