Compare commits

..

2 Commits

Author SHA1 Message Date
feed9b898a 1 2026-05-24 16:25:51 +08:00
4ad57dc1cf fix: correct ticket charge API body, logout type, and login/logout flow
- Fix upsertUserChargeLog: use separate userCharge + userChargelog objects
  matching Lionheart reference, validDate 90 days at 4AM
- Fix userLogout: use LogoutType.Logout = 1 (was incorrectly changed to 0)
- Fix loginDateTime tracking: userLoginFull now returns the exact timestamp
  sent to server, ensuring logout uses matching value
- Fix home page logout: call UserLogoutApi before navigating back
- Add force-logout for stale sessions before ticket flow login
- Add comprehensive debug logging across entire ticket flow

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-23 13:10:10 +08:00
5 changed files with 501 additions and 161 deletions

View File

@@ -16,7 +16,7 @@ android {
defaultConfig { defaultConfig {
applicationId = "com.example.rapollo" applicationId = "com.example.rapollo"
minSdk = 23 minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode versionCode = flutter.versionCode
versionName = flutter.versionName versionName = flutter.versionName

View File

@@ -76,7 +76,7 @@ class AppStrings {
static const stepCheckPreview = '检查登录状态'; static const stepCheckPreview = '检查登录状态';
static const stepUserLogin = '登录游戏'; static const stepUserLogin = '登录游戏';
static const stepFetchData = '获取用户数据'; static const stepFetchData = '获取用户数据';
static const stepSimulatePlay = '模拟游戏时间'; static const stepChargeTicket = '使用功能票';
static const stepUploadPlaylog = '上传游玩记录'; static const stepUploadPlaylog = '上传游玩记录';
static const stepUpsertAll = '上传用户数据'; static const stepUpsertAll = '上传用户数据';
static const stepLogout = '退出游戏'; static const stepLogout = '退出游戏';
@@ -92,6 +92,9 @@ class AppStrings {
static const loading = '加载中...'; static const loading = '加载中...';
static const ticketsNotLoaded = '点击刷新按钮加载功能票。'; static const ticketsNotLoaded = '点击刷新按钮加载功能票。';
static const noTickets = '暂无功能票。'; static const noTickets = '暂无功能票。';
static const ticketNotSelected = '请先在功能票列表中选中一张票。';
static const selectedTicket = '已选票';
static const rewards = '获得奖励';
// Settings // Settings
static const titleServerSettings = 'Title Server 设置'; static const titleServerSettings = 'Title Server 设置';

View File

@@ -28,6 +28,7 @@ class _HomePageState extends State<HomePage> {
UserPreviewDataBean? _data; UserPreviewDataBean? _data;
String? _error; String? _error;
bool _loading = true; bool _loading = true;
bool _loggingOut = false;
static const _tabTitles = [AppStrings.tabHome, AppStrings.tabTickets, AppStrings.tabSettings, AppStrings.tabAbout]; static const _tabTitles = [AppStrings.tabHome, AppStrings.tabTickets, AppStrings.tabSettings, AppStrings.tabAbout];
@@ -89,6 +90,23 @@ class _HomePageState extends State<HomePage> {
} }
} }
Future<void> _performLogout() async {
setState(() => _loggingOut = true);
await Future.delayed(const Duration(seconds: 5));
try {
if (TitleServerConfigHolder().isConfigured && _data != null && _data!.isLogin) {
final service = TitleApiService(TitleServerConfigHolder().config!);
final loginDateTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;
await service.userLogout(userId: widget.userId, loginDateTime: loginDateTime);
}
} catch (_) {
// Proceed with navigation even if logout fails
}
if (mounted) {
Navigator.of(context).pop();
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final theme = Theme.of(context); final theme = Theme.of(context);
@@ -97,10 +115,20 @@ class _HomePageState extends State<HomePage> {
appBar: AppBar( appBar: AppBar(
title: Text(_tabTitles[_currentTab]), title: Text(_tabTitles[_currentTab]),
actions: [ actions: [
if (_loggingOut)
const Padding(
padding: EdgeInsets.only(right: 12),
child: SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
),
)
else
IconButton( IconButton(
icon: const Icon(Icons.logout), icon: const Icon(Icons.logout),
tooltip: AppStrings.logoutTooltip, tooltip: AppStrings.logoutTooltip,
onPressed: () => Navigator.of(context).pop(), onPressed: _performLogout,
), ),
], ],
), ),

View File

@@ -11,7 +11,7 @@ enum TicketStep {
checkPreview, checkPreview,
userLogin, userLogin,
fetchData, fetchData,
simulatePlay, chargeTicket,
uploadPlaylog, uploadPlaylog,
upsertAll, upsertAll,
logout, logout,
@@ -31,22 +31,12 @@ class TicketPage extends StatefulWidget {
class _TicketPageState extends State<TicketPage> { class _TicketPageState extends State<TicketPage> {
static const _ticketNameMap = { static const _ticketNameMap = {
1: '?', 1: '🈚️功能票',
2: '2倍票', 2: '2倍功能',
3: '3倍票', 3: '3倍功能',
4: '4倍票', 4: '4倍功能',
5: '5倍票', 5: '5倍功能',
6: '6倍票', 6: '6倍功能'
7: '?',
8: '?',
9: '?',
10: '?',
11: '?',
12: '?',
13: '?',
14: '?',
15: '?',
16: '?',
}; };
final _formKey = GlobalKey<FormState>(); final _formKey = GlobalKey<FormState>();
@@ -66,6 +56,10 @@ class _TicketPageState extends State<TicketPage> {
List<Map<String, dynamic>>? _tickets; List<Map<String, dynamic>>? _tickets;
bool _ticketsLoading = false; bool _ticketsLoading = false;
String? _ticketsError; String? _ticketsError;
int? _selectedTicketId;
// Rewards from upsertUserAll response
List<Map<String, dynamic>>? _rewards;
String _ticketName(int chargeId) => String _ticketName(int chargeId) =>
_ticketNameMap[chargeId] ?? '$chargeId'; _ticketNameMap[chargeId] ?? '$chargeId';
@@ -107,10 +101,14 @@ class _TicketPageState extends State<TicketPage> {
Future<void> _loadTickets() async { Future<void> _loadTickets() async {
if (!TitleServerConfigHolder().isConfigured) return; if (!TitleServerConfigHolder().isConfigured) return;
// ignore: avoid_print
print('[TICKET] ===== _loadTickets =====');
setState(() { setState(() {
_ticketsLoading = true; _ticketsLoading = true;
_ticketsError = null; _ticketsError = null;
_tickets = null; _tickets = null;
_selectedTicketId = null;
}); });
try { try {
@@ -121,12 +119,22 @@ class _TicketPageState extends State<TicketPage> {
final rawList = data['userChargeList'] as List<dynamic>? ?? []; final rawList = data['userChargeList'] as List<dynamic>? ?? [];
// ignore: avoid_print
print('[TICKET] _loadTickets: got ${rawList.length} tickets');
for (var i = 0; i < rawList.length; i++) {
final t = rawList[i] as Map<String, dynamic>;
// ignore: avoid_print
print('[TICKET] ticket[$i] chargeId=${t['chargeId']} stock=${t['stock']} validDate=${t['validDate']}');
}
setState(() { setState(() {
_tickets = rawList.map((e) => e as Map<String, dynamic>).toList(); _tickets = rawList.map((e) => e as Map<String, dynamic>).toList();
_ticketsLoading = false; _ticketsLoading = false;
}); });
} catch (e) { } catch (e) {
if (!mounted) return; if (!mounted) return;
// ignore: avoid_print
print('[TICKET] _loadTickets ERROR: $e');
setState(() { setState(() {
_ticketsError = e.toString(); _ticketsError = e.toString();
_ticketsLoading = false; _ticketsLoading = false;
@@ -135,6 +143,26 @@ class _TicketPageState extends State<TicketPage> {
} }
Future<void> _runTicket() async { Future<void> _runTicket() async {
// ignore: avoid_print
print('');
// ignore: avoid_print
print('██████████████████████████████████████');
// ignore: avoid_print
print('[TICKET] ===== START _runTicket =====');
// ignore: avoid_print
print('[TICKET] userId=${widget.userId} token=${widget.token}');
// ignore: avoid_print
print('[TICKET] config: url=${TitleServerConfigHolder().config!.titleServerUrl} clientId=${TitleServerConfigHolder().config!.clientId} regionId=${TitleServerConfigHolder().config!.regionId} placeId=${TitleServerConfigHolder().config!.placeId}');
if (_selectedTicketId == null) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text(AppStrings.ticketNotSelected)),
);
}
return;
}
if (!TitleServerConfigHolder().isConfigured) { if (!TitleServerConfigHolder().isConfigured) {
if (mounted) { if (mounted) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
@@ -149,28 +177,108 @@ class _TicketPageState extends State<TicketPage> {
final musicData = _buildMusicData(); final musicData = _buildMusicData();
final userId = widget.userId; final userId = widget.userId;
final token = widget.token; final token = widget.token;
final ticketId = _selectedTicketId!;
// ignore: avoid_print
print('[TICKET] musicData: $musicData');
// ignore: avoid_print
print('[TICKET] selectedTicketId: $ticketId (${_ticketName(ticketId)})');
setState(() { setState(() {
_running = true; _running = true;
_error = null; _error = null;
_rewards = null;
}); });
var loginId = 0;
String lastLoginDate = '';
int loginTimestamp = 0;
try { try {
// Step 1: Check preview // Step 1: Check preview
// ignore: avoid_print
print('[TICKET] ── Step 1: getUserPreview ──');
_updateStep(TicketStep.checkPreview); _updateStep(TicketStep.checkPreview);
final preview = await service.getUserPreview(userId: userId, token: token); final preview = await service.getUserPreview(userId: userId, token: token);
// ignore: avoid_print
print('[TICKET] preview.isLogin=${preview.isLogin} errorId=${preview.errorId} userName=${preview.userName}');
if (preview.isLogin) { if (preview.isLogin) {
throw TitleApiException(AppStrings.ticketErrorAlreadyLogin); // ignore: avoid_print
print('[TICKET] user already logged in, force-logout stale session...');
try {
print('[TICKET] force-logout: waiting 5s...');
await Future.delayed(const Duration(seconds: 5));
print('[TICKET] force-logout: sending...');
await service.userLogout(userId: userId, loginDateTime: DateTime.now().millisecondsSinceEpoch ~/ 1000);
// ignore: avoid_print
print('[TICKET] force-logout done');
} catch (e) {
// ignore: avoid_print
print('[TICKET] force-logout failed (ignored): $e');
}
} }
// Step 2: Login // Step 2: Login
// ignore: avoid_print
print('[TICKET] ── Step 2: userLoginFull ──');
_updateStep(TicketStep.userLogin); _updateStep(TicketStep.userLogin);
final loginResult = await service.userLoginFull(userId: userId, token: token); final loginResult = await service.userLoginFull(userId: userId, token: token);
loginId = loginResult.loginId;
lastLoginDate = loginResult.lastLoginDate;
loginTimestamp = loginResult.loginDateTime;
// ignore: avoid_print
print('[TICKET] loginId=$loginId lastLoginDate=$lastLoginDate loginDateTime=$loginTimestamp newToken=${loginResult.token}');
// Step 3: Fetch user data // Wait 60s after LoginPacket for stability
// ignore: avoid_print
print('[TICKET] waiting 60s after LoginPacket...');
await Future.delayed(const Duration(seconds: 60));
// Step 3: Get user data (for playerRating)
// ignore: avoid_print
print('[TICKET] ── Step 3: getUserData ──');
_updateStep(TicketStep.fetchData); _updateStep(TicketStep.fetchData);
final userData = await service.getUserData(userId);
if (!mounted) return;
final playerRating = ((userData['userData'] as Map<String, dynamic>?)?['playerRating'] as num?)?.toInt() ?? 0;
// ignore: avoid_print
print('[TICKET] playerRating=$playerRating');
// Step 4: Use ticket (UpsertUserChargelogApi)
// ignore: avoid_print
print('[TICKET] ── Step 4: upsertUserChargeLog (ticketId=$ticketId) ──');
_updateStep(TicketStep.chargeTicket);
await service.upsertUserChargeLog(
userId: userId,
ticketId: ticketId,
loginDate: lastLoginDate,
playerRating: playerRating,
);
// ignore: avoid_print
print('[TICKET] upsertUserChargeLog OK');
// Step 5: Upload playlog (using ticket)
// ignore: avoid_print
print('[TICKET] ── Step 5: uploadUserPlaylog ──');
_updateStep(TicketStep.uploadPlaylog);
await service.uploadUserPlaylog(
userId: userId,
loginId: loginId,
musicData: musicData,
userData: userData,
ticketId: ticketId,
);
// ignore: avoid_print
print('[TICKET] uploadUserPlaylog OK');
// Step 6: Fetch remaining data + upsertAll
// ignore: avoid_print
print('[TICKET] ── Step 6: fetch all + upsertUserAll ──');
_updateStep(TicketStep.upsertAll);
// ignore: avoid_print
print('[TICKET] parallel fetching: userExtend, userOption, userRating, userCharge, userActivity, userMissionData...');
final results = await Future.wait([ final results = await Future.wait([
service.getUserData(userId), Future.value(userData),
service.getUserExtend(userId), service.getUserExtend(userId),
service.getUserOption(userId), service.getUserOption(userId),
service.getUserRating(userId), service.getUserRating(userId),
@@ -178,46 +286,75 @@ class _TicketPageState extends State<TicketPage> {
service.getUserActivity(userId), service.getUserActivity(userId),
service.getUserMissionData(userId), service.getUserMissionData(userId),
]); ]);
// ignore: avoid_print
print('[TICKET] all fetches done, calling upsertUserAll...');
// Step 4: Simulate play time final userAllJson = await service.upsertUserAll(
_updateStep(TicketStep.simulatePlay);
await Future.delayed(const Duration(seconds: 1));
// Step 5: Upload playlog
_updateStep(TicketStep.uploadPlaylog);
await service.uploadUserPlaylog(
userId: userId, userId: userId,
loginId: loginResult.loginId, loginId: loginId,
musicData: musicData, loginDate: lastLoginDate,
userData: results[0],
);
// Step 6: Upsert all
_updateStep(TicketStep.upsertAll);
await service.upsertUserAll(
userId: userId,
loginId: loginResult.loginId,
loginDate: loginResult.lastLoginDate,
musicData: musicData, musicData: musicData,
generalUserInfo: results, generalUserInfo: results,
ticketId: ticketId,
); );
// Step 7: Logout // Check rewards
_updateStep(TicketStep.logout); final upsertAll = userAllJson['upsertUserAll'] as Map<String, dynamic>?;
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; final pointList = upsertAll?['userGetPointList'] as List<dynamic>?;
await service.userLogout(userId: userId, loginDateTime: now); // ignore: avoid_print
print('[TICKET] upsertUserAll returnCode=${userAllJson['returnCode']}');
if (pointList != null && pointList.isNotEmpty) {
_rewards = pointList.map((e) => e as Map<String, dynamic>).toList();
// ignore: avoid_print
print('[TICKET] rewards: $_rewards');
} else {
// ignore: avoid_print
print('[TICKET] no rewards (userGetPointList empty or null)');
}
_updateStep(TicketStep.complete, AppStrings.ticketLoginInfo(loginResult.loginId)); // ignore: avoid_print
print('[TICKET] ===== TICKET COMPLETE =====');
_updateStep(TicketStep.complete, AppStrings.ticketLoginInfo(loginId));
} on TitleApiException catch (e) { } on TitleApiException catch (e) {
// ignore: avoid_print
print('[TICKET] ===== TitleApiException: ${e.message} =====');
_updateStep(TicketStep.failed, e.message); _updateStep(TicketStep.failed, e.message);
setState(() => _error = e.message); setState(() => _error = e.message);
} catch (e) { } catch (e) {
// ignore: avoid_print
print('[TICKET] ===== EXCEPTION: $e =====');
_updateStep(TicketStep.failed, e.toString()); _updateStep(TicketStep.failed, e.toString());
setState(() => _error = e.toString()); setState(() => _error = e.toString());
} finally { } finally {
// Step 7: Logout (always)
if (loginId != 0) {
// ignore: avoid_print
print('[TICKET] ── Step 7: userLogout (loginId=$loginId, loginTimestamp=$loginTimestamp) ──');
try {
_updateStep(TicketStep.logout);
print('[TICKET] ── Step 7: userLogout ── waiting 5s...');
await Future.delayed(const Duration(seconds: 5));
print('[TICKET] ── Step 7: userLogout ── sending...');
await service.userLogout(userId: userId, loginDateTime: loginTimestamp);
// ignore: avoid_print
print('[TICKET] userLogout OK');
} catch (e) {
// ignore: avoid_print
print('[TICKET] userLogout ERROR (ignored): $e');
}
} else {
// ignore: avoid_print
print('[TICKET] ── Step 7: SKIP logout (loginId=0) ──');
}
// ignore: avoid_print
print('██████████████████████████████████████');
// ignore: avoid_print
print('');
if (mounted) {
setState(() => _running = false); setState(() => _running = false);
} }
} }
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -266,7 +403,9 @@ class _TicketPageState extends State<TicketPage> {
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16), borderRadius: BorderRadius.circular(16),
side: BorderSide( side: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3), color: _selectedTicketId != null
? theme.colorScheme.primary.withValues(alpha: 0.5)
: theme.colorScheme.outline.withValues(alpha: 0.3),
), ),
), ),
child: Padding( child: Padding(
@@ -276,6 +415,8 @@ class _TicketPageState extends State<TicketPage> {
children: [ children: [
Row( Row(
children: [ children: [
Icon(Icons.confirmation_number, size: 18, color: theme.colorScheme.primary),
const SizedBox(width: 8),
Expanded( Expanded(
child: Text( child: Text(
AppStrings.myTickets, AppStrings.myTickets,
@@ -354,23 +495,44 @@ class _TicketPageState extends State<TicketPage> {
final stock = (ticket['stock'] as num?)?.toInt() ?? 0; final stock = (ticket['stock'] as num?)?.toInt() ?? 0;
final validDate = ticket['validDate'] as String? ?? '-'; final validDate = ticket['validDate'] as String? ?? '-';
final name = _ticketName(chargeId); final name = _ticketName(chargeId);
final isSelected = _selectedTicketId == chargeId;
return Padding( return InkWell(
padding: const EdgeInsets.symmetric(vertical: 4), borderRadius: BorderRadius.circular(8),
onTap: _running
? null
: () {
setState(() {
_selectedTicketId = isSelected ? null : chargeId;
});
},
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 8),
decoration: BoxDecoration(
color: isSelected
? theme.colorScheme.primaryContainer.withValues(alpha: 0.5)
: null,
borderRadius: BorderRadius.circular(8),
),
child: Row( child: Row(
children: [ children: [
Container( Container(
width: 28, width: 28,
height: 28, height: 28,
decoration: BoxDecoration( decoration: BoxDecoration(
color: theme.colorScheme.primaryContainer, color: isSelected
? theme.colorScheme.primary
: theme.colorScheme.primaryContainer,
borderRadius: BorderRadius.circular(6), borderRadius: BorderRadius.circular(6),
), ),
alignment: Alignment.center, alignment: Alignment.center,
child: Text( child: Text(
'$chargeId', '$chargeId',
style: theme.textTheme.labelSmall?.copyWith( style: theme.textTheme.labelSmall?.copyWith(
color: theme.colorScheme.onPrimaryContainer, color: isSelected
? theme.colorScheme.onPrimary
: theme.colorScheme.onPrimaryContainer,
fontWeight: FontWeight.w600, fontWeight: FontWeight.w600,
), ),
), ),
@@ -383,7 +545,8 @@ class _TicketPageState extends State<TicketPage> {
Text( Text(
name, name,
style: theme.textTheme.bodyMedium?.copyWith( style: theme.textTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.w600, fontWeight: isSelected ? FontWeight.w700 : FontWeight.w600,
color: isSelected ? theme.colorScheme.primary : null,
), ),
), ),
Text( Text(
@@ -395,6 +558,9 @@ class _TicketPageState extends State<TicketPage> {
], ],
), ),
), ),
if (isSelected)
Icon(Icons.check_circle, size: 18, color: theme.colorScheme.primary)
else
Container( Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4), padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration( decoration: BoxDecoration(
@@ -411,6 +577,7 @@ class _TicketPageState extends State<TicketPage> {
), ),
], ],
), ),
),
); );
} }
@@ -490,7 +657,31 @@ class _TicketPageState extends State<TicketPage> {
} }
Widget _buildRunButton(ThemeData theme) { Widget _buildRunButton(ThemeData theme) {
return SizedBox( final selectedName = _selectedTicketId != null
? _ticketName(_selectedTicketId!)
: null;
return Column(
children: [
if (selectedName != null) ...[
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
decoration: BoxDecoration(
color: theme.colorScheme.primaryContainer.withValues(alpha: 0.4),
borderRadius: BorderRadius.circular(10),
),
child: Text(
'${AppStrings.selectedTicket}: $selectedName (ID: $_selectedTicketId)',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
),
const SizedBox(height: 12),
],
SizedBox(
width: double.infinity, width: double.infinity,
child: FilledButton.icon( child: FilledButton.icon(
onPressed: _running ? null : _runTicket, onPressed: _running ? null : _runTicket,
@@ -509,17 +700,24 @@ class _TicketPageState extends State<TicketPage> {
), ),
), ),
), ),
),
],
); );
} }
Widget _buildProgressCard(ThemeData theme) { Widget _buildProgressCard(ThemeData theme) {
final isFailed = _step == TicketStep.failed;
final isDone = _step == TicketStep.complete;
return Card( return Card(
elevation: 0, elevation: 0,
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16), borderRadius: BorderRadius.circular(16),
side: BorderSide( side: BorderSide(
color: _step == TicketStep.failed color: isFailed
? theme.colorScheme.error.withValues(alpha: 0.5) ? theme.colorScheme.error.withValues(alpha: 0.5)
: isDone
? Colors.green.withValues(alpha: 0.5)
: theme.colorScheme.outline.withValues(alpha: 0.3), : theme.colorScheme.outline.withValues(alpha: 0.3),
), ),
), ),
@@ -528,20 +726,10 @@ class _TicketPageState extends State<TicketPage> {
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text(
_step == TicketStep.failed ? AppStrings.stepFailed : AppStrings.ticketsTitle,
style: theme.textTheme.labelLarge?.copyWith(
color: _step == TicketStep.failed
? theme.colorScheme.error
: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 14),
_stepRow(TicketStep.checkPreview, AppStrings.stepCheckPreview, theme), _stepRow(TicketStep.checkPreview, AppStrings.stepCheckPreview, theme),
_stepRow(TicketStep.userLogin, AppStrings.stepUserLogin, theme), _stepRow(TicketStep.userLogin, AppStrings.stepUserLogin, theme),
_stepRow(TicketStep.fetchData, AppStrings.stepFetchData, theme), _stepRow(TicketStep.fetchData, AppStrings.stepFetchData, theme),
_stepRow(TicketStep.simulatePlay, AppStrings.stepSimulatePlay, theme), _stepRow(TicketStep.chargeTicket, AppStrings.stepChargeTicket, theme),
_stepRow(TicketStep.uploadPlaylog, AppStrings.stepUploadPlaylog, theme), _stepRow(TicketStep.uploadPlaylog, AppStrings.stepUploadPlaylog, theme),
_stepRow(TicketStep.upsertAll, AppStrings.stepUpsertAll, theme), _stepRow(TicketStep.upsertAll, AppStrings.stepUpsertAll, theme),
_stepRow(TicketStep.logout, AppStrings.stepLogout, theme), _stepRow(TicketStep.logout, AppStrings.stepLogout, theme),
@@ -551,8 +739,10 @@ class _TicketPageState extends State<TicketPage> {
width: double.infinity, width: double.infinity,
padding: const EdgeInsets.all(10), padding: const EdgeInsets.all(10),
decoration: BoxDecoration( decoration: BoxDecoration(
color: _step == TicketStep.failed color: isFailed
? theme.colorScheme.errorContainer ? theme.colorScheme.errorContainer
: isDone
? Colors.green.withValues(alpha: 0.1)
: theme.colorScheme.surfaceContainerHighest, : theme.colorScheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
), ),
@@ -560,7 +750,7 @@ class _TicketPageState extends State<TicketPage> {
_stepMessage, _stepMessage,
style: theme.textTheme.bodySmall?.copyWith( style: theme.textTheme.bodySmall?.copyWith(
fontFamily: 'monospace', fontFamily: 'monospace',
color: _step == TicketStep.failed color: isFailed
? theme.colorScheme.onErrorContainer ? theme.colorScheme.onErrorContainer
: theme.colorScheme.onSurface, : theme.colorScheme.onSurface,
), ),
@@ -585,6 +775,40 @@ class _TicketPageState extends State<TicketPage> {
), ),
), ),
], ],
if (_rewards != null && _rewards!.isNotEmpty) ...[
const SizedBox(height: 12),
Container(
width: double.infinity,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.amber.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(8),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppStrings.rewards,
style: theme.textTheme.labelSmall?.copyWith(
fontWeight: FontWeight.w600,
color: Colors.amber.shade700,
),
),
const SizedBox(height: 6),
..._rewards!.map((r) {
final pointType = (r['pointType'] as num?)?.toInt() ?? 0;
final addPoint = (r['addPoint'] as num?)?.toInt() ?? 0;
return Text(
' - pointType: $pointType, point: $addPoint',
style: theme.textTheme.bodySmall?.copyWith(
fontFamily: 'monospace',
),
);
}),
],
),
),
],
], ],
), ),
), ),
@@ -595,9 +819,9 @@ class _TicketPageState extends State<TicketPage> {
IconData icon; IconData icon;
Color? color; Color? color;
if (_step == TicketStep.failed && _step.index > step.index) { if (_step == TicketStep.failed && _step.index <= step.index) {
icon = step.index < _step.index ? Icons.check_circle_outline : Icons.circle_outlined; icon = _step == step ? Icons.error : Icons.circle_outlined;
color = step.index < _step.index ? Colors.green : theme.colorScheme.onSurfaceVariant; color = _step == step ? theme.colorScheme.error : theme.colorScheme.onSurfaceVariant;
} else if (_step.index > step.index) { } else if (_step.index > step.index) {
icon = Icons.check_circle; icon = Icons.check_circle;
color = Colors.green; color = Colors.green;

View File

@@ -21,11 +21,13 @@ class UserLoginResult {
final String token; final String token;
final int loginId; final int loginId;
final String lastLoginDate; final String lastLoginDate;
final int loginDateTime;
const UserLoginResult({ const UserLoginResult({
required this.token, required this.token,
required this.loginId, required this.loginId,
required this.lastLoginDate, required this.lastLoginDate,
required this.loginDateTime,
}); });
} }
@@ -88,10 +90,16 @@ class TitleApiService {
} }
Map<String, dynamic> _processResponseBody(Uint8List bodyBytes) { Map<String, dynamic> _processResponseBody(Uint8List bodyBytes) {
try {
final decrypted = _aesDecrypt(bodyBytes); final decrypted = _aesDecrypt(bodyBytes);
final decompressed = _decompress(decrypted); final decompressed = _decompress(decrypted);
final jsonStr = utf8.decode(decompressed); final jsonStr = utf8.decode(decompressed);
return jsonDecode(jsonStr) as Map<String, dynamic>; return jsonDecode(jsonStr) as Map<String, dynamic>;
} on FormatException catch (e) {
throw TitleApiException('Response body is not valid JSON: $e');
} on Exception catch (e) {
throw TitleApiException('Failed to process response body: $e');
}
} }
String _buildHash(String apiName) { String _buildHash(String apiName) {
@@ -109,6 +117,25 @@ class TitleApiService {
final baseUrl = _normalizeUrl(_config.titleServerUrl); final baseUrl = _normalizeUrl(_config.titleServerUrl);
final url = Uri.parse('$baseUrl/$hash'); final url = Uri.parse('$baseUrl/$hash');
// ignore: avoid_print
print('══════════════════════════════════════');
// ignore: avoid_print
print('[$apiName] >>> REQUEST >>>');
// ignore: avoid_print
print('[$apiName] URL: $url');
// ignore: avoid_print
print('[$apiName] userId: $userId');
// ignore: avoid_print
print('[$apiName] hash: $hash');
// ignore: avoid_print
print('[$apiName] body (encrypted): ${body.length} bytes');
// ignore: avoid_print
final packetStr = const JsonEncoder.withIndent(' ').convert(packet);
// ignore: avoid_print
print('[$apiName] packet (plain):');
// ignore: avoid_print
print(packetStr);
final response = await http final response = await http
.post( .post(
url, url,
@@ -125,16 +152,27 @@ class TitleApiService {
) )
.timeout(const Duration(seconds: 15)); .timeout(const Duration(seconds: 15));
// ignore: avoid_print
print('[$apiName] HTTP status: ${response.statusCode}');
// ignore: avoid_print
print('[$apiName] response bytes: ${response.bodyBytes.length}');
if (response.statusCode != 200) { if (response.statusCode != 200) {
throw TitleApiException('$apiName returned ${response.statusCode}'); throw TitleApiException('$apiName returned ${response.statusCode}');
} }
if (response.bodyBytes.isEmpty) {
throw TitleApiException('$apiName returned empty body');
}
final json = _processResponseBody(response.bodyBytes); final json = _processResponseBody(response.bodyBytes);
final raw = const JsonEncoder.withIndent(' ').convert(json); final raw = const JsonEncoder.withIndent(' ').convert(json);
// ignore: avoid_print // ignore: avoid_print
print('[$apiName] === RESPONSE ==='); print('[$apiName] <<< RESPONSE <<<');
// ignore: avoid_print // ignore: avoid_print
print(raw); print(raw);
// ignore: avoid_print
print('══════════════════════════════════════');
return json; return json;
} }
@@ -380,9 +418,9 @@ class TitleApiService {
final json = await _callApi(apiName, packet, userId); final json = await _callApi(apiName, packet, userId);
final errorId = json['errorId'] as int? ?? -1; final returnCode = json['returnCode'] as int? ?? -1;
if (errorId != 0) { if (returnCode != 1) {
throw TitleApiException('UserLoginApi errorId=$errorId'); throw TitleApiException('UserLoginApi returnCode=$returnCode');
} }
final loginId = (json['loginId'] as num?)?.toInt() ?? 0; final loginId = (json['loginId'] as num?)?.toInt() ?? 0;
@@ -393,6 +431,7 @@ class TitleApiService {
token: newToken, token: newToken,
loginId: loginId, loginId: loginId,
lastLoginDate: lastLoginDate, lastLoginDate: lastLoginDate,
loginDateTime: now,
); );
} }
@@ -411,13 +450,55 @@ class TitleApiService {
'placeId': _config.placeId, 'placeId': _config.placeId,
'clientId': _config.clientId, 'clientId': _config.clientId,
'loginDateTime': loginDateTime, 'loginDateTime': loginDateTime,
'type': 1, 'type': 1, // LogoutType.Logout
}; };
final json = await _callApi(apiName, packet, userId); final json = await _callApi(apiName, packet, userId);
final errorId = json['errorId'] as int? ?? -1; final returnCode = json['returnCode'] as int? ?? -1;
if (errorId != 0) { if (returnCode != 1) {
throw TitleApiException('UserLogoutApi errorId=$errorId'); throw TitleApiException('UserLogoutApi returnCode=$returnCode');
}
}
// ---- UpsertUserChargeLog (使用功能票) ----
// Reference: Lionheart user.ts charge() method
Future<void> upsertUserChargeLog({
required int userId,
required int ticketId,
required String loginDate,
required int playerRating,
int price = 0,
}) async {
const apiName = 'UpsertUserChargelogApi';
final now = DateTime.now();
// validDate defaults to purchaseDate + 90 days at 4:00 AM
final validDate = DateTime(now.year, now.month, now.day, 4, 0, 0)
.add(const Duration(days: 90));
final purchaseDateStr = _formatDateTime(now);
final packet = {
'userId': userId,
'userCharge': {
'chargeId': ticketId,
'stock': 0,
'purchaseDate': purchaseDateStr,
'validDate': _formatDateTime(validDate),
},
'userChargelog': {
'chargeId': ticketId,
'clientId': _config.clientId,
'regionId': _config.regionId,
'placeId': _config.placeId,
'price': price,
'purchaseDate': purchaseDateStr,
},
};
final json = await _callApi(apiName, packet, userId);
final returnCode = json['returnCode'] as int? ?? -1;
if (returnCode != 1) {
throw TitleApiException('UpsertUserChargelogApi returnCode=$returnCode');
} }
} }
@@ -428,6 +509,7 @@ class TitleApiService {
required int loginId, required int loginId,
required Map<String, dynamic> musicData, required Map<String, dynamic> musicData,
required Map<String, dynamic> userData, required Map<String, dynamic> userData,
int? ticketId,
}) async { }) async {
const apiName = 'UploadUserPlaylogListApi'; const apiName = 'UploadUserPlaylogListApi';
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
@@ -435,14 +517,14 @@ class TitleApiService {
final packet = { final packet = {
'userId': userId, 'userId': userId,
'userPlaylogList': [ 'userPlaylogList': [
_buildPlaylogEntry(userId, loginId, now, musicData, userData), _buildPlaylogEntry(userId, loginId, now, musicData, userData, ticketId: ticketId),
], ],
}; };
final json = await _callApi(apiName, packet, userId); final json = await _callApi(apiName, packet, userId);
final errorId = json['errorId'] as int? ?? -1; final returnCode = json['returnCode'] as int? ?? -1;
if (errorId != 0) { if (returnCode != 1) {
throw TitleApiException('UploadUserPlaylogListApi errorId=$errorId'); throw TitleApiException('UploadUserPlaylogListApi returnCode=$returnCode');
} }
} }
@@ -451,8 +533,9 @@ class TitleApiService {
int loginId, int loginId,
int timestamp, int timestamp,
Map<String, dynamic> musicData, Map<String, dynamic> musicData,
Map<String, dynamic> userData, Map<String, dynamic> userData, {
) { int? ticketId,
}) {
final user = userData['userData'] as Map<String, dynamic>? ?? {}; final user = userData['userData'] as Map<String, dynamic>? ?? {};
final charaSlot = (user['charaSlot'] as List<dynamic>?) final charaSlot = (user['charaSlot'] as List<dynamic>?)
?.map((e) => (e as num).toInt()) ?.map((e) => (e as num).toInt())
@@ -574,12 +657,13 @@ class TitleApiService {
// ---- UpsertUserAll ---- // ---- UpsertUserAll ----
Future<void> upsertUserAll({ Future<Map<String, dynamic>> upsertUserAll({
required int userId, required int userId,
required int loginId, required int loginId,
required String loginDate, required String loginDate,
required Map<String, dynamic> musicData, required Map<String, dynamic> musicData,
required List<Map<String, dynamic>> generalUserInfo, required List<Map<String, dynamic>> generalUserInfo,
int? ticketId,
}) async { }) async {
const apiName = 'UpsertUserAllApi'; const apiName = 'UpsertUserAllApi';
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
@@ -601,7 +685,7 @@ class TitleApiService {
'isFreePlay': false, 'isFreePlay': false,
'loginDateTime': now, 'loginDateTime': now,
'userPlaylogList': [ 'userPlaylogList': [
_buildPlaylogEntry(userId, loginId, now, musicData, userData), _buildPlaylogEntry(userId, loginId, now, musicData, userData, ticketId: ticketId),
], ],
'upsertUserAll': { 'upsertUserAll': {
'userData': [ 'userData': [
@@ -722,7 +806,7 @@ class TitleApiService {
'version': user['lastRomVersion'] ?? 0, 'version': user['lastRomVersion'] ?? 0,
'playDate': _formatDateTime(DateTime.now()), 'playDate': _formatDateTime(DateTime.now()),
'playMode': 0, 'playMode': 0,
'useTicketId': -1, 'useTicketId': ticketId ?? -1,
'playCredit': 1, 'playCredit': 1,
'playTrack': 1, 'playTrack': 1,
'clientId': _config.clientId, 'clientId': _config.clientId,
@@ -764,10 +848,11 @@ class TitleApiService {
}; };
final json = await _callApi(apiName, packet, userId); final json = await _callApi(apiName, packet, userId);
final errorId = json['errorId'] as int? ?? -1; final returnCode = json['returnCode'] as int? ?? -1;
if (errorId != 0) { if (returnCode != 1) {
throw TitleApiException('UpsertUserAllApi errorId=$errorId'); throw TitleApiException('UpsertUserAllApi returnCode=$returnCode');
} }
return json;
} }
List<Map<String, dynamic>> _buildMissionDataList( List<Map<String, dynamic>> _buildMissionDataList(