告警中心的二级详情页面+信息中心的页面+对接机器人接口

This commit is contained in:
2026-06-02 08:40:50 +08:00
parent 9eab402551
commit d8879dad62
48 changed files with 4376 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
import 'package:maibu_satabot_v2/features/v2/message_center/data/datasources/message_center_remote_datasource.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/data/models/message_category_model.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/domain/entities/message_category_entity.dart';
/// 消息中心远程数据源实现(模拟数据)
class MessageCenterRemoteDataSourceImpl implements MessageCenterRemoteDataSource {
@override
Future<List<MessageCategoryModel>> getMessageList({MessageType? type}) async {
// 模拟网络延迟
await Future.delayed(const Duration(seconds: 1));
final allMessages = [
MessageCategoryModel(
type: 'alarm',
icon: 'notifications',
iconColor: '#FF3B30',
title: '告警通知',
content: '逆变器 INV-001 离网告警',
time: '09:24',
unreadCount: 12,
targetId: 'ALM-001',
targetRoute: '/alarm-detail',
),
MessageCategoryModel(
type: 'workorder',
icon: 'assignment',
iconColor: '#FF9500',
title: '工单提醒',
content: '工单 #WO-2025051908 已派发',
time: '09:18',
unreadCount: 3,
targetId: 'WO-2025051908',
targetRoute: '/workorder-detail',
),
MessageCategoryModel(
type: 'system',
icon: 'campaign',
iconColor: '#165DFF',
title: '系统消息',
content: '系统升级维护通知',
time: '08:30',
unreadCount: 0,
targetId: null,
targetRoute: null,
),
MessageCategoryModel(
type: 'manager',
icon: 'chat',
iconColor: '#00B42A',
title: '负责人消息',
content: '张工:请尽快处理该告警',
time: '08:15',
unreadCount: 2,
targetId: null,
targetRoute: null,
),
MessageCategoryModel(
type: 'daily',
icon: 'description',
iconColor: '#722ED1',
title: '日报推送',
content: '5月18日光伏电站运行日报',
time: '07:30',
unreadCount: 0,
targetId: null,
targetRoute: null,
),
MessageCategoryModel(
type: 'safety',
icon: 'security',
iconColor: '#165DFF',
title: '安全通知',
content: '安全策略更新通知',
time: '昨日',
unreadCount: 0,
targetId: null,
targetRoute: null,
),
];
// 根据类型筛选
if (type != null && type != MessageType.all) {
return allMessages
.where((msg) => msg.type == type.name)
.toList();
}
return allMessages;
}
@override
Future<bool> markAllAsRead() async {
// 模拟网络延迟
await Future.delayed(const Duration(milliseconds: 500));
return true;
}
}

View File

@@ -0,0 +1,11 @@
import 'package:maibu_satabot_v2/features/v2/message_center/data/models/message_category_model.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/domain/entities/message_category_entity.dart';
/// 消息中心远程数据源抽象
abstract class MessageCenterRemoteDataSource {
/// 获取消息列表
Future<List<MessageCategoryModel>> getMessageList({MessageType? type});
/// 一键已读
Future<bool> markAllAsRead();
}

View File

@@ -0,0 +1,73 @@
import 'package:maibu_satabot_v2/features/v2/message_center/domain/entities/message_category_entity.dart';
/// 消息分类数据模型
class MessageCategoryModel {
MessageCategoryModel({
required this.type,
required this.icon,
required this.iconColor,
required this.title,
required this.content,
required this.time,
this.unreadCount = 0,
this.targetId,
this.targetRoute,
});
final String type;
final String icon;
final String iconColor;
final String title;
final String content;
final String time;
final int unreadCount;
final String? targetId;
final String? targetRoute;
factory MessageCategoryModel.fromJson(Map<String, dynamic> json) {
return MessageCategoryModel(
type: json['type'] as String,
icon: json['icon'] as String,
iconColor: json['iconColor'] as String,
title: json['title'] as String,
content: json['content'] as String,
time: json['time'] as String,
unreadCount: json['unreadCount'] as int? ?? 0,
targetId: json['targetId'] as String?,
targetRoute: json['targetRoute'] as String?,
);
}
MessageCategoryEntity toEntity() {
return MessageCategoryEntity(
type: _parseMessageType(type),
icon: icon,
iconColor: iconColor,
title: title,
content: content,
time: time,
unreadCount: unreadCount,
targetId: targetId,
targetRoute: targetRoute,
);
}
MessageType _parseMessageType(String type) {
switch (type) {
case 'alarm':
return MessageType.alarm;
case 'workorder':
return MessageType.workorder;
case 'system':
return MessageType.system;
case 'manager':
return MessageType.manager;
case 'daily':
return MessageType.daily;
case 'safety':
return MessageType.safety;
default:
return MessageType.all;
}
}
}

View File

@@ -0,0 +1,34 @@
import 'package:fpdart/fpdart.dart';
import 'package:maibu_satabot_v2/core/error/failure.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/data/datasources/message_center_remote_datasource.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/domain/entities/message_category_entity.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/domain/repositories/message_center_repository.dart';
/// 消息中心仓储实现
class MessageCenterRepositoryImpl implements MessageCenterRepository {
MessageCenterRepositoryImpl(this._remoteDataSource);
final MessageCenterRemoteDataSource _remoteDataSource;
@override
Future<Either<Failure, List<MessageCategoryEntity>>> getMessageList({
MessageType? type,
}) async {
try {
final models = await _remoteDataSource.getMessageList(type: type);
return Right(models.map((model) => model.toEntity()).toList());
} catch (e) {
return Left(ServerFailure('获取消息列表失败:$e'));
}
}
@override
Future<Either<Failure, bool>> markAllAsRead() async {
try {
final success = await _remoteDataSource.markAllAsRead();
return Right(success);
} catch (e) {
return Left(ServerFailure('标记已读失败:$e'));
}
}
}

View File

@@ -0,0 +1,94 @@
import 'package:equatable/equatable.dart';
/// 消息类型枚举
enum MessageType {
all('全部'),
alarm('告警'),
workorder('工单'),
system('系统'),
manager('负责人'),
daily('日报'),
safety('安全');
const MessageType(this.label);
final String label;
}
/// 消息分类实体
class MessageCategoryEntity extends Equatable {
const MessageCategoryEntity({
required this.type,
required this.icon,
required this.iconColor,
required this.title,
required this.content,
required this.time,
this.unreadCount = 0,
this.targetId,
this.targetRoute,
});
/// 消息类型
final MessageType type;
/// 图标名称
final String icon;
/// 图标颜色
final String iconColor;
/// 标题
final String title;
/// 内容摘要
final String content;
/// 时间
final String time;
/// 未读数量
final int unreadCount;
/// 目标ID(用于跳转)
final String? targetId;
/// 目标路由(用于跳转)
final String? targetRoute;
@override
List<Object?> get props => [
type,
icon,
iconColor,
title,
content,
time,
unreadCount,
targetId,
targetRoute,
];
MessageCategoryEntity copyWith({
MessageType? type,
String? icon,
String? iconColor,
String? title,
String? content,
String? time,
int? unreadCount,
String? targetId,
String? targetRoute,
}) {
return MessageCategoryEntity(
type: type ?? this.type,
icon: icon ?? this.icon,
iconColor: iconColor ?? this.iconColor,
title: title ?? this.title,
content: content ?? this.content,
time: time ?? this.time,
unreadCount: unreadCount ?? this.unreadCount,
targetId: targetId ?? this.targetId,
targetRoute: targetRoute ?? this.targetRoute,
);
}
}

View File

@@ -0,0 +1,14 @@
import 'package:fpdart/fpdart.dart';
import 'package:maibu_satabot_v2/core/error/failure.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/domain/entities/message_category_entity.dart';
/// 消息中心仓储抽象
abstract class MessageCenterRepository {
/// 获取消息列表
Future<Either<Failure, List<MessageCategoryEntity>>> getMessageList({
MessageType? type,
});
/// 一键已读
Future<Either<Failure, bool>> markAllAsRead();
}

View File

@@ -0,0 +1,28 @@
import 'package:fpdart/fpdart.dart';
import 'package:maibu_satabot_v2/core/error/failure.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/domain/entities/message_category_entity.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/domain/repositories/message_center_repository.dart';
/// 获取消息列表用例
class GetMessageListUseCase {
GetMessageListUseCase(this._repository);
final MessageCenterRepository _repository;
Future<Either<Failure, List<MessageCategoryEntity>>> call({
MessageType? type,
}) async {
return await _repository.getMessageList(type: type);
}
}
/// 一键已读用例
class MarkAllAsReadUseCase {
MarkAllAsReadUseCase(this._repository);
final MessageCenterRepository _repository;
Future<Either<Failure, bool>> call() async {
return await _repository.markAllAsRead();
}
}

View File

@@ -0,0 +1,67 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/domain/entities/message_category_entity.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/domain/usecases/message_center_usecase.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/presentation/bloc/message_center_state.dart';
/// 消息中心Cubit
class MessageCenterCubit extends Cubit<MessageCenterState> {
MessageCenterCubit({
required GetMessageListUseCase getMessageListUseCase,
required MarkAllAsReadUseCase markAllAsReadUseCase,
}) : _getMessageListUseCase = getMessageListUseCase,
_markAllAsReadUseCase = markAllAsReadUseCase,
super(MessageCenterInitial());
final GetMessageListUseCase _getMessageListUseCase;
final MarkAllAsReadUseCase _markAllAsReadUseCase;
/// 加载消息列表
Future<void> loadMessageList({MessageType? type}) async {
emit(MessageCenterLoading());
final result = await _getMessageListUseCase(type: type);
result.fold(
(failure) => emit(MessageCenterError(failure.message)),
(messageList) {
final totalUnread = messageList.fold<int>(
0,
(sum, msg) => sum + msg.unreadCount,
);
emit(MessageCenterLoaded(
messageList: messageList,
selectedType: type ?? MessageType.all,
totalUnread: totalUnread,
));
},
);
}
/// 切换消息类型筛选
Future<void> changeFilter(MessageType type) async {
await loadMessageList(type: type);
}
/// 一键已读
Future<void> markAllAsRead() async {
// 检查当前状态
if (state is! MessageCenterLoaded) return;
emit(const MessageCenterActionInProgress('一键已读'));
final result = await _markAllAsReadUseCase();
result.fold(
(failure) => emit(MessageCenterError(failure.message)),
(success) {
if (success) {
// 将所有消息的未读数清零
final currentState = state as MessageCenterLoaded;
final updatedList = currentState.messageList
.map((msg) => msg.copyWith(unreadCount: 0))
.toList();
emit(currentState.copyWith(
messageList: updatedList,
totalUnread: 0,
));
}
},
);
}
}

View File

@@ -0,0 +1,64 @@
import 'package:equatable/equatable.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/domain/entities/message_category_entity.dart';
/// 消息中心状态
sealed class MessageCenterState extends Equatable {
const MessageCenterState();
@override
List<Object?> get props => [];
}
/// 初始状态
final class MessageCenterInitial extends MessageCenterState {}
/// 加载状态
final class MessageCenterLoading extends MessageCenterState {}
/// 加载成功状态
final class MessageCenterLoaded extends MessageCenterState {
const MessageCenterLoaded({
required this.messageList,
this.selectedType = MessageType.all,
this.totalUnread = 0,
});
final List<MessageCategoryEntity> messageList;
final MessageType selectedType;
final int totalUnread;
@override
List<Object?> get props => [messageList, selectedType, totalUnread];
MessageCenterLoaded copyWith({
List<MessageCategoryEntity>? messageList,
MessageType? selectedType,
int? totalUnread,
}) {
return MessageCenterLoaded(
messageList: messageList ?? this.messageList,
selectedType: selectedType ?? this.selectedType,
totalUnread: totalUnread ?? this.totalUnread,
);
}
}
/// 加载失败状态
final class MessageCenterError extends MessageCenterState {
const MessageCenterError(this.message);
final String message;
@override
List<Object?> get props => [message];
}
/// 操作中状态(一键已读)
final class MessageCenterActionInProgress extends MessageCenterState {
const MessageCenterActionInProgress(this.action);
final String action;
@override
List<Object?> get props => [action];
}

View File

@@ -0,0 +1,414 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:maibu_satabot_v2/core/di/injection.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/domain/entities/message_category_entity.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/presentation/bloc/message_center_cubit.dart';
import 'package:maibu_satabot_v2/features/v2/message_center/presentation/bloc/message_center_state.dart';
import 'package:maibu_satabot_v2/features/v2/waring_center/presentation/constants/alarm_constants.dart';
/// 消息中心页面
class MessageCenterPage extends StatelessWidget {
const MessageCenterPage({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => sl<MessageCenterCubit>()..loadMessageList(),
child: const MessageCenterView(),
);
}
}
class MessageCenterView extends StatelessWidget {
const MessageCenterView({super.key});
@override
Widget build(BuildContext context) {
final cubit = context.read<MessageCenterCubit>();
return Scaffold(
backgroundColor: AlarmColors.background,
body: BlocConsumer<MessageCenterCubit, MessageCenterState>(
listener: (context, state) {
if (state is MessageCenterActionInProgress) {
// 显示加载提示
}
if (state is MessageCenterError) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(state.message)),
);
}
},
builder: (context, state) {
return SafeArea(
child: Column(
children: [
// 顶部导航栏
_buildAppBar(context, cubit),
// 内容区域
Expanded(
child: _buildContent(context, state),
),
],
),
);
},
),
);
}
Widget _buildAppBar(BuildContext context, MessageCenterCubit cubit) {
return Container(
height: 44.0,
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
// 左侧:下拉筛选菜单
_buildFilterDropdown(context, cubit),
const Spacer(),
// 右侧:一键已读按钮
GestureDetector(
onTap: () => cubit.markAllAsRead(),
child: const Text(
'一键已读',
style: TextStyle(
fontSize: 14,
color: AlarmColors.primary,
fontWeight: FontWeight.w500,
),
),
),
],
),
);
}
Widget _buildFilterDropdown(BuildContext context, MessageCenterCubit cubit) {
return BlocBuilder<MessageCenterCubit, MessageCenterState>(
builder: (context, state) {
MessageType selectedType = MessageType.all;
if (state is MessageCenterLoaded) {
selectedType = state.selectedType;
}
return DropdownButton<MessageType>(
value: selectedType,
underline: const SizedBox(),
icon: const Icon(
Icons.keyboard_arrow_down,
size: 20,
color: AlarmColors.textSecondary,
),
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: AlarmColors.textPrimary,
),
items: MessageType.values.map((type) {
return DropdownMenuItem<MessageType>(
value: type,
child: Text(type.label),
);
}).toList(),
onChanged: (value) {
if (value != null) {
cubit.changeFilter(value);
}
},
);
},
);
}
Widget _buildContent(BuildContext context, MessageCenterState state) {
if (state is MessageCenterLoading) {
return _buildSkeletonScreen();
}
if (state is MessageCenterError) {
return _buildErrorState(context, state.message);
}
if (state is MessageCenterLoaded) {
return _buildMessageList(context, state);
}
if (state is MessageCenterActionInProgress) {
return _buildLoadingOverlay(context, state.action);
}
return const SizedBox();
}
Widget _buildMessageList(BuildContext context, MessageCenterLoaded state) {
if (state.messageList.isEmpty) {
return Center(
child: Text(
'暂无消息',
style: const TextStyle(
fontSize: 14,
color: AlarmColors.textSecondary,
),
),
);
}
return ListView.separated(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
itemCount: state.messageList.length,
separatorBuilder: (context, index) => const SizedBox(height: 12),
itemBuilder: (context, index) {
final message = state.messageList[index];
return _MessageCategoryCard(
message: message,
onTap: () => _handleMessageTap(context, message),
);
},
);
}
void _handleMessageTap(BuildContext context, MessageCategoryEntity message) {
// 根据目标路由跳转
if (message.targetRoute != null) {
context.push(message.targetRoute!);
} else {
// 显示消息详情(可以后续扩展)
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('查看${message.title}详情')),
);
}
}
Widget _buildSkeletonScreen() {
return ListView.separated(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
itemCount: 6,
separatorBuilder: (context, index) => const SizedBox(height: 12),
itemBuilder: (context, index) {
return Container(
height: 80,
decoration: BoxDecoration(
color: AlarmColors.background,
borderRadius: BorderRadius.circular(AlarmDimensions.borderRadius),
),
child: const Center(
child: CircularProgressIndicator(),
),
);
},
);
}
Widget _buildErrorState(BuildContext context, String message) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.error_outline,
size: 64,
color: AlarmColors.danger,
),
const SizedBox(height: 16),
Text(
message,
style: const TextStyle(
fontSize: 14,
color: AlarmColors.textSecondary,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () {
context.read<MessageCenterCubit>().loadMessageList();
},
child: const Text('重试'),
),
],
),
);
}
Widget _buildLoadingOverlay(BuildContext context, String action) {
return Stack(
children: [
BlocBuilder<MessageCenterCubit, MessageCenterState>(
builder: (context, state) {
if (state is MessageCenterLoaded) {
return _buildMessageList(context, state);
}
return const SizedBox();
},
),
Container(
color: Colors.black.withOpacity(0.3),
child: Center(
child: Container(
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const CircularProgressIndicator(),
const SizedBox(height: 16),
Text(
'$action中...',
style: const TextStyle(
fontSize: 14,
color: AlarmColors.textSecondary,
),
),
],
),
),
),
),
],
);
}
}
/// 消息分类卡片组件
class _MessageCategoryCard extends StatelessWidget {
const _MessageCategoryCard({
required this.message,
required this.onTap,
});
final MessageCategoryEntity message;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
final isUnread = message.unreadCount > 0;
return GestureDetector(
onTap: onTap,
child: Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: AlarmColors.background,
borderRadius: BorderRadius.circular(AlarmDimensions.borderRadius),
),
child: Row(
children: [
// 左侧图标
_buildIcon(),
const SizedBox(width: 12),
// 中间内容
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 标题
Text(
message.title,
style: TextStyle(
fontSize: 15,
fontWeight: isUnread ? FontWeight.bold : FontWeight.w500,
color: AlarmColors.textPrimary,
),
),
const SizedBox(height: 4),
// 内容摘要
Text(
message.content,
style: const TextStyle(
fontSize: 13,
color: AlarmColors.textSecondary,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
const SizedBox(width: 12),
// 右侧信息
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
// 未读角标
if (isUnread) _buildUnreadBadge(),
// 时间
Text(
message.time,
style: const TextStyle(
fontSize: 12,
color: AlarmColors.textSecondary,
),
),
],
),
],
),
),
);
}
Widget _buildIcon() {
final iconColor = Color(
int.parse(message.iconColor.replaceAll('#', '0xFF')),
);
IconData iconData;
switch (message.icon) {
case 'notifications':
iconData = Icons.notifications;
break;
case 'assignment':
iconData = Icons.assignment;
break;
case 'campaign':
iconData = Icons.campaign;
break;
case 'chat':
iconData = Icons.chat_bubble;
break;
case 'description':
iconData = Icons.description;
break;
case 'security':
iconData = Icons.security;
break;
default:
iconData = Icons.notifications;
}
return Container(
width: 40,
height: 40,
decoration: BoxDecoration(
color: iconColor.withOpacity(0.1),
borderRadius: BorderRadius.circular(8),
),
child: Icon(
iconData,
color: iconColor,
size: 20,
),
);
}
Widget _buildUnreadBadge() {
return Padding(
padding: const EdgeInsets.only(bottom: 4),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
decoration: BoxDecoration(
color: AlarmColors.danger,
borderRadius: BorderRadius.circular(10),
),
child: Text(
message.unreadCount.toString(),
style: const TextStyle(
fontSize: 11,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
);
}
}