一次大的提交
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:get_it/get_it.dart';
|
||||
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/core/consts/http_api_consts.dart';
|
||||
import 'package:maibu_satabot_v2/core/storage/user_storage.dart';
|
||||
import 'package:maibu_satabot_v2/features/auth/presentation/bloc/auth_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/home/data/datasources/site_datasource.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/home/domain/entities/site_entity.dart';
|
||||
|
||||
@@ -14,42 +15,38 @@ class SiteDataSourceImpl implements SiteDataSource {
|
||||
SiteDataSourceImpl(this.dio, this._userStorage, this._appUserCubit);
|
||||
|
||||
@override
|
||||
Future<List<SiteEntity>> getSiteList(int orgId) async {
|
||||
Future<List<SiteEntity>> getSiteList(String userId) async {
|
||||
print('🔍 [SiteDataSource] 开始获取 Token...');
|
||||
print('🔍 [SiteDataSource] AppUserCubit 当前用户: ${_appUserCubit.state.user?.username}');
|
||||
|
||||
// 优先从全局状态获取 Token(更快更可靠)
|
||||
print(
|
||||
'🔍 [SiteDataSource] AppUserCubit 当前用户: ${_appUserCubit.state.user?.username}',
|
||||
);
|
||||
|
||||
var token = _appUserCubit.state.user?.token;
|
||||
|
||||
print('🔍 [SiteDataSource] 从 AppUserCubit 获取的 Token: ${token != null ? "${token.substring(0, 20)}..." : "null"}');
|
||||
|
||||
// 如果全局状态没有,再从本地存储获取
|
||||
|
||||
print(
|
||||
'🔍 [SiteDataSource] 从 AppUserCubit 获取的 Token: ${token != null ? "${token.substring(0, 20)}..." : "null"}',
|
||||
);
|
||||
|
||||
if (token == null) {
|
||||
print('⚠️ [SiteDataSource] AppUserCubit 没有 Token,尝试从本地存储获取...');
|
||||
final user = await _userStorage.getUser();
|
||||
token = user?.token;
|
||||
print('🔍 [SiteDataSource] 从本地存储获取的 Token: ${token != null ? "${token.substring(0, 20)}..." : "null"}');
|
||||
print(
|
||||
'🔍 [SiteDataSource] 从本地存储获取的 Token: ${token != null ? "${token.substring(0, 20)}..." : "null"}',
|
||||
);
|
||||
}
|
||||
|
||||
print('🔑 [SiteDataSource] 获取到的 Token: ${token != null ? "${token.substring(0, 20)}..." : "null"}');
|
||||
|
||||
// 构建查询参数:orgId 为 0 时不传递
|
||||
final queryParams = <String, dynamic>{
|
||||
'pageNum': 1,
|
||||
'pageSize': 9999,
|
||||
};
|
||||
|
||||
if (orgId != 0) {
|
||||
queryParams['orgId'] = orgId;
|
||||
}
|
||||
|
||||
print(
|
||||
'🔑 [SiteDataSource] 获取到的 Token: ${token != null ? "${token.substring(0, 20)}..." : "null"}',
|
||||
);
|
||||
|
||||
final queryParams = <String, dynamic>{'userId': userId};
|
||||
|
||||
final response = await dio.get(
|
||||
HttpApiConsts.getSiteList,
|
||||
queryParameters: queryParams,
|
||||
options: Options(
|
||||
headers: {
|
||||
'Authorization': token != null ? 'Bearer $token' : '',
|
||||
},
|
||||
headers: {'Authorization': token != null ? 'Bearer $token' : ''},
|
||||
),
|
||||
);
|
||||
|
||||
@@ -59,17 +56,27 @@ class SiteDataSourceImpl implements SiteDataSource {
|
||||
|
||||
final responseData = response.data;
|
||||
|
||||
if (responseData['code'] == 401 || responseData['code'] == 403) {
|
||||
print(
|
||||
'🚨 [SiteDataSource] 收到认证错误码 ${responseData['code']},触发 Token 过期处理',
|
||||
);
|
||||
try {
|
||||
GetIt.I<AuthCubit>().tokenExpired();
|
||||
} catch (e) {}
|
||||
throw Exception('登录已过期,请重新登录');
|
||||
}
|
||||
|
||||
if (responseData['code'] != 200) {
|
||||
throw Exception(responseData['msg'] ?? '业务异常');
|
||||
}
|
||||
|
||||
final List<dynamic> rows = responseData['rows'] ?? [];
|
||||
|
||||
final List<dynamic> rows = responseData['data'] ?? [];
|
||||
|
||||
print('📤 [SiteDataSource] 接口返回原始数据:');
|
||||
for (var i = 0; i < rows.length && i < 3; i++) {
|
||||
print(' 场站$i: ${rows[i]}');
|
||||
}
|
||||
|
||||
|
||||
return rows.map((item) => SiteEntity.fromJson(item)).toList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user