97 lines
2.9 KiB
Dart
97 lines
2.9 KiB
Dart
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 {
|
|
AppDependencies._({
|
|
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();
|
|
final api = ApiClient(() => session.token);
|
|
return AppDependencies._(
|
|
session: session,
|
|
repository: ServiceRepository(api, GeolocatorLocationService()),
|
|
drafts: EncryptedDraftStore(),
|
|
);
|
|
}
|
|
}
|
|
|
|
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';
|
|
final SecureSessionStore _store;
|
|
|
|
String _token = '';
|
|
String _identity = '';
|
|
String _roleCode = '';
|
|
String _deviceIdentity = '';
|
|
|
|
String get token => _token;
|
|
String get identity => _identity;
|
|
String get roleCode => _roleCode;
|
|
String get deviceIdentity => _deviceIdentity;
|
|
bool get isAuthenticated => _token.isNotEmpty;
|
|
|
|
Future<void> restore() async {
|
|
_token = await _store.read(_tokenKey) ?? '';
|
|
_identity = await _store.read(_identityKey) ?? '';
|
|
_roleCode = await _store.read(_roleKey) ?? '';
|
|
_deviceIdentity = await _store.read(_deviceKey) ?? '';
|
|
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},
|
|
),
|
|
);
|
|
_token = details['access_token'] as String? ?? '';
|
|
_identity = details['identity'] as String? ?? '';
|
|
_roleCode = details['role_code'] as String? ?? '';
|
|
if (_token.isEmpty || _identity.isEmpty || _roleCode.isEmpty) {
|
|
throw const ApiException(500, '工作人员登录上下文缺失');
|
|
}
|
|
await _store.write(_tokenKey, _token);
|
|
await _store.write(_identityKey, _identity);
|
|
await _store.write(_roleKey, _roleCode);
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> logout() async {
|
|
_token = '';
|
|
_identity = '';
|
|
_roleCode = '';
|
|
await _store.delete(_tokenKey);
|
|
await _store.delete(_identityKey);
|
|
await _store.delete(_roleKey);
|
|
notifyListeners();
|
|
}
|
|
}
|