Files
photography/frontend/lib/categoryService.ts
xujiang af222afc33 feat: 完成前后端API联调测试并修复配置问题
- 启动后端go-zero API服务 (端口8080)
- 修复前端API配置中的端口号 (8888→8080)
- 完善前端API状态监控组件
- 创建categoryService服务层
- 更新前端数据查询和转换逻辑
- 完成完整API集成测试,验证所有接口正常工作
- 验证用户认证、分类管理、照片管理等核心功能
- 创建API集成测试脚本
- 更新任务进度文档

测试结果:
 后端健康检查正常
 用户认证功能正常 (admin/admin123)
 分类API正常 (5个分类)
 照片API正常 (0张照片,数据库为空)
 前后端API连接完全正常

下一步: 实现照片展示页面和搜索过滤功能
2025-07-11 11:42:14 +08:00

52 lines
1.5 KiB
TypeScript

import api from './api'
import { Category } from './queries'
// 分类服务 - 处理分类相关的API调用
class CategoryService {
private categoryCache: Map<number, string> = new Map()
// 获取所有分类
async getAllCategories(): Promise<Category[]> {
if (process.env.NEXT_PUBLIC_USE_REAL_API === 'true') {
const response: any = await api.get('/categories?page=1&page_size=100')
return response?.categories || []
} else {
// Mock API 返回字符串数组,需要转换
const categories: string[] = await api.get('/categories')
return categories.map((name: string, index: number) => ({
id: index + 1,
name,
description: '',
created_at: Date.now() / 1000,
updated_at: Date.now() / 1000
}))
}
}
// 根据分类ID获取分类名称
async getCategoryName(categoryId: number): Promise<string> {
if (this.categoryCache.has(categoryId)) {
return this.categoryCache.get(categoryId)!
}
try {
const categories = await this.getAllCategories()
// 缓存所有分类
categories.forEach(cat => {
this.categoryCache.set(cat.id, cat.name)
})
return this.categoryCache.get(categoryId) || 'unknown'
} catch (error) {
console.error('获取分类名称失败:', error)
return 'unknown'
}
}
// 清除缓存
clearCache() {
this.categoryCache.clear()
}
}
export const categoryService = new CategoryService()