一次大的提交
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import '../../domain/entities/site_entity.dart';
|
||||
|
||||
abstract class SiteDataSource {
|
||||
Future<List<SiteEntity>> getSiteList(int orgId);
|
||||
Future<List<SiteEntity>> getSiteList(String userId);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ class SiteRepositoryImpl implements SiteRepository {
|
||||
SiteRepositoryImpl(this.dataSource);
|
||||
|
||||
@override
|
||||
Future<Either<Failure, List<SiteEntity>>> getSiteList(int orgId) async {
|
||||
Future<Either<Failure, List<SiteEntity>>> getSiteList(String userId) async {
|
||||
try {
|
||||
final sites = await dataSource.getSiteList(orgId);
|
||||
final sites = await dataSource.getSiteList(userId);
|
||||
return Right(sites);
|
||||
} catch (e) {
|
||||
return Left(Failure(e.toString()));
|
||||
|
||||
@@ -5,5 +5,5 @@ import '../../../../../core/error/failure.dart';
|
||||
import '../entities/site_entity.dart';
|
||||
|
||||
abstract class SiteRepository {
|
||||
Future<Either<Failure, List<SiteEntity>>> getSiteList(int orgId);
|
||||
Future<Either<Failure, List<SiteEntity>>> getSiteList(String userId);
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ class GetSiteListUseCase {
|
||||
|
||||
GetSiteListUseCase(this.repository);
|
||||
|
||||
// pageNum 和 pageSize 固定,orgId 从登录用户信息中获取
|
||||
Future<Either<Failure, List<SiteEntity>>> call(int orgId) async {
|
||||
return await repository.getSiteList(orgId);
|
||||
// userId 从登录用户信息中获取
|
||||
Future<Either<Failure, List<SiteEntity>>> call(String userId) async {
|
||||
return await repository.getSiteList(userId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,12 @@ class HomeV2Bloc extends Bloc<HomeV2Event, HomeV2State> {
|
||||
final AppUserCubit appUserCubit;
|
||||
final SiteCubit siteCubit;
|
||||
|
||||
HomeV2Bloc(this.getHomeDataUseCase, this.getSiteListUseCase, this.appUserCubit, this.siteCubit) : super(const HomeV2Initial()) {
|
||||
HomeV2Bloc(
|
||||
this.getHomeDataUseCase,
|
||||
this.getSiteListUseCase,
|
||||
this.appUserCubit,
|
||||
this.siteCubit,
|
||||
) : super(const HomeV2Initial()) {
|
||||
on<HomeV2LoadData>(_onLoadData);
|
||||
on<HomeV2Refresh>(_onRefresh);
|
||||
on<HomeV2ToggleTrendType>(_onToggleTrendType);
|
||||
@@ -28,26 +33,25 @@ class HomeV2Bloc extends Bloc<HomeV2Event, HomeV2State> {
|
||||
|
||||
final user = appUserCubit.state.user;
|
||||
if (user == null) {
|
||||
emit(const HomeV2Error(
|
||||
message: '用户未登录',
|
||||
shouldShowError: true,
|
||||
));
|
||||
emit(const HomeV2Error(message: '用户未登录', shouldShowError: true));
|
||||
return;
|
||||
}
|
||||
|
||||
// 并行加载首页数据和场站列表
|
||||
final homeResult = await getHomeDataUseCase(const NoParams());
|
||||
final siteResult = await getSiteListUseCase(user.orgId); // 使用用户的 orgId
|
||||
final siteResult = await getSiteListUseCase(user.userId); // 使用用户的 userId
|
||||
|
||||
homeResult.fold(
|
||||
(failure) => emit(HomeV2Error(
|
||||
message: ErrorHandler.getErrorMessage(failure.message),
|
||||
shouldShowError: true, // 🔥 标记需要显示弹窗
|
||||
)),
|
||||
(failure) => emit(
|
||||
HomeV2Error(
|
||||
message: ErrorHandler.getErrorMessage(failure.message),
|
||||
shouldShowError: true, // 🔥 标记需要显示弹窗
|
||||
),
|
||||
),
|
||||
(homeData) {
|
||||
List<SiteEntity> sites = [];
|
||||
SiteEntity? selectedSite;
|
||||
|
||||
|
||||
siteResult.fold(
|
||||
(failure) {
|
||||
print('加载场站列表失败: ${failure.message}');
|
||||
@@ -56,7 +60,7 @@ class HomeV2Bloc extends Bloc<HomeV2Event, HomeV2State> {
|
||||
sites = siteList;
|
||||
// 从全局 SiteCubit 获取之前选中的场站
|
||||
final savedSelectedSite = siteCubit.state.selectedSite;
|
||||
|
||||
|
||||
// 尝试找到之前选中的场站
|
||||
if (savedSelectedSite != null && siteList.isNotEmpty) {
|
||||
selectedSite = siteList.firstWhere(
|
||||
@@ -67,19 +71,21 @@ class HomeV2Bloc extends Bloc<HomeV2Event, HomeV2State> {
|
||||
// 没有选中过,默认选中第一个
|
||||
selectedSite = siteList.isNotEmpty ? siteList.first : null;
|
||||
}
|
||||
|
||||
|
||||
// 更新全局 SiteCubit 的选中状态
|
||||
if (selectedSite != null) {
|
||||
siteCubit.selectSite(selectedSite!);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
emit(HomeV2Loaded(
|
||||
homeData: homeData,
|
||||
sites: sites,
|
||||
selectedSite: selectedSite,
|
||||
));
|
||||
|
||||
emit(
|
||||
HomeV2Loaded(
|
||||
homeData: homeData,
|
||||
sites: sites,
|
||||
selectedSite: selectedSite,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -90,29 +96,28 @@ class HomeV2Bloc extends Bloc<HomeV2Event, HomeV2State> {
|
||||
) async {
|
||||
if (state is HomeV2Loaded) {
|
||||
final currentState = state as HomeV2Loaded;
|
||||
|
||||
|
||||
final user = appUserCubit.state.user;
|
||||
if (user == null) {
|
||||
emit(const HomeV2Error(
|
||||
message: '用户未登录',
|
||||
shouldShowError: true,
|
||||
));
|
||||
emit(const HomeV2Error(message: '用户未登录', shouldShowError: true));
|
||||
return;
|
||||
}
|
||||
|
||||
// 并行刷新首页数据和电站列表
|
||||
final homeResult = await getHomeDataUseCase(const NoParams());
|
||||
final siteResult = await getSiteListUseCase(user.orgId);
|
||||
final siteResult = await getSiteListUseCase(user.userId);
|
||||
|
||||
homeResult.fold(
|
||||
(failure) => emit(HomeV2Error(
|
||||
message: ErrorHandler.getErrorMessage(failure.message),
|
||||
shouldShowError: true, // 🔥 标记需要显示弹窗
|
||||
)),
|
||||
(failure) => emit(
|
||||
HomeV2Error(
|
||||
message: ErrorHandler.getErrorMessage(failure.message),
|
||||
shouldShowError: true, // 🔥 标记需要显示弹窗
|
||||
),
|
||||
),
|
||||
(homeData) {
|
||||
List<SiteEntity> sites = currentState.sites;
|
||||
SiteEntity? selectedSite = currentState.selectedSite;
|
||||
|
||||
|
||||
siteResult.fold(
|
||||
(failure) {
|
||||
print('刷新场站列表失败: ${failure.message}');
|
||||
@@ -129,20 +134,22 @@ class HomeV2Bloc extends Bloc<HomeV2Event, HomeV2State> {
|
||||
} else if (siteList.isNotEmpty) {
|
||||
selectedSite = siteList.first;
|
||||
}
|
||||
|
||||
|
||||
// 更新全局 SiteCubit 的选中状态
|
||||
if (selectedSite != null) {
|
||||
siteCubit.selectSite(selectedSite!);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
emit(HomeV2Loaded(
|
||||
homeData: homeData,
|
||||
trendType: currentState.trendType,
|
||||
sites: sites,
|
||||
selectedSite: selectedSite,
|
||||
));
|
||||
|
||||
emit(
|
||||
HomeV2Loaded(
|
||||
homeData: homeData,
|
||||
trendType: currentState.trendType,
|
||||
sites: sites,
|
||||
selectedSite: selectedSite,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'package:maibu_satabot_v2/features/v2/home/presentation/bloc/home_v2_bloc
|
||||
import 'package:maibu_satabot_v2/features/v2/home/presentation/bloc/home_v2_event.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/home/presentation/bloc/home_v2_state.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/site/presentation/cubit/site_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/site/presentation/widgets/site_selector_widget.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/home/presentation/widgets/power_card.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/home/presentation/widgets/stats_grid.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/home/presentation/widgets/work_order_card.dart';
|
||||
@@ -60,9 +61,16 @@ class _HomeV2PageState extends State<HomeV2Page> {
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.error_outline, size: 48, color: Colors.orange),
|
||||
const Icon(
|
||||
Icons.error_outline,
|
||||
size: 48,
|
||||
color: Colors.orange,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(state.message, style: const TextStyle(fontSize: 14, color: Colors.grey)),
|
||||
Text(
|
||||
state.message,
|
||||
style: const TextStyle(fontSize: 14, color: Colors.grey),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: () => _bloc.add(const HomeV2LoadData()),
|
||||
@@ -91,47 +99,8 @@ class _HomeV2PageState extends State<HomeV2Page> {
|
||||
color: Colors.white,
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: InkWell(
|
||||
onTap: _showPlantSelector,
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Flexible(
|
||||
child: StreamBuilder<SiteState>(
|
||||
stream: sl<SiteCubit>().stream,
|
||||
builder: (context, snapshot) {
|
||||
final siteState =
|
||||
snapshot.data ??
|
||||
sl<SiteCubit>().state;
|
||||
return Text(
|
||||
siteState.selectedSite?.siteName ??
|
||||
AppLocalizations.of(
|
||||
context,
|
||||
).translate(
|
||||
'home_v2.select_site',
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Color(0xFF1D2129),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
const Icon(
|
||||
Icons.arrow_drop_down,
|
||||
size: 20,
|
||||
color: Color(0xFF1D2129),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(child: SiteSelectorWidget()),
|
||||
const SizedBox(width: 12),
|
||||
TcpStatusIndicator(
|
||||
onTap: () => _showDeviceStatusModal(context),
|
||||
),
|
||||
@@ -252,7 +221,9 @@ class _HomeV2PageState extends State<HomeV2Page> {
|
||||
}
|
||||
|
||||
// 🔥 Initial/Loading 状态显示加载指示器
|
||||
return const Center(child: CircularProgressIndicator(color: Color(0xFF165DFF)));
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(color: Color(0xFF165DFF)),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user