32bd64d676
- 心跳读取 /auth/ping 回带的授权概况,直接刷新横幅/状态栏/只读门禁,省去单独轮询 /license/info - 账号被停用/删除(401 USER_DISABLED)即强制重新登录 - 令牌持久化经串行队列 + 会话代号守卫,杜绝续期写入与登出交叉把失效 token 写回 - 写操作门禁(write_guard)+ 授权文案(license_copy)按 grace/readonly/locked 分阶段降级 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
59 lines
1.9 KiB
Dart
59 lines
1.9 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../core/api/api_client.dart';
|
|
import '../core/auth/auth_state.dart';
|
|
import '../models/license.dart';
|
|
import '../repositories/license_repository.dart';
|
|
|
|
export '../models/license.dart';
|
|
|
|
final licenseRepositoryProvider = Provider<LicenseRepository>(
|
|
(ref) => LicenseRepository(ref.read(apiClientProvider)),
|
|
);
|
|
|
|
final licenseProvider =
|
|
AsyncNotifierProvider<LicenseNotifier, LicenseInfo?>(LicenseNotifier.new);
|
|
|
|
class LicenseNotifier extends AsyncNotifier<LicenseInfo?> {
|
|
@override
|
|
Future<LicenseInfo?> build() async {
|
|
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
|
return _fetch();
|
|
}
|
|
|
|
Future<LicenseInfo?> _fetch() async {
|
|
try {
|
|
return await ref.read(licenseRepositoryProvider).getInfo();
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<void> reload() async {
|
|
state = const AsyncValue.loading();
|
|
state = AsyncValue.data(await _fetch());
|
|
}
|
|
|
|
/// 后台静默刷新(供心跳调用):不闪 loading;
|
|
/// 仅在成功拿到结果时更新(含「确无授权」的 null),
|
|
/// 网络/瞬时错误则保留上次状态,避免把已有授权误刷成「未激活」。
|
|
/// 会话被撤销(401)由 ApiClient 拦截器统一处理(触发登出),与此无关。
|
|
Future<void> refresh() async {
|
|
try {
|
|
final info = await ref.read(licenseRepositoryProvider).getInfo();
|
|
state = AsyncValue.data(info);
|
|
} catch (_) {
|
|
// 保留上次状态
|
|
}
|
|
}
|
|
|
|
/// 直接采纳心跳 /auth/ping 回带的授权概况,免去单独再请求 /license/info。
|
|
/// [raw] 为后端 LicenseInfoView 的 JSON(与 /license/info 同构);
|
|
/// 为 null 表示「确无有效授权」,与 reload/refresh 拿到 null 语义一致。
|
|
void applyServerInfo(dynamic raw) {
|
|
final info = raw == null
|
|
? null
|
|
: LicenseInfo.fromJson(Map<String, dynamic>.from(raw as Map));
|
|
state = AsyncValue.data(info);
|
|
}
|
|
}
|