xlqx/app/config/api.ts

161 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-10-31 15:34:13 +08:00
/**
* 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
}
2025-10-31 16:27:10 +08:00
/**
*
*/
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
}
2025-10-31 15:34:13 +08:00
/**
*
* @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 []
}
}
2025-10-31 16:27:10 +08:00
/**
*
* @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: [] }
}
}