Files
monisuo/flutter_monisuo/lib/providers/auth_provider.dart

158 lines
3.8 KiB
Dart
Raw Normal View History

2026-03-30 00:30:42 +08:00
import 'dart:typed_data';
import 'package:flutter/material.dart';
2026-03-23 00:43:19 +08:00
import '../core/network/api_response.dart';
import '../core/network/dio_client.dart';
import '../core/storage/local_storage.dart';
import '../data/models/user.dart';
import '../data/services/user_service.dart';
/// 认证状态管理
class AuthProvider extends ChangeNotifier {
final UserService _userService;
User? _user;
bool _isLoggedIn = false;
bool _isLoading = false;
String? _token;
AuthProvider(this._userService) {
2026-03-23 00:08:19 +08:00
_initAuth();
}
// Getters
User? get user => _user;
bool get isLoggedIn => _isLoggedIn;
bool get isLoading => _isLoading;
String? get token => _token;
2026-03-23 00:08:19 +08:00
/// 初始化认证状态
Future<void> _initAuth() async {
_token = LocalStorage.getToken();
2026-03-23 00:08:19 +08:00
_isLoggedIn = _token?.isNotEmpty == true;
if (_isLoggedIn) {
2026-03-23 00:08:19 +08:00
_user = _loadUserFromStorage();
}
notifyListeners();
}
2026-03-23 00:08:19 +08:00
User? _loadUserFromStorage() {
final userJson = LocalStorage.getUserInfo();
return userJson != null ? User.fromJson(userJson) : null;
}
/// 登录
2026-03-23 00:08:19 +08:00
Future<ApiResponse<User>> login(String username, String password) {
return _authenticate(() => _userService.login(username, password));
}
/// 注册
Future<ApiResponse<User>> register(String username, String password) {
return _authenticate(() => _userService.register(username, password));
}
/// 统一认证处理
Future<ApiResponse<User>> _authenticate(
Future<ApiResponse<Map<String, dynamic>>> Function() action,
) async {
_setLoading(true);
try {
2026-03-23 00:08:19 +08:00
final response = await action();
2026-03-23 00:08:19 +08:00
if (!response.success || response.data == null) {
return ApiResponse.fail(response.message ?? '操作失败');
}
2026-03-23 00:08:19 +08:00
return _handleAuthSuccess(response.data!, response.message);
} catch (e) {
2026-03-23 00:08:19 +08:00
return ApiResponse.fail('操作失败: $e');
} finally {
_setLoading(false);
}
}
2026-03-23 00:08:19 +08:00
/// 处理认证成功
ApiResponse<User> _handleAuthSuccess(
Map<String, dynamic> data,
String? message,
) {
_token = data['token'] as String?;
final userJson = data['user'] as Map<String, dynamic>? ??
data['userInfo'] as Map<String, dynamic>?;
if (_token != null) {
LocalStorage.saveToken(_token!);
}
2026-03-23 00:08:19 +08:00
if (userJson != null) {
LocalStorage.saveUserInfo(userJson);
_user = User.fromJson(userJson);
}
2026-03-23 00:08:19 +08:00
_isLoggedIn = true;
2026-03-26 01:35:51 +08:00
notifyListeners(); // 通知 UI 更新,触发页面跳转
2026-03-23 00:08:19 +08:00
return _user != null
? ApiResponse.success(_user!, message)
: ApiResponse.fail('用户信息获取失败');
}
/// 退出登录
Future<void> logout() async {
2026-03-23 00:08:19 +08:00
_setLoading(true);
try {
await _userService.logout();
} catch (_) {
// 忽略退出登录的接口错误
}
2026-03-23 00:08:19 +08:00
_clearAuthState();
_setLoading(false);
}
void _clearAuthState() {
LocalStorage.clearUserData();
_user = null;
_token = null;
_isLoggedIn = false;
}
/// 刷新用户信息
Future<void> refreshUserInfo() async {
if (!_isLoggedIn) return;
try {
final response = await _userService.getUserInfo();
if (response.success && response.data != null) {
_user = response.data;
await LocalStorage.saveUserInfo(_user!.toJson());
notifyListeners();
}
} catch (_) {
// 忽略错误
}
}
2026-03-23 00:08:19 +08:00
2026-03-30 00:30:42 +08:00
/// 提交KYC实名认证真实图片上传
Future<ApiResponse<void>> submitKyc(
Uint8List frontBytes, Uint8List backBytes) async {
try {
final response =
await _userService.uploadKyc(frontBytes, backBytes);
if (response.success) {
await refreshUserInfo();
}
return response;
} catch (e) {
return ApiResponse.fail('KYC提交失败: $e');
}
}
2026-03-23 00:08:19 +08:00
void _setLoading(bool value) {
_isLoading = value;
notifyListeners();
}
}