feat: 全面迁移到 Flutter + 新增 GitHub 源与 CI 流水线
前端
- 移除全部 Avalonia 依赖,模型渲染与界面统一改用 Flutter
- 新增用户页:登录/注册(SHA-512 + 盐)、签到积分(30 天一循环)、
主题色积分购买、二选一随机、今日人品
- 新增百宝箱页、模型内嵌渲染页、自绘标题栏(最小化/最大化/关闭)
- 下载页显示模型下载进度条,下载源读取加速
- 关于页新增「重新下载启动器」并在下载后做 sha256 完整性校验
- 修复浅色主题下标题栏不跟随主题色、无法连接后端等问题
后端
- 新增 UserService:.GC/user/<用户名>.yaml 存用户名/积分/SHA-512 口令,
登录返回 session token,本地 .GC/user/user.yaml 保存登录态
- 新增 /api/user/{register,login,me,signin,theme} 接口
- 去掉 Avalonia 窗口宿主,后端只提供 HTTP API
下载源
- 新增 GitHub 镜像源(Morris-morris-cloud/GC),与 Xhmcl 并发测速
- 单源探测超时收紧到 2 秒,取最快且可用的源;GitHub 复用 GiteaRemote
(contents 带 ref、releases 带 per_page=100)
构建与 CI
- macOS 上默认只出 macOS 包,Windows 交给原生 Windows 构建
- 修复 Windows 交叉编译中 flutter-cpp-client-wrapper 双层目录嵌套
- 新增 GitHub Actions:
· nightly-prerelease:每天 21:00 编译三平台,发布 nightly 预发布
· build-bot:Issue/PR 评论 @build-bot build <mac|win-amd|win-arm|all>,
发布 ip-prerelease 预发布
· pr-review:检查能否编译、静态分析、合并冲突、是否落后 main
- 忽略 79MB 课件产物等高线.html 与工具链临时备份
This commit is contained in:
1 parent
b9cb56cb8b
commit
ea1fa6b6d1
134 files changed
+4309
-16614
No files matched your search
@@ -138,6 +138,26 @@ class JobInfo {
|
||||
final bool isDone;
|
||||
}
|
||||
|
||||
/// 「重新下载启动器」的结果。
|
||||
///
|
||||
/// State: Ready(已下载并通过 sha256 校验)/ UpToDate(已是最新,无需重下)/ Failed。
|
||||
class LauncherRedownloadInfo {
|
||||
LauncherRedownloadInfo.fromJson(Map<String, dynamic> json)
|
||||
: state = json['state'] as String? ?? '',
|
||||
version = json['version'] as String? ?? '',
|
||||
message = json['message'] as String? ?? '',
|
||||
steps = ((json['steps'] as List<dynamic>?) ?? const <dynamic>[])
|
||||
.map((e) => e.toString())
|
||||
.toList();
|
||||
|
||||
final String state;
|
||||
final String version;
|
||||
final String message;
|
||||
final List<String> steps;
|
||||
|
||||
bool get ok => state == 'Ready' || state == 'UpToDate';
|
||||
}
|
||||
|
||||
/// 界面语言选项。
|
||||
class LanguageInfo {
|
||||
LanguageInfo.fromJson(Map<String, dynamic> json)
|
||||
@@ -336,6 +356,14 @@ class ActionResult {
|
||||
List<String> _stringList(dynamic value) =>
|
||||
(value as List<dynamic>? ?? const []).map((e) => e.toString()).toList();
|
||||
|
||||
/// 握手文件内容。
|
||||
class _Endpoint {
|
||||
const _Endpoint(this.url, this.token);
|
||||
|
||||
final String url;
|
||||
final String? token;
|
||||
}
|
||||
|
||||
/// GC-UI 后端客户端。
|
||||
///
|
||||
/// 后端是本地伴随进程,端口由启动器(GC-BOOT)选定后通过 `GC_UI_PORT`
|
||||
@@ -345,6 +373,9 @@ class GcApi {
|
||||
GcApi({String? baseUrl})
|
||||
: baseUrl = baseUrl ?? defaultBaseUrl;
|
||||
|
||||
/// 后端地址。可被 [rediscover] 更新(后端换端口 / 首次靠握手文件发现)。
|
||||
String baseUrl;
|
||||
|
||||
/// 复用同一个 HTTP 连接:轮询(下载任务每秒一次)时每次都新建连接
|
||||
/// 会不断握手,桌面端也能明显看到延迟抖动。
|
||||
static final http.Client _client = http.Client();
|
||||
@@ -363,63 +394,180 @@ class GcApi {
|
||||
final port = int.tryParse(Platform.environment['GC_UI_PORT'] ?? '');
|
||||
if (port != null) return 'http://127.0.0.1:$port';
|
||||
|
||||
// 手动起后端 / 前端调试时没有环境变量:读后端写的 .GC/ui-backend.json。
|
||||
final discovered = _endpointFileUrl();
|
||||
if (discovered != null) return discovered;
|
||||
// 没有环境变量(例如直接从 Finder 打开 GC-UI.app):读后端写的握手文件。
|
||||
final discovered = _readEndpointFile();
|
||||
if (discovered != null) return discovered.url;
|
||||
|
||||
return 'http://127.0.0.1:$fallbackPort';
|
||||
}
|
||||
|
||||
/// 从运行目录下的 `.GC/ui-backend.json` 发现后端地址。
|
||||
///
|
||||
/// 后端每次启动都会写这个文件(port / pid / url)。只认 24 小时内的记录:
|
||||
/// 更早的多半是上次遗留的,连过去只会得到一个「无法连接」。
|
||||
static String? _endpointFileUrl() {
|
||||
try {
|
||||
final file = File('${Directory.current.path}/.GC/ui-backend.json');
|
||||
if (!file.existsSync()) return null;
|
||||
/// 后端启动时写的握手文件:地址 + 会话令牌。
|
||||
static _Endpoint? _endpoint;
|
||||
|
||||
final age = DateTime.now().difference(file.lastModifiedSync());
|
||||
if (age.inHours >= 24) return null;
|
||||
|
||||
final decoded = jsonDecode(file.readAsStringSync());
|
||||
if (decoded is! Map<String, dynamic>) return null;
|
||||
|
||||
final url = decoded['url'];
|
||||
if (url is String && url.startsWith('http://127.0.0.1:')) return url;
|
||||
|
||||
final port = (decoded['port'] as num?)?.toInt();
|
||||
if (port != null && port > 0 && port < 65536) {
|
||||
return 'http://127.0.0.1:$port';
|
||||
}
|
||||
} catch (_) {
|
||||
// 文件损坏 / 无权限:退回默认端口即可
|
||||
/// 依次尝试的运行目录。打包后 GC-UI.app 在 GC.app/Contents/MacOS/ 下,
|
||||
/// 直接 Finder 打开时 Directory.current 可能是 `/`,所以要把可执行文件周围都试一遍。
|
||||
static List<String> _candidateRoots() {
|
||||
final roots = <String>[];
|
||||
void add(String path) {
|
||||
if (path.isEmpty) return;
|
||||
final root = path.endsWith('/') ? path.substring(0, path.length - 1) : path;
|
||||
if (!roots.contains(root)) roots.add(root);
|
||||
}
|
||||
return null;
|
||||
|
||||
add(Platform.environment['GC_UI_HOME'] ?? '');
|
||||
try {
|
||||
add(Directory.current.path);
|
||||
} catch (_) {}
|
||||
|
||||
try {
|
||||
var dir = File(Platform.resolvedExecutable).parent;
|
||||
// GC-UI.app/Contents/MacOS -> 逐级向上都可能是 .GC 所在目录
|
||||
for (var i = 0; i < 4; i++) {
|
||||
add(dir.path);
|
||||
if (dir.parent.path == dir.path) break;
|
||||
dir = dir.parent;
|
||||
}
|
||||
} catch (_) {}
|
||||
|
||||
return roots;
|
||||
}
|
||||
|
||||
final String baseUrl;
|
||||
static _Endpoint? _readEndpointFile({bool refresh = false}) {
|
||||
if (!refresh && _endpoint != null) return _endpoint;
|
||||
|
||||
/// 启动器注入的一次性会话令牌。后端在发布包模式下会校验它,
|
||||
/// 防止前端误连到同机其他进程伪造的「后端」。
|
||||
static final String? _sessionToken = _readSessionToken();
|
||||
for (final root in _candidateRoots()) {
|
||||
try {
|
||||
final file = File('$root/.GC/ui-backend.json');
|
||||
try {
|
||||
final logFile = File('${Directory.systemTemp.path}/gc-api-debug.log');
|
||||
logFile.writeAsStringSync('[api] 检查: $root/.GC/ui-backend.json (exists=${file.existsSync()})\n', mode: FileMode.append);
|
||||
} catch (_) {}
|
||||
if (!file.existsSync()) continue;
|
||||
|
||||
// 只认 24 小时内的记录:更早的多半是上次遗留,连过去只会得到「无法连接」
|
||||
final age = DateTime.now().difference(file.lastModifiedSync());
|
||||
if (age.inHours >= 24) continue;
|
||||
|
||||
final decoded = jsonDecode(file.readAsStringSync());
|
||||
if (decoded is! Map<String, dynamic>) continue;
|
||||
|
||||
String? url;
|
||||
final rawUrl = decoded['url'];
|
||||
if (rawUrl is String && rawUrl.startsWith('http://127.0.0.1:')) {
|
||||
url = rawUrl;
|
||||
} else {
|
||||
final port = (decoded['port'] as num?)?.toInt();
|
||||
if (port != null && port > 0 && port < 65536) {
|
||||
url = 'http://127.0.0.1:$port';
|
||||
}
|
||||
}
|
||||
if (url == null) continue;
|
||||
|
||||
final token = decoded['token'];
|
||||
return _endpoint = _Endpoint(url, token is String && token.isNotEmpty ? token : null);
|
||||
} catch (_) {
|
||||
// 文件损坏 / 无权限:试下一个候选目录
|
||||
}
|
||||
}
|
||||
return _endpoint = null;
|
||||
}
|
||||
|
||||
/// 重新解析后端地址与令牌(后端重启换端口、或环境变量缺失时用)。
|
||||
/// 返回 true 表示地址或令牌发生了变化,值得重试一次。
|
||||
bool rediscover() {
|
||||
final before = (_endpoint?.url, _sessionToken);
|
||||
_endpoint = null;
|
||||
final endpoint = _readEndpointFile(refresh: true);
|
||||
|
||||
if (endpoint?.token != null && endpoint!.token != _sessionToken) {
|
||||
_sessionToken = endpoint.token;
|
||||
}
|
||||
|
||||
final envUrl = Platform.environment['GC_UI_BASE_URL'];
|
||||
final envPort = int.tryParse(Platform.environment['GC_UI_PORT'] ?? '');
|
||||
final resolved = (envUrl != null && envUrl.isNotEmpty)
|
||||
? envUrl
|
||||
: (envPort != null
|
||||
? 'http://127.0.0.1:$envPort'
|
||||
: (endpoint?.url ?? 'http://127.0.0.1:$fallbackPort'));
|
||||
|
||||
if (resolved != baseUrl) {
|
||||
baseUrl = resolved;
|
||||
return true;
|
||||
}
|
||||
return (before.$1 != _endpoint?.url) || (before.$2 != _sessionToken);
|
||||
}
|
||||
|
||||
/// 启动器注入的一次性会话令牌:环境变量优先,其次握手文件。
|
||||
/// 后端在发布包模式下校验它,防止前端连到同机伪造的「后端」。
|
||||
static String? _sessionToken = _readSessionToken();
|
||||
|
||||
static String? _readSessionToken() {
|
||||
const fromDefine = String.fromEnvironment('GC_UI_TOKEN');
|
||||
if (fromDefine.isNotEmpty) return fromDefine;
|
||||
final fromEnv = Platform.environment['GC_UI_TOKEN'];
|
||||
return (fromEnv != null && fromEnv.isNotEmpty) ? fromEnv : null;
|
||||
if (fromEnv != null && fromEnv.isNotEmpty) return fromEnv;
|
||||
// 环境变量没传进来(Finder 直接打开 GC-UI.app):用握手文件里的令牌
|
||||
return _readEndpointFile()?.token;
|
||||
}
|
||||
|
||||
/// 是否拿到了会话令牌(诊断用)。
|
||||
bool get hasSessionToken => _sessionToken != null;
|
||||
|
||||
/// 用户 session token(登录时后端返回,持久化到 ~/.GC/user/user.yaml)
|
||||
static String? _userToken;
|
||||
static String? _username;
|
||||
|
||||
static String? get userToken => _userToken;
|
||||
static String? get username => _username;
|
||||
|
||||
static Future<void> _loadUserToken() async {
|
||||
try {
|
||||
final dir = Directory('${Platform.environment['HOME'] ?? '.'}/.GC/user');
|
||||
if (!dir.existsSync()) return;
|
||||
final file = File('${dir.path}/user.yaml');
|
||||
if (!file.existsSync()) return;
|
||||
final lines = file.readAsLinesSync();
|
||||
for (var line in lines) {
|
||||
if (line.startsWith('username:')) _username = line.split(':').skip(1).join(':').trim();
|
||||
if (line.startsWith('session_token:')) _userToken = line.split(':').skip(1).join(':').trim();
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
static Future<void> _saveUserToken(String username, String token) async {
|
||||
try {
|
||||
final dir = Directory('${Platform.environment['HOME'] ?? '.'}/.GC/user');
|
||||
dir.createSync(recursive: true);
|
||||
final file = File('${dir.path}/user.yaml');
|
||||
file.writeAsStringSync('username: $username\nsession_token: $token\n');
|
||||
_username = username;
|
||||
_userToken = token;
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
static Future<void> clearUserToken() async {
|
||||
_username = null;
|
||||
_userToken = null;
|
||||
try {
|
||||
final file = File('${Platform.environment['HOME'] ?? '.'}/.GC/user/user.yaml');
|
||||
if (file.existsSync()) file.deleteSync();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// 带会话令牌的请求头;没有令牌时返回 const 空 map(调试/独立后端模式)。
|
||||
Map<String, String> get _authHeaders =>
|
||||
_sessionToken == null ? const {} : {'X-GC-UI-Token': _sessionToken!};
|
||||
Map<String, String> get _authHeaders => {
|
||||
if (_sessionToken != null) 'X-GC-UI-Token': _sessionToken!,
|
||||
if (_userToken != null) 'X-User-Token': _userToken!,
|
||||
};
|
||||
|
||||
Uri _uri(String path, [Map<String, String>? query]) =>
|
||||
Uri.parse('$baseUrl$path').replace(queryParameters: query);
|
||||
|
||||
Future<dynamic> _send(String method, String path,
|
||||
{Map<String, String>? query, Object? body}) async {
|
||||
{Map<String, String>? query,
|
||||
Object? body,
|
||||
Duration? timeout}) async {
|
||||
final uri = _uri(path, query);
|
||||
final auth = _authHeaders;
|
||||
late http.Response response;
|
||||
@@ -427,18 +575,18 @@ class GcApi {
|
||||
switch (method) {
|
||||
case 'GET':
|
||||
response = await _client.get(uri, headers: auth)
|
||||
.timeout(const Duration(seconds: 30));
|
||||
.timeout(timeout ?? const Duration(seconds: 30));
|
||||
break;
|
||||
case 'POST':
|
||||
response = await _client
|
||||
.post(uri,
|
||||
headers: <String, String>{...auth, 'Content-Type': 'application/json'},
|
||||
body: body == null ? null : jsonEncode(body))
|
||||
.timeout(const Duration(seconds: 120));
|
||||
.timeout(timeout ?? const Duration(seconds: 120));
|
||||
break;
|
||||
case 'DELETE':
|
||||
response = await _client.delete(uri, headers: auth)
|
||||
.timeout(const Duration(seconds: 30));
|
||||
.timeout(timeout ?? const Duration(seconds: 30));
|
||||
break;
|
||||
default:
|
||||
throw ApiException('不支持的方法 $method');
|
||||
@@ -502,6 +650,14 @@ class GcApi {
|
||||
'force': force.toString(),
|
||||
}) as Map<String, dynamic>);
|
||||
|
||||
/// 重新下载本机启动器包,返回时后端已完成 sha256 完整性校验。
|
||||
Future<LauncherRedownloadInfo> redownloadLauncher() async =>
|
||||
LauncherRedownloadInfo.fromJson(await _send(
|
||||
'POST',
|
||||
'/api/launcher/redownload',
|
||||
timeout: const Duration(minutes: 10),
|
||||
) as Map<String, dynamic>);
|
||||
|
||||
Future<List<JobInfo>> jobs() async =>
|
||||
_asList(await _send('GET', '/api/download/jobs')).map(JobInfo.fromJson).toList();
|
||||
|
||||
@@ -546,4 +702,56 @@ class GcApi {
|
||||
'X-GC-UI-Token': _sessionToken!,
|
||||
}).toString();
|
||||
}
|
||||
|
||||
// ==================== 用户系统 ====================
|
||||
|
||||
/// 初始化:加载本地保存的用户token
|
||||
Future<void> initUserSession() async {
|
||||
await _loadUserToken();
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>?> me() async {
|
||||
try {
|
||||
final r = await _send('GET', '/api/user/me');
|
||||
return r as Map<String, dynamic>?;
|
||||
} on ApiException catch (e) {
|
||||
if (e.statusCode == 401 || e.statusCode == 403) {
|
||||
await clearUserToken();
|
||||
return null;
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> register(String username, String password) async {
|
||||
final r = await _send('POST', '/api/user/register',
|
||||
body: {'username': username, 'password': password});
|
||||
return r as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> login(String username, String password) async {
|
||||
final r = await _send('POST', '/api/user/login',
|
||||
body: {'username': username, 'password': password});
|
||||
final result = r as Map<String, dynamic>;
|
||||
// 保存 session token
|
||||
if (result.containsKey('session_token')) {
|
||||
await _saveUserToken(username, result['session_token'] as String);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await clearUserToken();
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> signin() async {
|
||||
final r = await _send('POST', '/api/user/signin');
|
||||
return r as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> changeTheme(String color, int cost) async {
|
||||
final r = await _send('POST', '/api/user/theme',
|
||||
body: {'color': color, 'cost': cost});
|
||||
return r as Map<String, dynamic>;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user