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 {
applicationId = "com.example.rapollo"
minSdk = 23
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode
versionName = flutter.versionName

View File

@@ -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 设置';

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,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
Widget build(BuildContext context) {
final theme = Theme.of(context);
@@ -97,11 +115,21 @@ class _HomePageState extends State<HomePage> {
appBar: AppBar(
title: Text(_tabTitles[_currentTab]),
actions: [
IconButton(
icon: const Icon(Icons.logout),
tooltip: AppStrings.logoutTooltip,
onPressed: () => Navigator.of(context).pop(),
),
if (_loggingOut)
const Padding(
padding: EdgeInsets.only(right: 12),
child: SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
),
)
else
IconButton(
icon: const Icon(Icons.logout),
tooltip: AppStrings.logoutTooltip,
onPressed: _performLogout,
),
],
),
body: IndexedStack(

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,108 @@ 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 {
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
// 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
// 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);
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 +286,73 @@ 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);
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);
}
}
}
@@ -266,7 +403,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 +415,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 +495,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 +657,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 +726,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 +739,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 +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;
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;

View File

@@ -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,
});
}
@@ -88,10 +90,16 @@ class TitleApiService {
}
Map<String, dynamic> _processResponseBody(Uint8List bodyBytes) {
final decrypted = _aesDecrypt(bodyBytes);
final decompressed = _decompress(decrypted);
final jsonStr = utf8.decode(decompressed);
return jsonDecode(jsonStr) as Map<String, dynamic>;
try {
final decrypted = _aesDecrypt(bodyBytes);
final decompressed = _decompress(decrypted);
final jsonStr = utf8.decode(decompressed);
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) {
@@ -109,6 +117,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,16 +152,27 @@ 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}');
}
if (response.bodyBytes.isEmpty) {
throw TitleApiException('$apiName returned empty body');
}
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 +418,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 +431,7 @@ class TitleApiService {
token: newToken,
loginId: loginId,
lastLoginDate: lastLoginDate,
loginDateTime: now,
);
}
@@ -411,13 +450,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<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 Map<String, dynamic> musicData,
required Map<String, dynamic> userData,
int? ticketId,
}) async {
const apiName = 'UploadUserPlaylogListApi';
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
@@ -435,14 +517,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 +533,9 @@ class TitleApiService {
int loginId,
int timestamp,
Map<String, dynamic> musicData,
Map<String, dynamic> userData,
) {
Map<String, dynamic> userData, {
int? ticketId,
}) {
final user = userData['userData'] as Map<String, dynamic>? ?? {};
final charaSlot = (user['charaSlot'] as List<dynamic>?)
?.map((e) => (e as num).toInt())
@@ -574,12 +657,13 @@ class TitleApiService {
// ---- UpsertUserAll ----
Future<void> upsertUserAll({
Future<Map<String, dynamic>> upsertUserAll({
required int userId,
required int loginId,
required String loginDate,
required Map<String, dynamic> musicData,
required List<Map<String, dynamic>> generalUserInfo,
int? ticketId,
}) async {
const apiName = 'UpsertUserAllApi';
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
@@ -601,7 +685,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 +806,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 +848,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<Map<String, dynamic>> _buildMissionDataList(