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>
This commit is contained in:
2026-05-23 13:10:10 +08:00
parent fee7291ab9
commit 4ad57dc1cf
4 changed files with 467 additions and 153 deletions

View File

@@ -28,6 +28,7 @@ class _HomePageState extends State<HomePage> {
UserPreviewDataBean? _data;
String? _error;
bool _loading = true;
bool _loggingOut = false;
static const _tabTitles = [AppStrings.tabHome, AppStrings.tabTickets, AppStrings.tabSettings, AppStrings.tabAbout];
@@ -89,6 +90,22 @@ class _HomePageState extends State<HomePage> {
}
}
Future<void> _performLogout() async {
setState(() => _loggingOut = true);
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
Widget build(BuildContext context) {
final theme = Theme.of(context);
@@ -98,9 +115,15 @@ class _HomePageState extends State<HomePage> {
title: Text(_tabTitles[_currentTab]),
actions: [
IconButton(
icon: const Icon(Icons.logout),
icon: _loggingOut
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.logout),
tooltip: AppStrings.logoutTooltip,
onPressed: () => Navigator.of(context).pop(),
onPressed: _loggingOut ? null : _performLogout,
),
],
),

View File

@@ -11,7 +11,7 @@ enum TicketStep {
checkPreview,
userLogin,
fetchData,
simulatePlay,
chargeTicket,
uploadPlaylog,
upsertAll,
logout,
@@ -31,22 +31,12 @@ class TicketPage extends StatefulWidget {
class _TicketPageState extends State<TicketPage> {
static const _ticketNameMap = {
1: '?',
2: '2倍票',
3: '3倍票',
4: '4倍票',
5: '5倍票',
6: '6倍票',
7: '?',
8: '?',
9: '?',
10: '?',
11: '?',
12: '?',
13: '?',
14: '?',
15: '?',
16: '?',
1: '🈚️功能票',
2: '2倍功能',
3: '3倍功能',
4: '4倍功能',
5: '5倍功能',
6: '6倍功能'
};
final _formKey = GlobalKey<FormState>();
@@ -66,6 +56,10 @@ class _TicketPageState extends State<TicketPage> {
List<Map<String, dynamic>>? _tickets;
bool _ticketsLoading = false;
String? _ticketsError;
int? _selectedTicketId;
// Rewards from upsertUserAll response
List<Map<String, dynamic>>? _rewards;
String _ticketName(int chargeId) =>
_ticketNameMap[chargeId] ?? '$chargeId';
@@ -107,10 +101,14 @@ class _TicketPageState extends State<TicketPage> {
Future<void> _loadTickets() async {
if (!TitleServerConfigHolder().isConfigured) return;
// ignore: avoid_print
print('[TICKET] ===== _loadTickets =====');
setState(() {
_ticketsLoading = true;
_ticketsError = null;
_tickets = null;
_selectedTicketId = null;
});
try {
@@ -121,12 +119,22 @@ class _TicketPageState extends State<TicketPage> {
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(() {
_tickets = rawList.map((e) => e as Map<String, dynamic>).toList();
_ticketsLoading = false;
});
} catch (e) {
if (!mounted) return;
// ignore: avoid_print
print('[TICKET] _loadTickets ERROR: $e');
setState(() {
_ticketsError = e.toString();
_ticketsLoading = false;
@@ -135,6 +143,26 @@ class _TicketPageState extends State<TicketPage> {
}
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 (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
@@ -149,28 +177,100 @@ class _TicketPageState extends State<TicketPage> {
final musicData = _buildMusicData();
final userId = widget.userId;
final token = widget.token;
final ticketId = _selectedTicketId!;
// ignore: avoid_print
print('[TICKET] musicData: $musicData');
// ignore: avoid_print
print('[TICKET] selectedTicketId: $ticketId (${_ticketName(ticketId)})');
setState(() {
_running = true;
_error = null;
_rewards = null;
});
var loginId = 0;
String lastLoginDate = '';
int loginTimestamp = 0;
try {
// Step 1: Check preview
// ignore: avoid_print
print('[TICKET] ── Step 1: getUserPreview ──');
_updateStep(TicketStep.checkPreview);
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) {
throw TitleApiException(AppStrings.ticketErrorAlreadyLogin);
// ignore: avoid_print
print('[TICKET] user already logged in, force-logout stale session...');
try {
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
// ignore: avoid_print
print('[TICKET] ── Step 2: userLoginFull ──');
_updateStep(TicketStep.userLogin);
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
// Step 3: Get user data (for playerRating)
// ignore: avoid_print
print('[TICKET] ── Step 3: getUserData ──');
_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([
service.getUserData(userId),
Future.value(userData),
service.getUserExtend(userId),
service.getUserOption(userId),
service.getUserRating(userId),
@@ -178,44 +278,70 @@ class _TicketPageState extends State<TicketPage> {
service.getUserActivity(userId),
service.getUserMissionData(userId),
]);
// ignore: avoid_print
print('[TICKET] all fetches done, calling upsertUserAll...');
// Step 4: Simulate play time
_updateStep(TicketStep.simulatePlay);
await Future.delayed(const Duration(seconds: 1));
// Step 5: Upload playlog
_updateStep(TicketStep.uploadPlaylog);
await service.uploadUserPlaylog(
final userAllJson = await service.upsertUserAll(
userId: userId,
loginId: loginResult.loginId,
musicData: musicData,
userData: results[0],
);
// Step 6: Upsert all
_updateStep(TicketStep.upsertAll);
await service.upsertUserAll(
userId: userId,
loginId: loginResult.loginId,
loginDate: loginResult.lastLoginDate,
loginId: loginId,
loginDate: lastLoginDate,
musicData: musicData,
generalUserInfo: results,
ticketId: ticketId,
);
// Step 7: Logout
_updateStep(TicketStep.logout);
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
await service.userLogout(userId: userId, loginDateTime: now);
// Check rewards
final upsertAll = userAllJson['upsertUserAll'] as Map<String, dynamic>?;
final pointList = upsertAll?['userGetPointList'] as List<dynamic>?;
// 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) {
// ignore: avoid_print
print('[TICKET] ===== TitleApiException: ${e.message} =====');
_updateStep(TicketStep.failed, e.message);
setState(() => _error = e.message);
} catch (e) {
// ignore: avoid_print
print('[TICKET] ===== EXCEPTION: $e =====');
_updateStep(TicketStep.failed, e.toString());
setState(() => _error = e.toString());
} finally {
setState(() => _running = false);
// Step 7: Logout (always)
if (loginId != 0) {
// ignore: avoid_print
print('[TICKET] ── Step 7: userLogout (loginId=$loginId, loginTimestamp=$loginTimestamp) ──');
try {
_updateStep(TicketStep.logout);
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);
}
}
}
@@ -266,7 +392,9 @@ class _TicketPageState extends State<TicketPage> {
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
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(
@@ -276,6 +404,8 @@ class _TicketPageState extends State<TicketPage> {
children: [
Row(
children: [
Icon(Icons.confirmation_number, size: 18, color: theme.colorScheme.primary),
const SizedBox(width: 8),
Expanded(
child: Text(
AppStrings.myTickets,
@@ -354,62 +484,88 @@ class _TicketPageState extends State<TicketPage> {
final stock = (ticket['stock'] as num?)?.toInt() ?? 0;
final validDate = ticket['validDate'] as String? ?? '-';
final name = _ticketName(chargeId);
final isSelected = _selectedTicketId == chargeId;
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
children: [
Container(
width: 28,
height: 28,
decoration: BoxDecoration(
color: theme.colorScheme.primaryContainer,
borderRadius: BorderRadius.circular(6),
),
alignment: Alignment.center,
child: Text(
'$chargeId',
style: theme.textTheme.labelSmall?.copyWith(
color: theme.colorScheme.onPrimaryContainer,
fontWeight: FontWeight.w600,
return InkWell(
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(
children: [
Container(
width: 28,
height: 28,
decoration: BoxDecoration(
color: isSelected
? theme.colorScheme.primary
: theme.colorScheme.primaryContainer,
borderRadius: BorderRadius.circular(6),
),
alignment: Alignment.center,
child: Text(
'$chargeId',
style: theme.textTheme.labelSmall?.copyWith(
color: isSelected
? theme.colorScheme.onPrimary
: theme.colorScheme.onPrimaryContainer,
fontWeight: FontWeight.w600,
),
),
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
name,
style: theme.textTheme.bodyMedium?.copyWith(
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
name,
style: theme.textTheme.bodyMedium?.copyWith(
fontWeight: isSelected ? FontWeight.w700 : FontWeight.w600,
color: isSelected ? theme.colorScheme.primary : null,
),
),
Text(
'有效期: $validDate',
style: theme.textTheme.labelSmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
],
),
),
if (isSelected)
Icon(Icons.check_circle, size: 18, color: theme.colorScheme.primary)
else
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration(
color: stock > 0 ? Colors.green.withValues(alpha: 0.15) : Colors.grey.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(6),
),
child: Text(
'剩余 $stock',
style: theme.textTheme.labelSmall?.copyWith(
color: stock > 0 ? Colors.green : Colors.grey,
fontWeight: FontWeight.w600,
),
),
Text(
'有效期: $validDate',
style: theme.textTheme.labelSmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
],
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration(
color: stock > 0 ? Colors.green.withValues(alpha: 0.15) : Colors.grey.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(6),
),
child: Text(
'剩余 $stock',
style: theme.textTheme.labelSmall?.copyWith(
color: stock > 0 ? Colors.green : Colors.grey,
fontWeight: FontWeight.w600,
),
),
),
],
],
),
),
);
}
@@ -490,37 +646,68 @@ class _TicketPageState extends State<TicketPage> {
}
Widget _buildRunButton(ThemeData theme) {
return SizedBox(
width: double.infinity,
child: FilledButton.icon(
onPressed: _running ? null : _runTicket,
icon: _running
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
)
: const Icon(Icons.play_arrow, size: 20),
label: Text(_running ? AppStrings.runningTicket : AppStrings.runTicket),
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
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,
child: FilledButton.icon(
onPressed: _running ? null : _runTicket,
icon: _running
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
)
: const Icon(Icons.play_arrow, size: 20),
label: Text(_running ? AppStrings.runningTicket : AppStrings.runTicket),
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
),
),
],
);
}
Widget _buildProgressCard(ThemeData theme) {
final isFailed = _step == TicketStep.failed;
final isDone = _step == TicketStep.complete;
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(
color: _step == TicketStep.failed
color: isFailed
? theme.colorScheme.error.withValues(alpha: 0.5)
: theme.colorScheme.outline.withValues(alpha: 0.3),
: isDone
? Colors.green.withValues(alpha: 0.5)
: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
child: Padding(
@@ -528,20 +715,10 @@ class _TicketPageState extends State<TicketPage> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
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.userLogin, AppStrings.stepUserLogin, 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.upsertAll, AppStrings.stepUpsertAll, theme),
_stepRow(TicketStep.logout, AppStrings.stepLogout, theme),
@@ -551,16 +728,18 @@ class _TicketPageState extends State<TicketPage> {
width: double.infinity,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: _step == TicketStep.failed
color: isFailed
? theme.colorScheme.errorContainer
: theme.colorScheme.surfaceContainerHighest,
: isDone
? Colors.green.withValues(alpha: 0.1)
: theme.colorScheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(8),
),
child: Text(
_stepMessage,
style: theme.textTheme.bodySmall?.copyWith(
fontFamily: 'monospace',
color: _step == TicketStep.failed
color: isFailed
? theme.colorScheme.onErrorContainer
: theme.colorScheme.onSurface,
),
@@ -585,6 +764,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 +808,9 @@ class _TicketPageState extends State<TicketPage> {
IconData icon;
Color? color;
if (_step == TicketStep.failed && _step.index > step.index) {
icon = step.index < _step.index ? Icons.check_circle_outline : Icons.circle_outlined;
color = step.index < _step.index ? Colors.green : theme.colorScheme.onSurfaceVariant;
if (_step == TicketStep.failed && _step.index <= step.index) {
icon = _step == step ? Icons.error : Icons.circle_outlined;
color = _step == step ? theme.colorScheme.error : theme.colorScheme.onSurfaceVariant;
} else if (_step.index > step.index) {
icon = Icons.check_circle;
color = Colors.green;