/** * 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 { 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 { total: number rows: T[] } /** * 获取文章列表的查询参数 */ export interface GetArticleListParams { pageNum?: number pageSize?: number code?: string } /** * 获取轮播图列表 * @returns Promise 返回轮播图数据数组 */ export async function getBannerList(): Promise { try { const response = await $fetch>( `${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> 返回分页的文章数据 */ export async function getArticleList( params: GetArticleListParams = {} ): Promise> { try { const { pageNum = 1, pageSize = 10, code } = params // 构建查询参数 const queryParams: Record = { pageNum: pageNum.toString(), pageSize: pageSize.toString(), } if (code) { queryParams.code = code } const response = await $fetch>( `${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
返回文章详情数据 */ export async function getArticleDetail(id: string): Promise
{ try { const response = await $fetch>( `${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 } } /** * 商家加盟申请数据类型定义 */ export interface MchApplyParams { name: string mobile: string content: string hasDevice: number city: string } /** * 提交商家加盟申请 * @param params 申请参数 * @returns Promise> 返回申请结果 */ export async function submitMchApply(params: MchApplyParams): Promise> { try { const response = await $fetch>( `${API_BASE_URL}/app/mchApply`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: params, } ) return response } catch (error) { console.error('提交商家加盟申请时发生错误:', error) throw error } }