Files
Ichino/lib/services/api_service.dart
T
Bad0RANG3 45214b11a0 refactor: 抽取高危功能页共享脚手架与传输层日志
诊断:功能票 / 解锁歌曲 / 收藏品 / 旅行伙伴 / 数值修改 / 一键跑图 /
万花筒 / 高危 hub 这些页面各自抄了一份同样的脚手架——冷却计时器、_Step
枚举、自动退登开关、卡片/提示/进度卡;TitleApiService.getUserPreview 还
重复实现了整套传输层;服务层有 37 处裸 print 会把 token / JSESSIONID
打进日志。

- 新增共享原语,7 个功能页切换到:
  * widgets/cooldown_mixin.dart  CooldownMixin(登录后冷却倒计时)
  * widgets/step_progress.dart   RiskStep + StepProgressCard
  * widgets/app_card.dart        AppCard / SectionTitle / AutoLogoutToggle
  * widgets/app_notice.dart      AppNotice + context.showSnack
- TitleApiService: getUserPreview 收敛进通用 _callApi;新增 fromHolder()
  工厂与 config getter,消除散落的 isConfigured + config!
- 传输层 print -> ApiLog(仅 debug 输出),删除全部 avoid_print 忽略
- UserAllPayloadBuilder 新增 placeholderMusicData()(原 5 处重复)
- LoginPage 从 main.dart 抽到 pages/login_page.dart,入口只留 main + MyApp
- 更新 README / CLAUDE.md 目录结构与已过时的 web/依赖说明

验证:flutter analyze 0 issue(含 info);flutter test 96/96 通过。
纯结构重构,行为保持不变;仅有意统一了功能票 / 音乐风险 / hub 的
卡片与步骤视觉样式。

净删除约 2176 行重复代码。
2026-09-28 14:50:51 +08:00

105 lines
2.9 KiB
Dart

import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:http/http.dart' as http;
import '../config/title_server_config.dart';
import 'api_log.dart';
class LoginResult {
final bool success;
final int errorId;
final int userId;
final String token;
const LoginResult({
required this.success,
required this.errorId,
required this.userId,
required this.token,
});
}
class ApiService {
final TitleServerConfig? _config;
ApiService([this._config]);
TitleServerConfig? get _cfg => _config ?? TitleServerConfigHolder().config;
String get _chipId => _cfg?.keychipId ?? '';
String get _aimeSalt => _cfg?.aimeSalt ?? '';
String get _aimeUrl => _cfg?.aimeUrl ?? '';
String get _openGameID {
final v = _cfg?.openGameID;
return (v != null && v.isNotEmpty) ? v : 'MAID';
}
String _formatTimestamp() {
final tokyo = DateTime.now().toUtc().add(const Duration(hours: 9));
final y = (tokyo.year % 100).toString().padLeft(2, '0');
final M = tokyo.month.toString().padLeft(2, '0');
final d = tokyo.day.toString().padLeft(2, '0');
final h = tokyo.hour.toString().padLeft(2, '0');
final m = tokyo.minute.toString().padLeft(2, '0');
final s = tokyo.second.toString().padLeft(2, '0');
return '$y$M$d$h$m$s';
}
String _sha256(String input) {
final bytes = utf8.encode(input);
return sha256.convert(bytes).toString();
}
static String extractQRCode(String qrCodeToken) {
if (qrCodeToken.length > 64) {
return qrCodeToken.substring(qrCodeToken.length - 64);
}
return qrCodeToken;
}
Future<LoginResult> login(String qrCodeToken) async {
final timestamp = _formatTimestamp();
final qrCode = extractQRCode(qrCodeToken);
final chipId = _chipId;
final rawKey = chipId + timestamp + _aimeSalt;
final key = _sha256(rawKey).toUpperCase();
final body = jsonEncode({
'chipID': chipId,
'openGameID': _openGameID,
'key': key,
'qrCode': qrCode,
'timestamp': timestamp,
});
ApiLog.log('[login] POST $_aimeUrl');
ApiLog.log('[login] chipId=$chipId openGameID=$_openGameID');
final response = await http
.post(
Uri.parse(_aimeUrl),
headers: {
'Content-Type': 'application/json',
'User-Agent': 'WC_AIME_LIB',
},
body: body,
)
.timeout(const Duration(seconds: 15));
final obj = jsonDecode(response.body) as Map<String, dynamic>;
final errorId = obj['errorID'] as int? ?? -1;
final userId = obj['userID'] as int? ?? -1;
final token = obj['token'] as String? ?? '';
// Session cookies (JSESSIONID) are captured from the title server's
// UserLoginApi response, not from the Aime server — see empurple.
return LoginResult(
success: errorId == 0,
errorId: errorId,
userId: userId,
token: token,
);
}
}