/** * API 请求封装 */ // 从环境变量读取 API 基础地址 const BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:3000' interface RequestOptions { url: string method?: 'GET' | 'POST' | 'PUT' | 'DELETE' data?: any header?: Record } /** * 统一请求方法 */ function request(options: RequestOptions): Promise { return new Promise((resolve, reject) => { uni.request({ url: BASE_URL + options.url, method: options.method || 'GET', data: options.data || {}, header: { 'Content-Type': 'application/json', ...options.header }, success: (res) => { if (res.statusCode >= 200 && res.statusCode < 300) { resolve(res.data as T) } else { reject(new Error(`请求失败: ${res.statusCode} ${(res.data as any)?.message || ''}`)) } }, fail: (err) => { console.error('请求错误:', err) reject(new Error(`网络请求失败: ${err.errMsg || '未知错误'}`)) } }) }) } // 数据类型定义 export interface BidItem { id: string title: string url: string publishDate: string source: string pin: boolean createdAt: string updatedAt: string } export interface AiRecommendation { id: string title: string confidence: number createdAt: string } export interface CrawlInfoStat { source: string count: number latestUpdate: string latestPublishDate: string error: string } /** * 获取置顶投标项目 */ export function getPinnedBids(): Promise { return request({ url: '/api/bids/pinned', method: 'GET' }) } /** * 获取 AI 推荐 */ export function getAiRecommendations(): Promise { return request({ url: '/api/ai/latest-recommendations', method: 'GET' }) } /** * 获取爬虫统计信息 */ export function getCrawlInfoStats(): Promise { return request({ url: '/api/bids/crawl-info-stats', method: 'GET' }) } export default { request, getPinnedBids, getAiRecommendations, getCrawlInfoStats }