fix: 后端连接失败自动重连重试;签到与每日计数改按本地日历日

1. 有时无法连接后端:
- 根因:请求连接失败直接抛错,运行中从不重新发现地址(睡眠唤醒后旧连接已死、后端换端口后必现)
- _send 对 GET 连接失败自动 rediscover + 重试 1 次;POST 不自动重试,避免签到/扣积分重复提交
- 启动重试从 5 次(约 31s)放宽到 10 次、间隔封顶 8s

2. 签到读日历,不按 24 小时:
- 根因:签到用 DateTime.UtcNow.Date,东八区要早上 8 点才翻篇,凌晨可同一天签到两次
- UserService(签到/人品值/二选一跨天计数)与 UserPayload 统一改 DateTime.Today 本地日历日
- 实测:本地 11-22 07:33(UTC 仍 11-21)时 last_sign_in=11-22,重复签到被拒,免费次数按本地日重置
This commit is contained in:
远影科技 committed 2026-11-22 07:33:56 +08:00
1 parent cfd6809714
commit 40d9da1c3d
5 files changed
+49 -8

No files matched your search

+20
View File
@@ -533,6 +533,26 @@ class GcApi {
{Map<String, String>? query,
Object? body,
Duration? timeout}) async {
try {
return await _sendOnce(method, path,
query: query, body: body, timeout: timeout);
} on ApiException catch (e) {
// HTTP 层错误(后端明确回了 4xx/5xx)不是连接问题,直接抛。
if (e.statusCode != null) rethrow;
// 连接层失败:可能是电脑睡眠唤醒后旧连接已死,或后端换了端口。
// 重新解析地址后重试一次。只对 GET 自动重试——POST(签到/扣积分)
// 在连接不确定的情况下重发可能重复计费,宁可让用户手动再点。
if (method != 'GET') rethrow;
if (!rediscover()) rethrow;
return await _sendOnce(method, path,
query: query, body: body, timeout: timeout);
}
}
Future<dynamic> _sendOnce(String method, String path,
{Map<String, String>? query,
Object? body,
Duration? timeout}) async {
final uri = _uri(path, query);
final auth = _authHeaders;
late http.Response response;
+4 -3
View File
@@ -82,10 +82,11 @@ class ShellController extends ChangeNotifier {
notifyListeners();
}
/// 后端没起来时自动重试(2s、4s、8s…最多 5 次),不必让用户手动点。
/// 后端没起来时自动重试(1s、2s、4s、之后每次 8s,共 10 次 ≈ 70 秒),
/// 不必让用户手动点。重试前 rediscover() 重新解析地址与令牌。
void _scheduleRetry() {
if (_retryAttempt >= 5) return;
final seconds = 1 << _retryAttempt; // 1,2,4,8,16
if (_retryAttempt >= 10) return;
final seconds = 1 << _retryAttempt.clamp(0, 3); // 1,2,4,8,8,8…(封顶 8s)
_retryAttempt++;
_retryTimer?.cancel();
_retryTimer = Timer(Duration(seconds: seconds), () {