youhua
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../core/network/dio_client.dart';
|
||||
import '../core/storage/local_storage.dart';
|
||||
@@ -15,7 +14,7 @@ class AuthProvider extends ChangeNotifier {
|
||||
String? _token;
|
||||
|
||||
AuthProvider(this._userService) {
|
||||
_checkAuth();
|
||||
_initAuth();
|
||||
}
|
||||
|
||||
// Getters
|
||||
@@ -24,100 +23,81 @@ class AuthProvider extends ChangeNotifier {
|
||||
bool get isLoading => _isLoading;
|
||||
String? get token => _token;
|
||||
|
||||
/// 检查登录状态
|
||||
Future<void> _checkAuth() async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
/// 初始化认证状态
|
||||
Future<void> _initAuth() async {
|
||||
_token = LocalStorage.getToken();
|
||||
_isLoggedIn = _token != null && _token!.isNotEmpty;
|
||||
_isLoggedIn = _token?.isNotEmpty == true;
|
||||
|
||||
if (_isLoggedIn) {
|
||||
final userJson = LocalStorage.getUserInfo();
|
||||
if (userJson != null) {
|
||||
_user = User.fromJson(userJson);
|
||||
}
|
||||
_user = _loadUserFromStorage();
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
User? _loadUserFromStorage() {
|
||||
final userJson = LocalStorage.getUserInfo();
|
||||
return userJson != null ? User.fromJson(userJson) : null;
|
||||
}
|
||||
|
||||
/// 登录
|
||||
Future<ApiResponse<User>> login(String username, String password) async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final response = await _userService.login(username, password);
|
||||
|
||||
if (response.success && response.data != null) {
|
||||
_token = response.data!['token'] as String?;
|
||||
final userJson = response.data!['user'] as Map<String, dynamic>? ??
|
||||
response.data!['userInfo'] as Map<String, dynamic>?;
|
||||
|
||||
if (_token != null) {
|
||||
await LocalStorage.saveToken(_token!);
|
||||
}
|
||||
if (userJson != null) {
|
||||
await LocalStorage.saveUserInfo(userJson);
|
||||
_user = User.fromJson(userJson);
|
||||
}
|
||||
|
||||
_isLoggedIn = true;
|
||||
notifyListeners();
|
||||
return ApiResponse.success(_user!, response.message);
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return ApiResponse.fail(response.message ?? '登录失败');
|
||||
} catch (e) {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return ApiResponse.fail('登录失败: $e');
|
||||
}
|
||||
Future<ApiResponse<User>> login(String username, String password) {
|
||||
return _authenticate(() => _userService.login(username, password));
|
||||
}
|
||||
|
||||
/// 注册
|
||||
Future<ApiResponse<User>> register(String username, String password) async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
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 {
|
||||
final response = await _userService.register(username, password);
|
||||
final response = await action();
|
||||
|
||||
if (response.success && response.data != null) {
|
||||
_token = response.data!['token'] as String?;
|
||||
final userJson = response.data!['userInfo'] as Map<String, dynamic>?;
|
||||
|
||||
if (_token != null) {
|
||||
await LocalStorage.saveToken(_token!);
|
||||
}
|
||||
if (userJson != null) {
|
||||
await LocalStorage.saveUserInfo(userJson);
|
||||
_user = User.fromJson(userJson);
|
||||
}
|
||||
|
||||
_isLoggedIn = true;
|
||||
notifyListeners();
|
||||
return ApiResponse.success(_user!, response.message);
|
||||
if (!response.success || response.data == null) {
|
||||
return ApiResponse.fail(response.message ?? '操作失败');
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return ApiResponse.fail(response.message ?? '注册失败');
|
||||
return _handleAuthSuccess(response.data!, response.message);
|
||||
} catch (e) {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return ApiResponse.fail('注册失败: $e');
|
||||
return ApiResponse.fail('操作失败: $e');
|
||||
} finally {
|
||||
_setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// 处理认证成功
|
||||
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!);
|
||||
}
|
||||
|
||||
if (userJson != null) {
|
||||
LocalStorage.saveUserInfo(userJson);
|
||||
_user = User.fromJson(userJson);
|
||||
}
|
||||
|
||||
_isLoggedIn = true;
|
||||
|
||||
return _user != null
|
||||
? ApiResponse.success(_user!, message)
|
||||
: ApiResponse.fail('用户信息获取失败');
|
||||
}
|
||||
|
||||
/// 退出登录
|
||||
Future<void> logout() async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
_setLoading(true);
|
||||
|
||||
try {
|
||||
await _userService.logout();
|
||||
@@ -125,12 +105,15 @@ class AuthProvider extends ChangeNotifier {
|
||||
// 忽略退出登录的接口错误
|
||||
}
|
||||
|
||||
await LocalStorage.clearUserData();
|
||||
_clearAuthState();
|
||||
_setLoading(false);
|
||||
}
|
||||
|
||||
void _clearAuthState() {
|
||||
LocalStorage.clearUserData();
|
||||
_user = null;
|
||||
_token = null;
|
||||
_isLoggedIn = false;
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 刷新用户信息
|
||||
@@ -148,4 +131,9 @@ class AuthProvider extends ChangeNotifier {
|
||||
// 忽略错误
|
||||
}
|
||||
}
|
||||
|
||||
void _setLoading(bool value) {
|
||||
_isLoading = value;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user