| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- import 'package:flutter/material.dart';
- import '../../data/models/user.dart';
- import '../../data/models/auth/login_request.dart';
- import '../../data/models/auth/register_request.dart';
- import '../../data/repositories/auth_repository.dart';
-
- class AuthProvider with ChangeNotifier {
- final AuthRepository authRepository;
-
- User? _user;
- bool _isLoading = false;
- String? _error;
-
- AuthProvider({required this.authRepository}) {
- _loadCurrentUser();
- }
-
- User? get user => _user;
- bool get isAuthenticated => _user != null;
- bool get isLoading => _isLoading;
- String? get error => _error;
-
- Future<void> _loadCurrentUser() async {
- final response = await authRepository.getCurrentUser();
- if (response.success && response.data != null) {
- _user = response.data;
- notifyListeners();
- }
- }
-
- Future<void> login(String username, String password) async {
- _isLoading = true;
- _error = null;
- notifyListeners();
-
- try {
- final response = await authRepository.login(
- LoginRequest(username: username, password: password),
- );
-
- if (response.success && response.data != null) {
- _user = response.data;
- _error = null;
- } else {
- _error = response.message;
- }
- } catch (e) {
- _error = '登录失败: $e';
- } finally {
- _isLoading = false;
- notifyListeners();
- }
- }
-
- Future<void> register(String username, String email, String password, String passwordConfirm, String? phone) async {
- _isLoading = true;
- _error = null;
- notifyListeners();
-
- try {
- final response = await authRepository.register(
- RegisterRequest(username: username, email: email, password: password, passwordConfirm: passwordConfirm),
- );
-
- if (response.success && response.data != null) {
- _user = response.data;
- _error = null;
- } else {
- _error = response.message;
- }
- } catch (e) {
- _error = '注册失败: $e';
- } finally {
- _isLoading = false;
- notifyListeners();
- }
- }
-
- Future<void> modifyPassword(String oldPassword, String newPassword) async {
- _isLoading = true;
- _error = null;
- notifyListeners();
-
- try {
- // 1. 调用API验证旧密码
- // final isValidOldPassword = await authRepository.validatePassword(oldPassword);
-
- // if (!isValidOldPassword) {
- // _error = '当前密码不正确';
- // return;
- // }
-
- // 2. 调用API更新密码
- // final response = await authRepository.updatePassword(
- // LoginRequest(username: oldPassword, password: newPassword),
- // );
-
- // if (response.success && response.data != null) {
- // // 可选:清除本地存储的token,让用户重新登录
- // // await _authService.logout();
- // _error = null;
- // } else {
- // _error = response.message;
- // }
- } catch (e) {
- _error = '密码修改失败: $e';
- } finally {
- _isLoading = false;
- notifyListeners();
- }
- }
-
- Future<void> logout() async {
- _isLoading = true;
- _error = null;
- notifyListeners();
-
- try {
- final response = await authRepository.logout();
-
- if (response.success) {
- _user = null;
- _error = null;
- } else {
- _error = response.message;
- }
- } catch (e) {
- _error = '登出失败: $e';
- } finally {
- _isLoading = false;
- notifyListeners();
- }
- }
-
- Future<void> checkAuthStatus() async {
- final isLoggedIn = await authRepository.isLoggedIn();
- if (!isLoggedIn) {
- _user = null;
- } else {
- await _loadCurrentUser();
- }
- notifyListeners();
- }
-
- void clearError() {
- _error = null;
- notifyListeners();
- }
-
- void updateUser(User? user) {
- _user = user;
- notifyListeners();
- }
- }
|