auth_store.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import 'package:chicken_farm/apis/index.dart';
  2. import 'package:chicken_farm/core/config/app_config.dart';
  3. import 'package:chicken_farm/core/services/navigation_service.dart';
  4. import 'package:chicken_farm/core/utils/jwt_token.dart';
  5. import 'package:chicken_farm/core/utils/logger.dart';
  6. import 'package:chicken_farm/core/utils/service_checker.dart';
  7. import 'package:chicken_farm/modes/auth/login_model.dart';
  8. import 'package:chicken_farm/modes/user/user_model.dart';
  9. import 'package:chicken_farm/routes/app_routes.dart';
  10. import 'package:flutter_riverpod/flutter_riverpod.dart';
  11. import 'package:go_router/go_router.dart';
  12. /// 认证状态枚举
  13. enum AuthState { authenticated, unauthenticated, loading }
  14. /// 用户认证信息状态
  15. class AuthInfo {
  16. final AuthState state;
  17. final String? token;
  18. final UserModel? user;
  19. final List<String>? permissions;
  20. final List<String>? roles;
  21. AuthInfo({
  22. required this.state,
  23. this.token,
  24. this.user,
  25. this.permissions,
  26. this.roles,
  27. });
  28. /// 创建已认证状态
  29. AuthInfo.authenticated({
  30. required String token,
  31. required UserModel user,
  32. List<String>? permissions,
  33. List<String>? roles,
  34. }) : this(
  35. state: AuthState.authenticated,
  36. token: token,
  37. user: user,
  38. permissions: permissions,
  39. roles: roles,
  40. );
  41. /// 创建未认证状态
  42. AuthInfo.unauthenticated()
  43. : this(
  44. state: AuthState.unauthenticated,
  45. token: null,
  46. user: null,
  47. permissions: const [],
  48. roles: const [],
  49. );
  50. /// 创建加载状态
  51. AuthInfo.loading()
  52. : this(
  53. state: AuthState.loading,
  54. token: null,
  55. user: null,
  56. permissions: const [],
  57. roles: const [],
  58. );
  59. /// 复制对象并更新部分字段
  60. AuthInfo copyWith({
  61. AuthState? state,
  62. String? token,
  63. UserModel? user,
  64. List<String>? permissions,
  65. List<String>? roles,
  66. }) {
  67. return AuthInfo(
  68. state: state ?? this.state,
  69. token: token ?? this.token,
  70. user: user ?? this.user,
  71. permissions: permissions ?? this.permissions,
  72. roles: roles ?? this.roles,
  73. );
  74. }
  75. }
  76. class AuthStore extends StateNotifier<AuthInfo> {
  77. AuthStore() : super(AuthInfo.unauthenticated()) {
  78. _init();
  79. }
  80. /// 初始化认证状态
  81. Future<void> _init() async {
  82. state = AuthInfo.loading();
  83. try {
  84. final token = await JwtToken.getToken();
  85. if (token != null) {
  86. // 检查网络连接状态
  87. final isConnected = await ServiceChecker().forceCheckService();
  88. if (isConnected) {
  89. // 有网络连接,强制获取最新用户信息
  90. try {
  91. final userInfo = await apis.loginApi.getInfo();
  92. if (userInfo == null) {
  93. throw Exception('用户信息获取失败');
  94. }
  95. // 保存用户信息到本地存储,供离线时使用
  96. await JwtToken.saveUserInfo(userInfo);
  97. state = AuthInfo.authenticated(
  98. token: token,
  99. user: userInfo.user!,
  100. permissions: userInfo.permissions,
  101. roles: userInfo.roles,
  102. );
  103. logger.i('在线模式下已登录 state: $state');
  104. } catch (e) {
  105. // 网络连接但是获取用户信息失败,可能是token过期
  106. logger.e('在线模式下获取用户信息失败: $e');
  107. await JwtToken.clear();
  108. state = AuthInfo.unauthenticated();
  109. }
  110. } else {
  111. // 无网络连接,使用离线模式
  112. try {
  113. // 尝试从本地存储获取上次登录的用户信息
  114. final cachedUserInfo = await JwtToken.getUserInfo();
  115. if (cachedUserInfo != null && cachedUserInfo.user != null) {
  116. state = AuthInfo.authenticated(
  117. token: token,
  118. user: cachedUserInfo.user!,
  119. permissions: cachedUserInfo.permissions ?? [],
  120. roles: cachedUserInfo.roles ?? [],
  121. );
  122. logger.i('离线模式下使用缓存用户信息: $state');
  123. } else {
  124. // 没有缓存的用户信息,不能进行离线认证
  125. logger.w('离线模式下无缓存用户信息,无法进行离线认证');
  126. state = AuthInfo.unauthenticated();
  127. }
  128. } catch (e) {
  129. logger.e('离线模式初始化失败: $e');
  130. state = AuthInfo.unauthenticated();
  131. }
  132. }
  133. } else {
  134. state = AuthInfo.unauthenticated();
  135. }
  136. } catch (e) {
  137. logger.e('认证初始化失败: $e');
  138. state = AuthInfo.unauthenticated();
  139. }
  140. }
  141. /// 登录操作
  142. Future<void> login(LoginModel loginModel) async {
  143. state = AuthInfo.loading();
  144. try {
  145. loginModel.clientId = AppConfig.clientId;
  146. final authResult = await apis.loginApi.login(loginModel);
  147. final token = authResult.accessToken;
  148. await JwtToken.setToken(token, authResult.refreshToken);
  149. // 获取用户信息
  150. final userInfo = await apis.loginApi.getInfo();
  151. if (userInfo == null) {
  152. throw Exception('用户信息获取失败');
  153. }
  154. // 保存用户信息到本地存储,供离线时使用
  155. await JwtToken.saveUserInfo(userInfo);
  156. state = AuthInfo.authenticated(
  157. token: token,
  158. user: userInfo.user!,
  159. permissions: userInfo.permissions,
  160. roles: userInfo.roles,
  161. );
  162. logger.i('登录成功 state: $state');
  163. } catch (e) {
  164. await JwtToken.clear();
  165. state = AuthInfo.unauthenticated();
  166. }
  167. }
  168. /// 登出操作
  169. Future<void> logout() async {
  170. try {
  171. await apis.loginApi.logout();
  172. } catch (e) {
  173. // 即使API调用失败也要清除本地状态
  174. logger.e('Logout API call failed: $e');
  175. } finally {
  176. await JwtToken.clear();
  177. state = AuthInfo.unauthenticated();
  178. if (NavigationService.navigatorKey.currentState != null &&
  179. NavigationService.navigatorKey.currentContext != null) {
  180. NavigationService.navigatorKey.currentContext!.goNamed(
  181. AppRouteNames.login,
  182. );
  183. }
  184. }
  185. }
  186. /// 刷新token
  187. Future<void> refreshToken() async {
  188. try {
  189. final refreshToken = await JwtToken.getRefreshToken();
  190. if (refreshToken != null) {
  191. final authResult = await apis.loginApi.refreshToken(refreshToken);
  192. final newToken = authResult.accessToken;
  193. await JwtToken.setToken(newToken, authResult.refreshToken);
  194. state = state.copyWith(token: newToken);
  195. }
  196. } catch (e) {
  197. // 刷新失败则登出
  198. await logout();
  199. rethrow;
  200. }
  201. }
  202. /// 重新获取用户信息
  203. Future<void> getUserInfo() async {
  204. try {
  205. final userInfo = await apis.loginApi.getInfo();
  206. if (userInfo == null) {
  207. throw Exception('用户信息获取失败');
  208. }
  209. // 更新本地存储的用户信息
  210. await JwtToken.saveUserInfo(userInfo);
  211. state = state.copyWith(
  212. user: userInfo.user,
  213. permissions: userInfo.permissions,
  214. roles: userInfo.roles,
  215. );
  216. } catch (e) {
  217. rethrow;
  218. }
  219. }
  220. /// 检查是否有特定权限
  221. bool hasPermission(String permission) {
  222. return state.permissions?.contains(permission) ?? false;
  223. }
  224. /// 检查是否有特定角色
  225. bool hasRole(String role) {
  226. return state.roles?.contains(role) ?? false;
  227. }
  228. /// 是否是超级管理员
  229. bool isSuperAdmin() {
  230. if (state.user == null) return false;
  231. return state.user!.userName == "admin" ||
  232. (state.roles?.contains("super_admin") ?? false);
  233. }
  234. }
  235. // 添加 Provider 实例
  236. final authStoreProvider = StateNotifierProvider<AuthStore, AuthInfo>(
  237. (ref) => AuthStore(),
  238. );