这是CaiYouHui前端,一个关于flutter的安卓app,前端使用flutter实现

auth_provider.dart 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import 'package:flutter/material.dart';
  2. import '../../data/models/user.dart';
  3. import '../../data/models/auth/login_request.dart';
  4. import '../../data/models/auth/register_request.dart';
  5. import '../../data/repositories/auth_repository.dart';
  6. class AuthProvider with ChangeNotifier {
  7. final AuthRepository authRepository;
  8. User? _user;
  9. bool _isLoading = false;
  10. String? _error;
  11. AuthProvider({required this.authRepository}) {
  12. _loadCurrentUser();
  13. }
  14. User? get user => _user;
  15. bool get isAuthenticated => _user != null;
  16. bool get isLoading => _isLoading;
  17. String? get error => _error;
  18. Future<void> _loadCurrentUser() async {
  19. final response = await authRepository.getCurrentUser();
  20. if (response.success && response.data != null) {
  21. _user = response.data;
  22. notifyListeners();
  23. }
  24. }
  25. Future<void> login(String username, String password) async {
  26. _isLoading = true;
  27. _error = null;
  28. notifyListeners();
  29. try {
  30. final response = await authRepository.login(
  31. LoginRequest(username: username, password: password),
  32. );
  33. if (response.success && response.data != null) {
  34. _user = response.data;
  35. _error = null;
  36. } else {
  37. _error = response.message;
  38. }
  39. } catch (e) {
  40. _error = '登录失败: $e';
  41. } finally {
  42. _isLoading = false;
  43. notifyListeners();
  44. }
  45. }
  46. Future<void> register(String username, String email, String password, String passwordConfirm, String? phone) async {
  47. _isLoading = true;
  48. _error = null;
  49. notifyListeners();
  50. try {
  51. final response = await authRepository.register(
  52. RegisterRequest(username: username, email: email, password: password, passwordConfirm: passwordConfirm),
  53. );
  54. if (response.success && response.data != null) {
  55. _user = response.data;
  56. _error = null;
  57. } else {
  58. _error = response.message;
  59. }
  60. } catch (e) {
  61. _error = '注册失败: $e';
  62. } finally {
  63. _isLoading = false;
  64. notifyListeners();
  65. }
  66. }
  67. Future<void> modifyPassword(String oldPassword, String newPassword) async {
  68. _isLoading = true;
  69. _error = null;
  70. notifyListeners();
  71. try {
  72. // 1. 调用API验证旧密码
  73. // final isValidOldPassword = await authRepository.validatePassword(oldPassword);
  74. // if (!isValidOldPassword) {
  75. // _error = '当前密码不正确';
  76. // return;
  77. // }
  78. // 2. 调用API更新密码
  79. // final response = await authRepository.updatePassword(
  80. // LoginRequest(username: oldPassword, password: newPassword),
  81. // );
  82. // if (response.success && response.data != null) {
  83. // // 可选:清除本地存储的token,让用户重新登录
  84. // // await _authService.logout();
  85. // _error = null;
  86. // } else {
  87. // _error = response.message;
  88. // }
  89. } catch (e) {
  90. _error = '密码修改失败: $e';
  91. } finally {
  92. _isLoading = false;
  93. notifyListeners();
  94. }
  95. }
  96. Future<void> logout() async {
  97. _isLoading = true;
  98. _error = null;
  99. notifyListeners();
  100. try {
  101. final response = await authRepository.logout();
  102. if (response.success) {
  103. _user = null;
  104. _error = null;
  105. } else {
  106. _error = response.message;
  107. }
  108. } catch (e) {
  109. _error = '登出失败: $e';
  110. } finally {
  111. _isLoading = false;
  112. notifyListeners();
  113. }
  114. }
  115. Future<void> checkAuthStatus() async {
  116. final isLoggedIn = await authRepository.isLoggedIn();
  117. if (!isLoggedIn) {
  118. _user = null;
  119. } else {
  120. await _loadCurrentUser();
  121. }
  122. notifyListeners();
  123. }
  124. void clearError() {
  125. _error = null;
  126. notifyListeners();
  127. }
  128. void updateUser(User? user) {
  129. _user = user;
  130. notifyListeners();
  131. }
  132. }