xlqx/app/config/api.ts
2025-10-31 15:34:13 +08:00

75 lines
1.8 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
}
/**
* 获取轮播图列表
* @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 []
}
}