From 262ce70998fa52dec3120589f51c47e96fb96df4 Mon Sep 17 00:00:00 2001
From: 905086 <k2049@qq.com>
Date: Tue, 03 Nov 2026 17:04:19 +0800
Subject: [PATCH] feat: 完成游戏盒子前后端基础功能开发
---
fluttergamebox/lib/api/api_service.dart | 229 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 229 insertions(+), 0 deletions(-)
diff --git a/fluttergamebox/lib/api/api_service.dart b/fluttergamebox/lib/api/api_service.dart
new file mode 100644
index 0000000..a0c78b5
--- /dev/null
+++ b/fluttergamebox/lib/api/api_service.dart
@@ -0,0 +1,229 @@
+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 cover;
+ final String video;
+ final List<String> screenshots;
+ final String description;
+ final List<GameFile> 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.cover,
+ required this.video,
+ required this.screenshots,
+ required this.description,
+ required this.files,
+ required this.isBanner,
+ required this.downloadCount,
+ });
+
+ factory Game.fromJson(Map<String, dynamic> j) {
+ List<String> shots = [];
+ try {
+ if (j['screenshots'] is String && j['screenshots'].isNotEmpty) {
+ shots = (jsonDecode(j['screenshots']) as List).cast<String>();
+ }
+ } catch (_) {}
+ List<GameFile> 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'] ?? '',
+ 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<String, dynamic> 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<String, dynamic> 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;
+ Tool({
+ required this.id,
+ required this.name,
+ required this.size,
+ required this.cover,
+ required this.description,
+ required this.fileUrl,
+ });
+ factory Tool.fromJson(Map<String, dynamic> j) => Tool(
+ id: j['id'],
+ name: j['name'] ?? '',
+ size: j['size'] ?? '',
+ cover: j['cover'] ?? '',
+ description: j['description'] ?? '',
+ fileUrl: j['file_url'] ?? '',
+ );
+}
+
+class ApiService {
+ // ---------- 认证 ----------
+ static Future<Map<String, dynamic>> captcha() async =>
+ await ApiClient.get('/api/captcha');
+
+ static Future<Map<String, dynamic>> register(Map<String, dynamic> data) async =>
+ await ApiClient.post('/api/register', data);
+
+ static Future<Map<String, dynamic>> login(String phone, String password) async =>
+ await ApiClient.post('/api/login', {'phone': phone, 'password': password});
+
+ static Future<Map<String, dynamic>> resetPassword(Map<String, dynamic> data) async =>
+ await ApiClient.post('/api/reset_password', data);
+
+ static Future<Map<String, dynamic>> userInfo() async =>
+ await ApiClient.get('/api/user/info');
+
+ // ---------- 游戏数据 ----------
+ static Future<List<Game>> 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<List<Game>> 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<Map<String, dynamic>> gameDetail(int id) async =>
+ await ApiClient.get('/api/games/$id');
+
+ static Future<List<Category>> 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<List<Tool>> 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<String> 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<Map<String, dynamic>> toggleFavorite(int gameId) async =>
+ await ApiClient.post('/api/favorites', {'game_id': gameId});
+
+ static Future<List<Game>> 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<Map<String, dynamic>> addToLibrary(int gameId) async =>
+ await ApiClient.post('/api/library', {'game_id': gameId});
+
+ static Future<List<Game>> 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<Map<String, dynamic>> removeFromLibrary(int gameId) async =>
+ await ApiClient.delete('/api/library/$gameId');
+
+ // ---------- 下载 / 密钥 ----------
+ static Future<Map<String, dynamic>> downloadInfo(int gameId) async =>
+ await ApiClient.get('/api/download/$gameId');
+
+ static Future<Map<String, dynamic>> activateKey(
+ String code, String machineCode) async {
+ return await ApiClient.post(
+ '/api/keys/activate', {'code': code, 'machine_code': machineCode});
+ }
+}
--
Gitblit v1.10.0