Files
Ichino/lib/models/session_share.dart
T
Cookiues 44c4a1daae feat: 连接信息恢复的会话在 isLogin 时直接开放票据与风险功能
- 恢复时除了 GetUserDataApi 再拉一次 GetUserPreviewApi:服务器说 isLogin 为 true
  就把这次会话当已登录,_gameLogin 落地,票据与风险页按「登录后 60 秒冷却」计时
  (loginDateTime 取恢复当下),不必再发一次 UserLoginApi
- GetUserPreviewApi 的响应里没有 loginId,而 UpsertUserAll 要拿它当 playlogId,
  所以导出连接信息时把本机的 loginId 一起带过去,恢复端优先用这个值,
  拉不到才退回 preview 里的字段(多半是 0)
- 只有 Cookie、preview 拉不到、或服务器说没在登录中时仍按只读处理,
  提示横幅与 SnackBar 文案区分「可写」和「只能读」两种恢复结果
- HomePage 的恢复入口收成 restored: SessionRestoreResult?,把 preview 与
  userData 一起当种子,省掉两次重复请求
2026-09-29 13:47:03 +08:00

88 lines
2.8 KiB
Dart

import 'dart:convert';
/// 实验性:一次登录的可搬运快照(用户 ID + JSESSIONID Cookie + 令牌),
/// 编码方式是 Base64(JSON),用于在别的设备上恢复会话、免重新扫码。
///
/// 没有任何加密,字符串在谁手里谁就能操作这个账号,只适合自己传递。
class SessionShare {
static const String _kind = 'ichino.session';
static const int _version = 1;
final int userId;
final String token;
/// 导出端这次登录的 `loginId`。GetUserPreviewApi 不回这个字段,而恢复端写
/// UpsertUserAll 时要拿它当 playlogId,所以只能由导出端一起带上;没有就是 0。
final int loginId;
/// 原始 Cookie 串,可能是 `JSESSIONID=...`,也可能带别的字段。
final String? cookie;
const SessionShare({
required this.userId,
required this.token,
this.loginId = 0,
this.cookie,
});
/// 只取 `JSESSIONID`:传输层后续请求就靠它维持会话
/// (见 `TitleApiService._captureCookiesFromResponse`)。
String? get jsessionid {
final raw = cookie;
if (raw == null) return null;
for (final part in raw.split(';')) {
final pair = part.trim();
final eq = pair.indexOf('=');
if (eq <= 0) continue;
if (pair.substring(0, eq).toUpperCase() != 'JSESSIONID') continue;
final value = pair.substring(eq + 1).trim();
return value.isEmpty ? null : 'JSESSIONID=$value';
}
return null;
}
bool get hasCookie => jsessionid != null;
bool get hasToken => token.isNotEmpty;
String encode() {
final map = <String, dynamic>{
'kind': _kind,
'version': _version,
'userId': userId,
'token': token,
'loginId': loginId,
'cookie': jsessionid,
};
return base64Encode(utf8.encode(jsonEncode(map)));
}
/// 不是连接信息时返回 `null`——登录页靠它区分 QR 令牌和会话快照。
static SessionShare? tryDecode(String input) {
var text = input.replaceAll(RegExp(r'\s'), '');
if (text.length < 16) return null;
final pad = text.length % 4;
if (pad != 0) text += '=' * (4 - pad);
try {
final decoded = jsonDecode(utf8.decode(base64Decode(text)));
if (decoded is! Map<String, dynamic>) return null;
if (decoded['kind'] != _kind) return null;
if ((decoded['version'] as num?)?.toInt() != _version) return null;
final share = SessionShare(
userId: (decoded['userId'] as num?)?.toInt() ?? 0,
token: decoded['token'] as String? ?? '',
loginId: (decoded['loginId'] as num?)?.toInt() ?? 0,
cookie: decoded['cookie'] as String?,
);
// 两头都空的话这串信息什么都恢复不了。
if (share.userId <= 0 || (!share.hasCookie && !share.hasToken)) {
return null;
}
return share;
} catch (_) {
return null;
}
}
}