2026-09-05 19:51:36 +08:00
|
|
|
|
// 功能描述:装配工作人员端依赖,并统一管理登录、退出和失效会话。
|
|
|
|
|
|
// 版本:1.1.0
|
2026-07-30 21:47:41 +08:00
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
|
|
|
|
|
|
|
import '../data/offline/encrypted_draft_store.dart';
|
|
|
|
|
|
import '../data/repositories/service_repository.dart';
|
|
|
|
|
|
import '../data/services/api_client.dart';
|
|
|
|
|
|
import '../data/services/location_service.dart';
|
|
|
|
|
|
import '../data/services/secure_session_store.dart';
|
|
|
|
|
|
|
|
|
|
|
|
class AppDependencies {
|
2026-09-05 19:51:36 +08:00
|
|
|
|
AppDependencies({
|
2026-07-30 21:47:41 +08:00
|
|
|
|
required this.session,
|
|
|
|
|
|
required this.repository,
|
|
|
|
|
|
required this.drafts,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
final StaffSession session;
|
|
|
|
|
|
final ServiceRepository repository;
|
|
|
|
|
|
final EncryptedDraftStore drafts;
|
|
|
|
|
|
|
|
|
|
|
|
static Future<AppDependencies> create() async {
|
|
|
|
|
|
final store = SecureSessionStore();
|
|
|
|
|
|
final session = StaffSession(store);
|
|
|
|
|
|
await session.restore();
|
2026-09-05 19:51:36 +08:00
|
|
|
|
final authenticatedApi = ApiClient(
|
|
|
|
|
|
() => session.token,
|
|
|
|
|
|
onUnauthorized: session.invalidate,
|
|
|
|
|
|
);
|
|
|
|
|
|
return AppDependencies(
|
2026-07-30 21:47:41 +08:00
|
|
|
|
session: session,
|
2026-09-05 19:51:36 +08:00
|
|
|
|
repository: ServiceRepository(authenticatedApi, GeolocatorLocationService()),
|
2026-07-30 21:47:41 +08:00
|
|
|
|
drafts: EncryptedDraftStore(),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-05 19:51:36 +08:00
|
|
|
|
/// 管理工作人员令牌、身份、角色、设备标识及会话失效通知。
|
2026-07-30 21:47:41 +08:00
|
|
|
|
class StaffSession extends ChangeNotifier {
|
|
|
|
|
|
StaffSession(this._store);
|
|
|
|
|
|
|
|
|
|
|
|
static const _root = '/heqi/client/v1/staff';
|
|
|
|
|
|
static const _tokenKey = 'service_app_access_token';
|
|
|
|
|
|
static const _identityKey = 'service_app_identity';
|
|
|
|
|
|
static const _roleKey = 'service_app_role';
|
|
|
|
|
|
static const _deviceKey = 'service_app_device';
|
2026-09-05 19:51:36 +08:00
|
|
|
|
final SessionStore _store;
|
2026-07-30 21:47:41 +08:00
|
|
|
|
|
|
|
|
|
|
String _token = '';
|
|
|
|
|
|
String _identity = '';
|
|
|
|
|
|
String _roleCode = '';
|
|
|
|
|
|
String _deviceIdentity = '';
|
2026-09-05 19:51:36 +08:00
|
|
|
|
bool _expired = false;
|
|
|
|
|
|
Future<void> _pendingClear = Future<void>.value();
|
2026-07-30 21:47:41 +08:00
|
|
|
|
|
|
|
|
|
|
String get token => _token;
|
|
|
|
|
|
String get identity => _identity;
|
|
|
|
|
|
String get roleCode => _roleCode;
|
|
|
|
|
|
String get deviceIdentity => _deviceIdentity;
|
|
|
|
|
|
bool get isAuthenticated => _token.isNotEmpty;
|
2026-09-05 19:51:36 +08:00
|
|
|
|
bool get hasExpired => _expired;
|
2026-07-30 21:47:41 +08:00
|
|
|
|
|
2026-09-05 19:51:36 +08:00
|
|
|
|
/// 从安全存储恢复会话上下文;是否有效由后续受保护接口响应确认。
|
2026-07-30 21:47:41 +08:00
|
|
|
|
Future<void> restore() async {
|
|
|
|
|
|
_token = await _store.read(_tokenKey) ?? '';
|
|
|
|
|
|
_identity = await _store.read(_identityKey) ?? '';
|
|
|
|
|
|
_roleCode = await _store.read(_roleKey) ?? '';
|
|
|
|
|
|
_deviceIdentity = await _store.read(_deviceKey) ?? '';
|
2026-09-05 19:51:36 +08:00
|
|
|
|
_expired = false;
|
2026-07-30 21:47:41 +08:00
|
|
|
|
if (_deviceIdentity.isEmpty) {
|
|
|
|
|
|
_deviceIdentity = const Uuid().v7();
|
|
|
|
|
|
await _store.write(_deviceKey, _deviceIdentity);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> login(String phone, String password) async {
|
|
|
|
|
|
final api = ApiClient(() => '');
|
|
|
|
|
|
final details = jsonMap(
|
|
|
|
|
|
await api.post(
|
|
|
|
|
|
'$_root/auth/login',
|
|
|
|
|
|
authenticated: false,
|
|
|
|
|
|
body: {'phone': phone, 'mode': 'password', 'password': password},
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
2026-09-05 19:51:36 +08:00
|
|
|
|
final token = details['access_token'] as String? ?? '';
|
|
|
|
|
|
final identity = details['identity'] as String? ?? '';
|
|
|
|
|
|
final roleCode = details['role_code'] as String? ?? '';
|
|
|
|
|
|
if (token.isEmpty || identity.isEmpty || roleCode.isEmpty) {
|
2026-07-30 21:47:41 +08:00
|
|
|
|
throw const ApiException(500, '工作人员登录上下文缺失');
|
|
|
|
|
|
}
|
2026-09-05 19:51:36 +08:00
|
|
|
|
|
|
|
|
|
|
// 等待旧会话清理结束,避免迟到的删除任务误删刚写入的新会话。
|
|
|
|
|
|
await _pendingClear;
|
|
|
|
|
|
await _store.write(_tokenKey, token);
|
|
|
|
|
|
await _store.write(_identityKey, identity);
|
|
|
|
|
|
await _store.write(_roleKey, roleCode);
|
|
|
|
|
|
_token = token;
|
|
|
|
|
|
_identity = identity;
|
|
|
|
|
|
_roleCode = roleCode;
|
|
|
|
|
|
_expired = false;
|
2026-07-30 21:47:41 +08:00
|
|
|
|
notifyListeners();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> logout() async {
|
|
|
|
|
|
_token = '';
|
|
|
|
|
|
_identity = '';
|
|
|
|
|
|
_roleCode = '';
|
2026-09-05 19:51:36 +08:00
|
|
|
|
_expired = false;
|
|
|
|
|
|
notifyListeners();
|
|
|
|
|
|
_pendingClear = _clearStoredSession();
|
|
|
|
|
|
await _pendingClear;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 仅当服务端拒绝的仍是当前令牌时,使会话失效并通知路由。
|
|
|
|
|
|
void invalidate(String rejectedToken) {
|
|
|
|
|
|
if (rejectedToken.isEmpty || rejectedToken != _token || _expired) return;
|
|
|
|
|
|
|
|
|
|
|
|
_token = '';
|
|
|
|
|
|
_identity = '';
|
|
|
|
|
|
_roleCode = '';
|
|
|
|
|
|
_expired = true;
|
2026-07-30 21:47:41 +08:00
|
|
|
|
notifyListeners();
|
2026-09-05 19:51:36 +08:00
|
|
|
|
_pendingClear = _clearStoredSession();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 尽力删除会话字段并保留设备标识;单项失败不得阻塞返回登录页。
|
|
|
|
|
|
Future<void> _clearStoredSession() async {
|
|
|
|
|
|
for (final key in const [_tokenKey, _identityKey, _roleKey]) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await _store.delete(key);
|
|
|
|
|
|
} catch (error, stackTrace) {
|
|
|
|
|
|
debugPrint('清理工作人员失效会话失败($key):$error');
|
|
|
|
|
|
debugPrintStack(stackTrace: stackTrace);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-07-30 21:47:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|