905086
2026-11-07 7c8e509c95c2af0c0d076b13fbbc5aa63a06cb69
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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<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.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<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'] ?? '',
      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<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;
  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<String, dynamic> 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<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<Map<String, dynamic>> toolDownloadInfo(int toolId) async =>
      await ApiClient.get('/api/tools/$toolId/download');
 
  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});
  }
}