import 'dart:convert'; import 'package:fluttergamebox/api/api_client.dart'; class Game { final int id; final String title; final int categoryId; final String categoryName; final String platform; // pc / switch final String tags; final String size; final String installSize; final String osReq; final String cpuReq; final String gpuReq; final String ramReq; final String versionNote; final String launchExe; // 启动程序相对路径(相对游戏安装目录) final String cover; final String video; final List screenshots; final String description; final List files; final bool isBanner; final int downloadCount; Game({ required this.id, required this.title, required this.categoryId, required this.categoryName, required this.platform, required this.tags, required this.size, required this.installSize, required this.osReq, required this.cpuReq, required this.gpuReq, required this.ramReq, required this.versionNote, required this.launchExe, required this.cover, required this.video, required this.screenshots, required this.description, required this.files, required this.isBanner, required this.downloadCount, }); factory Game.fromJson(Map j) { List shots = []; try { if (j['screenshots'] is String && j['screenshots'].isNotEmpty) { shots = (jsonDecode(j['screenshots']) as List).cast(); } } catch (_) {} List files = []; try { if (j['files'] is String && j['files'].isNotEmpty) { files = (jsonDecode(j['files']) as List) .map((e) => GameFile.fromJson(e)) .toList(); } } catch (_) {} return Game( id: (j['id'] ?? 0) as int, title: j['title'] ?? '', categoryId: (j['category_id'] ?? 0) as int, categoryName: j['category_name'] ?? '', platform: j['platform'] ?? 'pc', tags: j['tags'] ?? '', size: j['size'] ?? '', installSize: j['install_size'] ?? '', osReq: j['os_req'] ?? '', cpuReq: j['cpu_req'] ?? '', gpuReq: j['gpu_req'] ?? '', ramReq: j['ram_req'] ?? '', versionNote: j['version_note'] ?? '', launchExe: j['launch_exe'] ?? '', cover: j['cover'] ?? '', video: j['video'] ?? '', screenshots: shots, description: j['description'] ?? '', files: files, isBanner: (j['is_banner'] ?? false) as bool, downloadCount: (j['download_count'] ?? 0) as int, ); } String get platformLabel => platform == 'switch' ? 'Switch资源' : 'PC资源'; } class GameFile { final String name; final String size; final String url; GameFile({required this.name, required this.size, required this.url}); factory GameFile.fromJson(Map j) => GameFile( name: j['name'] ?? '', size: j['size'] ?? '', url: j['url'] ?? '', ); } class Category { final int id; final String name; Category({required this.id, required this.name}); factory Category.fromJson(Map j) => Category(id: j['id'], name: j['name']); } class Tool { final int id; final String name; final String size; final String cover; final String description; final String fileUrl; final String launchExe; Tool({ required this.id, required this.name, required this.size, required this.cover, required this.description, required this.fileUrl, this.launchExe = '', }); factory Tool.fromJson(Map j) => Tool( id: j['id'], name: j['name'] ?? '', size: j['size'] ?? '', cover: j['cover'] ?? '', description: j['description'] ?? '', fileUrl: j['file_url'] ?? '', launchExe: j['launch_exe'] ?? '', ); } class ApiService { // ---------- 认证 ---------- static Future> captcha() async => await ApiClient.get('/api/captcha'); static Future> register(Map data) async => await ApiClient.post('/api/register', data); static Future> login(String phone, String password) async => await ApiClient.post('/api/login', {'phone': phone, 'password': password}); static Future> resetPassword(Map data) async => await ApiClient.post('/api/reset_password', data); static Future> userInfo() async => await ApiClient.get('/api/user/info'); // ---------- 游戏数据 ---------- static Future> games( {String platform = 'all', int category = 0, String keyword = '', String sort = 'updated'}) async { final res = await ApiClient.get('/api/games', params: { 'platform': platform, 'category': category.toString(), 'keyword': keyword, 'sort': sort, }); if (res['code'] != 0) return []; return (res['data'] as List).map((e) => Game.fromJson(e)).toList(); } static Future> banners() async { final res = await ApiClient.get('/api/banners'); if (res['code'] != 0) return []; return (res['data'] as List).map((e) => Game.fromJson(e)).toList(); } static Future> gameDetail(int id) async => await ApiClient.get('/api/games/$id'); static Future> categories() async { final res = await ApiClient.get('/api/categories'); if (res['code'] != 0) return []; return (res['data'] as List).map((e) => Category.fromJson(e)).toList(); } static Future> tools() async { final res = await ApiClient.get('/api/tools'); if (res['code'] != 0) return []; return (res['data'] as List).map((e) => Tool.fromJson(e)).toList(); } static Future> toolDownloadInfo(int toolId) async => await ApiClient.get('/api/tools/$toolId/download'); static Future announcements() async { final res = await ApiClient.get('/api/announcements'); if (res['code'] != 0) return ''; final list = res['data'] as List; if (list.isEmpty) return ''; return list.first['content'] ?? ''; } // ---------- 收藏 / 资源库 ---------- static Future> toggleFavorite(int gameId) async => await ApiClient.post('/api/favorites', {'game_id': gameId}); static Future> favorites() async { final res = await ApiClient.get('/api/favorites'); if (res['code'] != 0) return []; return (res['data'] as List).map((e) => Game.fromJson(e)).toList(); } static Future> addToLibrary(int gameId) async => await ApiClient.post('/api/library', {'game_id': gameId}); static Future> library() async { final res = await ApiClient.get('/api/library'); if (res['code'] != 0) return []; return (res['data'] as List).map((e) => Game.fromJson(e)).toList(); } static Future> removeFromLibrary(int gameId) async => await ApiClient.delete('/api/library/$gameId'); // ---------- 下载 / 密钥 ---------- static Future> downloadInfo(int gameId) async => await ApiClient.get('/api/download/$gameId'); static Future> activateKey( String code, String machineCode) async { return await ApiClient.post( '/api/keys/activate', {'code': code, 'machine_code': machineCode}); } }