From 4ad57dc1cfa39f3bf8171896c5c9dee7b79dae3f Mon Sep 17 00:00:00 2001 From: chuxuehaocai Date: Sat, 23 May 2026 13:10:10 +0800 Subject: [PATCH] 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 --- lib/config/strings.dart | 5 +- lib/pages/home_page.dart | 27 +- lib/pages/ticket_page.dart | 473 ++++++++++++++++++++-------- lib/services/title_api_service.dart | 115 +++++-- 4 files changed, 467 insertions(+), 153 deletions(-) diff --git a/lib/config/strings.dart b/lib/config/strings.dart index 37f4cb1..7d5c81a 100644 --- a/lib/config/strings.dart +++ b/lib/config/strings.dart @@ -76,7 +76,7 @@ class AppStrings { static const stepCheckPreview = '检查登录状态'; static const stepUserLogin = '登录游戏'; static const stepFetchData = '获取用户数据'; - static const stepSimulatePlay = '模拟游戏时间'; + static const stepChargeTicket = '使用功能票'; static const stepUploadPlaylog = '上传游玩记录'; static const stepUpsertAll = '上传用户数据'; static const stepLogout = '退出游戏'; @@ -92,6 +92,9 @@ class AppStrings { static const loading = '加载中...'; static const ticketsNotLoaded = '点击刷新按钮加载功能票。'; static const noTickets = '暂无功能票。'; + static const ticketNotSelected = '请先在功能票列表中选中一张票。'; + static const selectedTicket = '已选票'; + static const rewards = '获得奖励'; // Settings static const titleServerSettings = 'Title Server 设置'; diff --git a/lib/pages/home_page.dart b/lib/pages/home_page.dart index 56f11c2..46c80bc 100644 --- a/lib/pages/home_page.dart +++ b/lib/pages/home_page.dart @@ -28,6 +28,7 @@ class _HomePageState extends State { 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 { } } + Future _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 { 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, ), ], ), diff --git a/lib/pages/ticket_page.dart b/lib/pages/ticket_page.dart index 9068e51..f405af7 100644 --- a/lib/pages/ticket_page.dart +++ b/lib/pages/ticket_page.dart @@ -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 { 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(); @@ -66,6 +56,10 @@ class _TicketPageState extends State { List>? _tickets; bool _ticketsLoading = false; String? _ticketsError; + int? _selectedTicketId; + + // Rewards from upsertUserAll response + List>? _rewards; String _ticketName(int chargeId) => _ticketNameMap[chargeId] ?? '票$chargeId'; @@ -107,10 +101,14 @@ class _TicketPageState extends State { Future _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 { final rawList = data['userChargeList'] as List? ?? []; + // ignore: avoid_print + print('[TICKET] _loadTickets: got ${rawList.length} tickets'); + for (var i = 0; i < rawList.length; i++) { + final t = rawList[i] as Map; + // ignore: avoid_print + print('[TICKET] ticket[$i] chargeId=${t['chargeId']} stock=${t['stock']} validDate=${t['validDate']}'); + } + setState(() { _tickets = rawList.map((e) => e as Map).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 { } Future _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 { 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?)?['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 { 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?; + final pointList = upsertAll?['userGetPointList'] as List?; + // ignore: avoid_print + print('[TICKET] upsertUserAll returnCode=${userAllJson['returnCode']}'); + if (pointList != null && pointList.isNotEmpty) { + _rewards = pointList.map((e) => e as Map).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 { 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 { 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 { 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 { } 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 { 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 { 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 { ), ), ], + 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 { 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; diff --git a/lib/services/title_api_service.dart b/lib/services/title_api_service.dart index dc5a9c0..c3c46e2 100644 --- a/lib/services/title_api_service.dart +++ b/lib/services/title_api_service.dart @@ -21,11 +21,13 @@ class UserLoginResult { final String token; final int loginId; final String lastLoginDate; + final int loginDateTime; const UserLoginResult({ required this.token, required this.loginId, required this.lastLoginDate, + required this.loginDateTime, }); } @@ -109,6 +111,25 @@ class TitleApiService { final baseUrl = _normalizeUrl(_config.titleServerUrl); 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 .post( url, @@ -125,6 +146,11 @@ class TitleApiService { ) .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) { throw TitleApiException('$apiName returned ${response.statusCode}'); } @@ -132,9 +158,11 @@ class TitleApiService { final json = _processResponseBody(response.bodyBytes); final raw = const JsonEncoder.withIndent(' ').convert(json); // ignore: avoid_print - print('[$apiName] === RESPONSE ==='); + print('[$apiName] <<< RESPONSE <<<'); // ignore: avoid_print print(raw); + // ignore: avoid_print + print('══════════════════════════════════════'); return json; } @@ -380,9 +408,9 @@ class TitleApiService { final json = await _callApi(apiName, packet, userId); - final errorId = json['errorId'] as int? ?? -1; - if (errorId != 0) { - throw TitleApiException('UserLoginApi errorId=$errorId'); + final returnCode = json['returnCode'] as int? ?? -1; + if (returnCode != 1) { + throw TitleApiException('UserLoginApi returnCode=$returnCode'); } final loginId = (json['loginId'] as num?)?.toInt() ?? 0; @@ -393,6 +421,7 @@ class TitleApiService { token: newToken, loginId: loginId, lastLoginDate: lastLoginDate, + loginDateTime: now, ); } @@ -411,13 +440,55 @@ class TitleApiService { 'placeId': _config.placeId, 'clientId': _config.clientId, 'loginDateTime': loginDateTime, - 'type': 1, + 'type': 1, // LogoutType.Logout }; final json = await _callApi(apiName, packet, userId); - final errorId = json['errorId'] as int? ?? -1; - if (errorId != 0) { - throw TitleApiException('UserLogoutApi errorId=$errorId'); + final returnCode = json['returnCode'] as int? ?? -1; + if (returnCode != 1) { + throw TitleApiException('UserLogoutApi returnCode=$returnCode'); + } + } + + // ---- UpsertUserChargeLog (使用功能票) ---- + // Reference: Lionheart user.ts charge() method + + Future 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 +499,7 @@ class TitleApiService { required int loginId, required Map musicData, required Map userData, + int? ticketId, }) async { const apiName = 'UploadUserPlaylogListApi'; final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; @@ -435,14 +507,14 @@ class TitleApiService { final packet = { 'userId': userId, 'userPlaylogList': [ - _buildPlaylogEntry(userId, loginId, now, musicData, userData), + _buildPlaylogEntry(userId, loginId, now, musicData, userData, ticketId: ticketId), ], }; final json = await _callApi(apiName, packet, userId); - final errorId = json['errorId'] as int? ?? -1; - if (errorId != 0) { - throw TitleApiException('UploadUserPlaylogListApi errorId=$errorId'); + final returnCode = json['returnCode'] as int? ?? -1; + if (returnCode != 1) { + throw TitleApiException('UploadUserPlaylogListApi returnCode=$returnCode'); } } @@ -451,8 +523,9 @@ class TitleApiService { int loginId, int timestamp, Map musicData, - Map userData, - ) { + Map userData, { + int? ticketId, + }) { final user = userData['userData'] as Map? ?? {}; final charaSlot = (user['charaSlot'] as List?) ?.map((e) => (e as num).toInt()) @@ -574,12 +647,13 @@ class TitleApiService { // ---- UpsertUserAll ---- - Future upsertUserAll({ + Future> upsertUserAll({ required int userId, required int loginId, required String loginDate, required Map musicData, required List> generalUserInfo, + int? ticketId, }) async { const apiName = 'UpsertUserAllApi'; final now = DateTime.now().millisecondsSinceEpoch ~/ 1000; @@ -601,7 +675,7 @@ class TitleApiService { 'isFreePlay': false, 'loginDateTime': now, 'userPlaylogList': [ - _buildPlaylogEntry(userId, loginId, now, musicData, userData), + _buildPlaylogEntry(userId, loginId, now, musicData, userData, ticketId: ticketId), ], 'upsertUserAll': { 'userData': [ @@ -722,7 +796,7 @@ class TitleApiService { 'version': user['lastRomVersion'] ?? 0, 'playDate': _formatDateTime(DateTime.now()), 'playMode': 0, - 'useTicketId': -1, + 'useTicketId': ticketId ?? -1, 'playCredit': 1, 'playTrack': 1, 'clientId': _config.clientId, @@ -764,10 +838,11 @@ class TitleApiService { }; final json = await _callApi(apiName, packet, userId); - final errorId = json['errorId'] as int? ?? -1; - if (errorId != 0) { - throw TitleApiException('UpsertUserAllApi errorId=$errorId'); + final returnCode = json['returnCode'] as int? ?? -1; + if (returnCode != 1) { + throw TitleApiException('UpsertUserAllApi returnCode=$returnCode'); } + return json; } List> _buildMissionDataList(