feat(app): 内容安全词库 + 过滤服务 + 分享前检查 — 28 个测试全覆盖
新增文件: - sensitive_words.dart — 8 分类 ~200 条敏感词 + 谐音/形近/数字变体映射 - content_filter_service.dart — 精确匹配 + 变体匹配 + 文本预处理(去零宽/空格/符号) - content_filter_service_test.dart — 28 个测试(8分类精确/安全内容/预处理/变体/边界/词库完整性) 修改: - share_bottom_sheet.dart — 分享到班级前调用 ContentFilterService, 有敏感词时弹出警告对话框(返回修改/仍然分享),新增 contentText 参数
This commit is contained in:
@@ -4,20 +4,27 @@
|
||||
// - 温暖友好的文案(面向小学生)
|
||||
// - 分享到班级(有班级时显示)/ 仅自己可见
|
||||
// - 无班级时提示加入班级后可分享
|
||||
// - 分享前自动进行内容安全检查(敏感词过滤)
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../data/services/content_filter_service.dart';
|
||||
|
||||
/// 编辑器完成后的分享选择面板
|
||||
class ShareBottomSheet extends StatelessWidget {
|
||||
final String? classId;
|
||||
final String className;
|
||||
final void Function(bool shareToClass) onDecision;
|
||||
|
||||
/// 用于内容安全检查的文本内容(标题 + 文本元素)
|
||||
final String contentText;
|
||||
|
||||
const ShareBottomSheet({
|
||||
super.key,
|
||||
required this.classId,
|
||||
required this.className,
|
||||
required this.onDecision,
|
||||
this.contentText = '',
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -65,10 +72,7 @@ class ShareBottomSheet extends StatelessWidget {
|
||||
width: double.infinity,
|
||||
height: 52,
|
||||
child: FilledButton.icon(
|
||||
onPressed: () {
|
||||
onDecision(true);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
onPressed: () => _handleShare(context, shareToClass: true),
|
||||
icon: const Icon(Icons.group),
|
||||
label: Text('分享到 $className'),
|
||||
style: FilledButton.styleFrom(
|
||||
@@ -86,10 +90,7 @@ class ShareBottomSheet extends StatelessWidget {
|
||||
width: double.infinity,
|
||||
height: 52,
|
||||
child: OutlinedButton.icon(
|
||||
onPressed: () {
|
||||
onDecision(false);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
onPressed: () => _handleShare(context, shareToClass: false),
|
||||
icon: const Icon(Icons.lock_outline),
|
||||
label: const Text('仅自己可见'),
|
||||
style: OutlinedButton.styleFrom(
|
||||
@@ -116,4 +117,80 @@ class ShareBottomSheet extends StatelessWidget {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 处理分享/保存决定
|
||||
void _handleShare(BuildContext context, {required bool shareToClass}) {
|
||||
// 仅在分享到班级时进行内容安全检查
|
||||
if (shareToClass && contentText.isNotEmpty) {
|
||||
final matches = ContentFilterService.checkText(contentText);
|
||||
if (matches.isNotEmpty) {
|
||||
_showContentWarning(context, matches);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 安全或仅自己可见 → 直接执行
|
||||
onDecision(shareToClass);
|
||||
Navigator.pop(context);
|
||||
}
|
||||
|
||||
/// 显示内容安全警告对话框
|
||||
void _showContentWarning(
|
||||
BuildContext context,
|
||||
List<SensitiveWordMatch> matches,
|
||||
) {
|
||||
final categories = ContentFilterService.getMatchedCategories(matches);
|
||||
final words = matches.map((m) => ' "${m.word}"').toSet().toList();
|
||||
final wordList = words.take(5).join('、');
|
||||
final categoryList = categories.join('、');
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
title: const Row(
|
||||
children: [
|
||||
Icon(Icons.warning_amber_rounded, color: Colors.orange),
|
||||
SizedBox(width: 8),
|
||||
Text('内容提醒'),
|
||||
],
|
||||
),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('日记中可能包含不太合适分享的内容:'),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
wordList,
|
||||
style: const TextStyle(fontWeight: FontWeight.w600),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'涉及:$categoryList',
|
||||
style: TextStyle(fontSize: 13, color: Colors.grey.shade600),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
const Text(
|
||||
'建议修改后再分享,或者先保存为仅自己可见。',
|
||||
style: TextStyle(fontSize: 13),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(dialogContext),
|
||||
child: const Text('返回修改'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(dialogContext); // 关闭对话框
|
||||
onDecision(true); // 仍然分享
|
||||
Navigator.pop(context); // 关闭 BottomSheet
|
||||
},
|
||||
child: const Text('仍然分享'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user