Browse Source

feat(举报): 添加AI内容举报功能

- 在API服务中添加举报接口
- 创建ReportController处理举报逻辑
- 重构举报表单为独立组件
- 实现举报原因选择和提交功能
newdev_shunjiawei
lxm 1 year ago
parent
commit
ee6babf4aa
  1. 9
      lib/data/services/network/api.dart
  2. 2
      lib/modules/agent/bindings/agent_binding.dart
  3. 74
      lib/modules/agent/controllers/report_controller.dart
  4. 410
      lib/modules/agent/views/agent_view.dart

9
lib/data/services/network/api.dart

@ -89,4 +89,13 @@ class Api {
params: params, params: params,
); );
} }
//举报AI内容
static reportAiContext([params]) {
return DioManager().request(
NWMethod.post,
'/api/home/user_reportaicontext',
params: params,
);
}
} }

2
lib/modules/agent/bindings/agent_binding.dart

@ -1,9 +1,11 @@
import 'package:get/get.dart'; import 'package:get/get.dart';
import '../controllers/agent_controller.dart'; import '../controllers/agent_controller.dart';
import '../controllers/report_controller.dart';
class AgentBinding extends Bindings { class AgentBinding extends Bindings {
@override @override
void dependencies() { void dependencies() {
Get.lazyPut<AgentController>(() => AgentController()); Get.lazyPut<AgentController>(() => AgentController());
Get.lazyPut<ReportController>(() => ReportController());
} }
} }

74
lib/modules/agent/controllers/report_controller.dart

@ -0,0 +1,74 @@
import 'package:get/get.dart';
import 'agent_controller.dart'; // 确保导入Message模型
import 'package:flutter/material.dart';
import '../../../data/services/network/api.dart';
class ReportController extends GetxController {
// 当前举报的消息
final reportingMessage = Rxn<Message>();
// 选择的举报原因
List<String> selectedReasons = [];
// 举报描述
final reportDescription = ''.obs;
// 初始化举报状态
void initReport(Message message) {
reportingMessage.value = message;
selectedReasons.clear();
reportDescription.value = '';
}
// 切换举报原因选择状态
void toggleReason(String reason) {
if (selectedReasons.contains(reason)) {
selectedReasons.remove(reason);
} else {
selectedReasons.add(reason);
}
update();
}
// 更新举报描述
void updateDescription(String text) {
reportDescription.value = text;
}
// 提交举报
Future<void> submitReport() async {
if (reportingMessage.value == null) return;
// 构建举报数据
final reportData = {
'describe': reportDescription.value,
'message': reportingMessage.value,
'reportContext': reportingMessage.value,
'reporttype': selectedReasons.toList(),
};
print('提交举报数据: ${reportData}');
//调用API提交举报:
final Response = Api.reportAiContext(reportData);
print('提交举报响应: $Response');
// 提交成功后重置状态
selectedReasons.clear();
reportDescription.value = '';
reportingMessage.value = null;
// 关闭举报表单
Navigator.of(Get.context!).pop();
// 显示成功通知
Get.snackbar(
'提交成功',
'举报已提交,我们会尽快处理',
backgroundColor: Colors.green.withOpacity(0.8),
colorText: Colors.white,
duration: const Duration(seconds: 2),
);
}
}

410
lib/modules/agent/views/agent_view.dart

@ -2,6 +2,7 @@ import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import '../controllers/agent_controller.dart'; import '../controllers/agent_controller.dart';
import '../controllers/report_controller.dart';
import 'message_bubble.dart'; import 'message_bubble.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
@ -609,25 +610,272 @@ class AgentView extends StatelessWidget {
isDarkMode ? Colors.red[700]! : Colors.red, isDarkMode ? Colors.red[700]! : Colors.red,
() { () {
// 实现举报功能 // 实现举报功能
final reportController = Get.find<ReportController>();
reportController.initReport(message);
_showReportForm(context, isDarkMode);
// showModalBottomSheet(
// context: context,
// isScrollControlled: true, // 关键:允许弹窗随键盘移动
// backgroundColor:
// isDarkMode ? AppColors.cardBackground : Colors.white,
// shape: RoundedRectangleBorder(
// borderRadius: BorderRadius.vertical(top: Radius.circular(16.r)),
// ),
// builder: (BuildContext context) {
// return Padding(
// // 添加底部内边距以容纳键盘
// padding: EdgeInsets.only(
// bottom: MediaQuery.of(context).viewInsets.bottom,
// ),
// child: Container(
// padding: EdgeInsets.all(16.w),
// child: SingleChildScrollView(
// // 添加滚动视图
// child: Column(
// mainAxisSize: MainAxisSize.min,
// crossAxisAlignment: CrossAxisAlignment.stretch,
// children: [
// Stack(
// alignment: Alignment.center,
// children: [
// Center(
// child: Text(
// '举报',
// style: TextStyle(
// fontSize: 18.sp,
// fontWeight: FontWeight.bold,
// color: isDarkMode
// ? Colors.white
// : Colors.black,
// ),
// ),
// ),
// SizedBox(
// height: 20.w,
// ),
// Positioned(
// right: 0,
// child: IconButton(
// icon: Icon(
// Icons.close,
// color: isDarkMode
// ? Colors.grey[400]
// : Colors.black54,
// ),
// onPressed: () => Navigator.pop(context),
// ),
// ),
// ],
// ),
// Container(
// padding: EdgeInsets.symmetric(horizontal: 12.w),
// margin: EdgeInsets.only(bottom: 10.w),
// child: Divider(
// height: 1,
// color: Colors.grey[300],
// ),
// ),
// Wrap(
// spacing: 8.w,
// runSpacing: 8.h,
// children: [
// _buildReportChip('有害/不安全', isDarkMode),
// _buildReportChip('色情低俗', isDarkMode),
// _buildReportChip('误导攻击', isDarkMode),
// _buildReportChip('违法犯罪', isDarkMode),
// _buildReportChip('隐私相关', isDarkMode),
// _buildReportChip('侵犯我的著作权、名誉权等', isDarkMode),
// _buildReportChip('其他', isDarkMode),
// ],
// ),
// SizedBox(height: 16.h),
// Container(
// padding: EdgeInsets.all(12.w),
// decoration: BoxDecoration(
// color: isDarkMode
// ? Colors.grey[800]
// : Colors.grey[200],
// borderRadius: BorderRadius.circular(8.r),
// ),
// child: TextField(
// maxLines: 3,
// style: TextStyle(
// color: isDarkMode ? Colors.white : Colors.black,
// ),
// decoration: InputDecoration(
// hintText: '我们想知道你举报的原因,你可以描述你遇到的问题。',
// hintStyle: TextStyle(
// color: isDarkMode
// ? Colors.white70
// : Colors.black54,
// fontSize: 14.sp,
// ),
// border: InputBorder.none,
// ),
// ),
// ),
// SizedBox(height: 14.h),
// ElevatedButton(
// style: ElevatedButton.styleFrom(
// backgroundColor: Colors.blue,
// foregroundColor: Colors.white,
// padding: EdgeInsets.symmetric(vertical: 12.h),
// shape: RoundedRectangleBorder(
// borderRadius: BorderRadius.circular(8.r),
// ),
// ),
// child: Text('提交'),
// onPressed: () {
// Navigator.pop(context);
// Get.snackbar(
// '提示',
// '举报已提交,我们会尽快处理',
// backgroundColor: Colors.green.withOpacity(0.8),
// colorText: Colors.white,
// );
// },
// ),
// SizedBox(height: 8.h),
// ],
// ),
// ),
// ),
// );
// },
// );
},
),
],
);
}
// 更新_showReportForm方法
void _showReportForm(BuildContext context, bool isDarkMode) {
showModalBottomSheet( showModalBottomSheet(
context: context, context: context,
isScrollControlled: true, // 关键:允许弹窗随键盘移动 isScrollControlled: true,
backgroundColor: backgroundColor: isDarkMode ? AppColors.cardBackground : Colors.white,
isDarkMode ? AppColors.cardBackground : Colors.white,
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(16.r)), borderRadius: BorderRadius.vertical(top: Radius.circular(16.r)),
), ),
builder: (BuildContext context) { builder: (BuildContext context) {
return Padding( return Padding(
// 添加底部内边距以容纳键盘
padding: EdgeInsets.only( padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom, bottom: MediaQuery.of(context).viewInsets.bottom,
), ),
child: _ReportForm(isDarkMode: isDarkMode),
);
},
);
}
// // 添加这个辅助方法来创建举报选项
// Widget _buildReportChip(String label, bool isDarkMode) {
// return ChoiceChip(
// label: Text(
// label,
// style: TextStyle(
// color: isDarkMode ? Colors.white : Colors.black87,
// fontSize: 14.sp,
// ),
// ),
// selected: false,
// backgroundColor: isDarkMode ? Colors.grey[800] : Colors.grey[200],
// shape: RoundedRectangleBorder(
// borderRadius: BorderRadius.circular(16.r),
// ),
// onSelected: (bool selected) {
// // 可以在这里处理选择逻辑
// },
// );
// }
PopupMenuEntry _popupMenuEntry(
Icon icon,
String title,
Color color,
Function() onTap,
) {
return PopupMenuItem(
onTap: onTap,
child: SizedBox(
width: 140.w,
child: Row(
children: [
icon,
8.horizontalSpace,
Text(
title,
style: TextStyle(
color: color,
fontSize: 14.sp,
),
),
],
),
),
);
}
// 构建提示选项芯片
Widget _buildPromptChip({
required BuildContext context,
required String label,
required VoidCallback onTap,
}) {
final theme = Theme.of(context);
final primaryColor = theme.primaryColor;
return GestureDetector(
onTap: onTap,
child: Container( child: Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color: primaryColor.withOpacity(0.08),
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: primaryColor.withOpacity(0.2),
width: 1,
),
),
child: Text(
label,
style: TextStyle(
fontSize: 13,
color: primaryColor,
fontWeight: FontWeight.w500,
),
),
),
);
}
// 构建声音柱
Widget _buildSoundBar(int index) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 1),
child: Obx(() => _SoundBar(
delay: index * 0.1,
isActive: Get.find<AgentController>().isListening.value,
)),
);
}
}
class _ReportForm extends StatelessWidget {
final bool isDarkMode;
final ReportController reportController = Get.find<ReportController>();
_ReportForm({required this.isDarkMode});
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(16.w), padding: EdgeInsets.all(16.w),
child: SingleChildScrollView( child: SingleChildScrollView(
// 添加滚动视图 child: GetBuilder<ReportController>(
child: Column( builder: (_) => Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch, crossAxisAlignment: CrossAxisAlignment.stretch,
children: [ children: [
@ -640,23 +888,16 @@ class AgentView extends StatelessWidget {
style: TextStyle( style: TextStyle(
fontSize: 18.sp, fontSize: 18.sp,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
color: isDarkMode color: isDarkMode ? Colors.white : Colors.black,
? Colors.white
: Colors.black,
),
), ),
), ),
SizedBox(
height: 20.w,
), ),
Positioned( Positioned(
right: 0, right: 0,
child: IconButton( child: IconButton(
icon: Icon( icon: Icon(
Icons.close, Icons.close,
color: isDarkMode color: isDarkMode ? Colors.grey[400] : Colors.black54,
? Colors.grey[400]
: Colors.black54,
), ),
onPressed: () => Navigator.pop(context), onPressed: () => Navigator.pop(context),
), ),
@ -671,39 +912,40 @@ class AgentView extends StatelessWidget {
color: Colors.grey[300], color: Colors.grey[300],
), ),
), ),
Wrap( GetBuilder<ReportController>(
builder: (controller) {
return Wrap(
spacing: 8.w, spacing: 8.w,
runSpacing: 8.h, runSpacing: 8.h,
children: [ children: [
_buildReportChip('有害/不安全', isDarkMode), _buildReportChip('有害/不安全', isDarkMode, controller),
_buildReportChip('色情低俗', isDarkMode), _buildReportChip('色情低俗', isDarkMode, controller),
_buildReportChip('误导攻击', isDarkMode), _buildReportChip('误导攻击', isDarkMode, controller),
_buildReportChip('违法犯罪', isDarkMode), _buildReportChip('违法犯罪', isDarkMode, controller),
_buildReportChip('隐私相关', isDarkMode), _buildReportChip('隐私相关', isDarkMode, controller),
_buildReportChip('侵犯我的著作权、名誉权等', isDarkMode), _buildReportChip('侵犯我的著作权、名誉权等', isDarkMode, controller),
_buildReportChip('其他', isDarkMode), _buildReportChip('其他', isDarkMode, controller),
], ],
);
},
), ),
SizedBox(height: 16.h), SizedBox(height: 16.h),
Container( Container(
padding: EdgeInsets.all(12.w), padding: EdgeInsets.all(12.w),
decoration: BoxDecoration( decoration: BoxDecoration(
color: isDarkMode color: isDarkMode ? Colors.grey[800] : Colors.grey[200],
? Colors.grey[800]
: Colors.grey[200],
borderRadius: BorderRadius.circular(8.r), borderRadius: BorderRadius.circular(8.r),
), ),
child: TextField( child: TextField(
maxLines: 3, maxLines: 3,
onChanged: reportController.updateDescription,
style: TextStyle( style: TextStyle(
color: isDarkMode ? Colors.white : Colors.black, color: isDarkMode ? Colors.white : Colors.black,
), ),
decoration: InputDecoration( decoration: InputDecoration(
hintText: '我们想知道你举报的原因,你可以描述你遇到的问题。', hintText: '我们想知道你举报的原因,你可以描述你遇到的问题。',
hintStyle: TextStyle( hintStyle: TextStyle(
color: isDarkMode color: isDarkMode ? Colors.white70 : Colors.black54,
? Colors.white70
: Colors.black54,
fontSize: 14.sp, fontSize: 14.sp,
), ),
border: InputBorder.none, border: InputBorder.none,
@ -721,15 +963,7 @@ class AgentView extends StatelessWidget {
), ),
), ),
child: Text('提交'), child: Text('提交'),
onPressed: () { onPressed: () => reportController.submitReport(),
Navigator.pop(context);
Get.snackbar(
'提示',
'举报已提交,我们会尽快处理',
backgroundColor: Colors.green.withOpacity(0.8),
colorText: Colors.white,
);
},
), ),
SizedBox(height: 8.h), SizedBox(height: 8.h),
], ],
@ -737,105 +971,41 @@ class AgentView extends StatelessWidget {
), ),
), ),
); );
},
);
},
),
],
);
} }
// 添加这个辅助方法来创建举报选项 Widget _buildReportChip(
Widget _buildReportChip(String label, bool isDarkMode) { String label, bool isDarkMode, ReportController controller) {
return ChoiceChip( final isSelected = reportController.selectedReasons.contains(label);
label: Text(
label,
style: TextStyle(
color: isDarkMode ? Colors.white : Colors.black87,
fontSize: 14.sp,
),
),
selected: false,
backgroundColor: isDarkMode ? Colors.grey[800] : Colors.grey[200],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.r),
),
onSelected: (bool selected) {
// 可以在这里处理选择逻辑
},
);
}
PopupMenuEntry _popupMenuEntry(
Icon icon,
String title,
Color color,
Function() onTap,
) {
return PopupMenuItem(
onTap: onTap,
child: SizedBox(
width: 140.w,
child: Row(
children: [
icon,
8.horizontalSpace,
Text(
title,
style: TextStyle(
color: color,
fontSize: 14.sp,
),
),
],
),
),
);
}
// 构建提示选项芯片
Widget _buildPromptChip({
required BuildContext context,
required String label,
required VoidCallback onTap,
}) {
final theme = Theme.of(context);
final primaryColor = theme.primaryColor;
return GestureDetector( return GestureDetector(
onTap: onTap, onTap: () => reportController.toggleReason(label),
child: Container( child: Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
decoration: BoxDecoration( decoration: BoxDecoration(
color: primaryColor.withOpacity(0.08), color: isSelected
borderRadius: BorderRadius.circular(16), ? Colors.red
: (isDarkMode ? Colors.grey[800] : Colors.grey[200]),
borderRadius: BorderRadius.circular(16.r),
border: Border.all( border: Border.all(
color: primaryColor.withOpacity(0.2), color: isSelected
width: 1, ? Colors.red[700]!
: (isDarkMode ? Colors.grey[700]! : Colors.grey[300]!),
width: isSelected ? 1.0 : 0.5,
), ),
), ),
child: Text( child: Text(
label, label,
style: TextStyle( style: TextStyle(
fontSize: 13, color: isSelected
color: primaryColor, ? Colors.white
fontWeight: FontWeight.w500, : (isDarkMode ? Colors.white : Colors.black87),
fontSize: 14.sp,
fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
), ),
), ),
), ),
); );
} }
// 构建声音柱
Widget _buildSoundBar(int index) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 1),
child: Obx(() => _SoundBar(
delay: index * 0.1,
isActive: Get.find<AgentController>().isListening.value,
)),
);
}
} }
// 声音柱动画组件 // 声音柱动画组件

Loading…
Cancel
Save