Files
GC/GC-UI/frontend/lib/pages/user_page.dart
T
Morris ea1fa6b6d1
PR 审核 / 后端编译 (pull_request) Waiting to run
PR 审核 / 前端检查(Flutter) (pull_request) Waiting to run
PR 审核 / 安全扫描 (pull_request) Waiting to run
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 与工具链临时备份
2026-11-21 19:18:58 +08:00

405 lines
15 KiB
Dart

import 'package:flutter/material.dart';
import 'dart:math';
import '../controller.dart';
import '../theme.dart';
import '../i18n.dart';
import '../models/user.dart';
/// 用户页面:登录/注册、签到、主题颜色、二选一
class UserPage extends StatefulWidget {
const UserPage({super.key, required this.controller});
final ShellController controller;
@override
State<UserPage> createState() => _UserPageState();
}
class _UserPageState extends State<UserPage> {
UserRecord? _user;
bool _loading = true;
String? _error;
// 登录/注册表单
final _usernameCtrl = TextEditingController();
final _passwordCtrl = TextEditingController();
bool _isLogin = true;
// 二选一
final _option1Ctrl = TextEditingController();
final _option2Ctrl = TextEditingController();
String? _randomResult;
@override
void initState() {
super.initState();
_loadUser();
}
@override
void dispose() {
_usernameCtrl.dispose();
_passwordCtrl.dispose();
_option1Ctrl.dispose();
_option2Ctrl.dispose();
super.dispose();
}
Future<void> _loadUser() async {
setState(() {
_loading = true;
_error = null;
});
try {
await widget.controller.api.initUserSession();
final resp = await widget.controller.api.me();
if (!mounted) return;
if (resp != null) {
setState(() {
_user = UserRecord.fromJson(resp);
_loading = false;
});
} else {
setState(() {
_loading = false;
});
}
} catch (e) {
if (!mounted) return;
setState(() {
_loading = false;
_error = e.toString();
});
}
}
Future<void> _loginOrRegister() async {
final username = _usernameCtrl.text.trim();
final password = _passwordCtrl.text;
if (username.isEmpty || password.isEmpty) {
_showError(T('user.fillAll'));
return;
}
try {
Map<String, dynamic> resp;
if (_isLogin) {
resp = await widget.controller.api.login(username, password);
} else {
resp = await widget.controller.api.register(username, password);
if (!mounted) return;
// 注册成功后自动登录
resp = await widget.controller.api.login(username, password);
}
if (!mounted) return;
setState(() {
_user = UserRecord.fromJson(resp);
_usernameCtrl.clear();
_passwordCtrl.clear();
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(_isLogin ? T('user.loginSuccess') : T('user.registerSuccess'))),
);
} catch (e) {
if (!mounted) return;
_showError(e.toString());
}
}
Future<void> _signin() async {
try {
final resp = await widget.controller.api.signin();
if (!mounted) return;
if (resp.containsKey('message') && resp['message'] == '今日已签到') {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(T('user.alreadySigned'))),
);
return;
}
setState(() {
_user = UserRecord.fromJson({
...resp,
'username': _user?.username ?? '',
});
});
final reward = resp['reward'] as int? ?? 0;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('${T('user.signinSuccess')} +$reward ${T('user.points')}')),
);
} catch (e) {
if (!mounted) return;
_showError(e.toString());
}
}
void _pickRandom() {
final opt1 = _option1Ctrl.text.trim();
final opt2 = _option2Ctrl.text.trim();
if (opt1.isEmpty || opt2.isEmpty) {
_showError(T('user.fillOptions'));
return;
}
final result = Random().nextBool() ? opt1 : opt2;
setState(() => _randomResult = result);
}
Future<void> _changeTheme(String hex, int cost) async {
if (_user == null) {
_showError(T('user.loginFirst'));
return;
}
try {
final resp = await widget.controller.api.changeTheme(hex, cost);
if (!mounted) return;
if (resp.containsKey('message')) {
setState(() {
_user = UserRecord(
username: _user!.username,
points: resp['points'] as int? ?? _user!.points,
consecutiveDays: _user!.consecutiveDays,
lastSignIn: _user!.lastSignIn,
themeColor: '0x${resp['theme_color'] ?? hex}',
);
});
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('主题已更新')),
);
}
} catch (e) {
if (!mounted) return;
_showError(e.toString());
}
}
void _showError(String msg) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(msg)),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: GcColors.appBg,
body: _loading
? const Center(child: CircularProgressIndicator())
: SingleChildScrollView(
padding: const EdgeInsets.all(GcSpace.xxl),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
// 用户信息卡片
if (_user != null) ...[
GcCard(
padding: const EdgeInsets.all(GcSpace.lg),
child: Row(
children: <Widget>[
Icon(Icons.person_rounded, size: 40, color: GcColors.accent),
const SizedBox(width: GcSpace.md),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
_user!.username,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.w700,
),
),
const SizedBox(height: 4),
Text(
'${T('user.points')}: ${_user!.points}',
style: TextStyle(color: GcColors.muted),
),
Text(
'${T('user.days')}: ${_user!.consecutiveDays}',
style: TextStyle(color: GcColors.muted),
),
],
),
),
FilledButton(
onPressed: _signin,
child: Text(T('user.signin')),
),
],
),
),
const SizedBox(height: GcSpace.lg),
],
// 登录/注册表单
GcCard(
padding: const EdgeInsets.all(GcSpace.lg),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
_isLogin ? T('user.login') : T('user.register'),
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: GcSpace.md),
TextField(
controller: _usernameCtrl,
decoration: InputDecoration(
labelText: T('user.username'),
border: const OutlineInputBorder(),
),
),
const SizedBox(height: GcSpace.md),
TextField(
controller: _passwordCtrl,
decoration: InputDecoration(
labelText: T('user.password'),
border: const OutlineInputBorder(),
),
obscureText: true,
),
const SizedBox(height: GcSpace.md),
FilledButton(
onPressed: _loginOrRegister,
child: Text(_isLogin ? T('user.login') : T('user.register')),
),
TextButton(
onPressed: () => setState(() => _isLogin = !_isLogin),
child: Text(_isLogin ? T('user.switchToRegister') : T('user.switchToLogin')),
),
],
),
),
const SizedBox(height: GcSpace.lg),
// 二选一
GcCard(
padding: const EdgeInsets.all(GcSpace.lg),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
T('user.pick'),
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: GcSpace.md),
TextField(
controller: _option1Ctrl,
decoration: InputDecoration(
labelText: T('user.option1'),
border: const OutlineInputBorder(),
),
),
const SizedBox(height: GcSpace.md),
TextField(
controller: _option2Ctrl,
decoration: InputDecoration(
labelText: T('user.option2'),
border: const OutlineInputBorder(),
),
),
const SizedBox(height: GcSpace.md),
FilledButton(
onPressed: _pickRandom,
child: Text(T('user.pickRandom')),
),
if (_randomResult != null) ...[
const SizedBox(height: GcSpace.md),
GcCard(
child: Text(
_randomResult!,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
),
),
],
],
),
),
const SizedBox(height: GcSpace.lg),
// 主题颜色
GcCard(
padding: const EdgeInsets.all(GcSpace.lg),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
T('user.theme'),
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: GcSpace.md),
Wrap(
spacing: GcSpace.md,
runSpacing: GcSpace.md,
children: ThemeColor.predefined.map((c) {
final canAfford = _user == null || _user!.points >= c.cost;
final isCurrent = _user?.themeColor == '0x$c.hex';
return InkWell(
onTap: canAfford ? () => _changeTheme(c.hex, c.cost) : null,
borderRadius: BorderRadius.circular(8),
child: Container(
width: 60,
height: 60,
decoration: BoxDecoration(
color: Color(int.parse('0xFF${c.hex}')),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: isCurrent ? GcColors.accent : GcColors.line,
width: isCurrent ? 2 : 1,
),
),
child: Stack(
children: <Widget>[
if (c.cost > 0 && !_canAfford(c.cost))
Container(
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.5),
borderRadius: BorderRadius.circular(8),
),
child: Center(
child: Text(
'${c.cost}',
style: const TextStyle(color: Colors.white, fontSize: 12),
),
),
),
if (isCurrent)
const Positioned(
bottom: 2,
right: 2,
child: Icon(Icons.check_rounded, size: 16, color: Colors.white),
),
],
),
),
);
}).toList(),
),
],
),
),
if (_error != null) ...[
const SizedBox(height: GcSpace.md),
Text(_error!, style: const TextStyle(color: Colors.red)),
],
],
),
),
);
}
bool _canAfford(int cost) {
if (cost <= 0) return true;
if (_user == null) return false;
return _user!.points >= cost;
}
}