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_client.dart |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 55 insertions(+), 0 deletions(-)

diff --git a/fluttergamebox/lib/api/api_client.dart b/fluttergamebox/lib/api/api_client.dart
new file mode 100644
index 0000000..6b6bed3
--- /dev/null
+++ b/fluttergamebox/lib/api/api_client.dart
@@ -0,0 +1,55 @@
+import 'package:dio/dio.dart';
+import 'package:shared_preferences/shared_preferences.dart';
+
+class ApiClient {
+  static const baseUrl = 'http://localhost:8080';
+  static final Dio dio = Dio(BaseOptions(
+    baseUrl: baseUrl,
+    connectTimeout: const Duration(seconds: 10),
+    receiveTimeout: const Duration(minutes: 30),
+  ));
+
+  static String? token;
+
+  static Future<void> init() async {
+    final sp = await SharedPreferences.getInstance();
+    token = sp.getString('token');
+    dio.interceptors.add(InterceptorsWrapper(
+      onRequest: (options, handler) {
+        if (token != null) {
+          options.headers['Authorization'] = 'Bearer $token';
+        }
+        handler.next(options);
+      },
+    ));
+  }
+
+  static Future<void> saveToken(String t) async {
+    token = t;
+    final sp = await SharedPreferences.getInstance();
+    await sp.setString('token', t);
+  }
+
+  static Future<void> clearToken() async {
+    token = null;
+    final sp = await SharedPreferences.getInstance();
+    await sp.remove('token');
+  }
+
+  static Future<Map<String, dynamic>> get(String path,
+      {Map<String, dynamic>? params}) async {
+    final res = await dio.get(path, queryParameters: params);
+    return res.data as Map<String, dynamic>;
+  }
+
+  static Future<Map<String, dynamic>> post(String path,
+      [dynamic data]) async {
+    final res = await dio.post(path, data: data);
+    return res.data as Map<String, dynamic>;
+  }
+
+  static Future<Map<String, dynamic>> delete(String path) async {
+    final res = await dio.delete(path);
+    return res.data as Map<String, dynamic>;
+  }
+}

--
Gitblit v1.10.0