Add AI batch reconciliation for accessibility bills

This commit is contained in:
2026-07-25 18:41:09 +08:00
parent eb8909a192
commit 7cca34b331
25 changed files with 2771 additions and 230 deletions
@@ -19,7 +19,7 @@ class LegalDocumentPage extends StatelessWidget {
final LegalDocumentKind kind;
const LegalDocumentPage({super.key, required this.kind});
static const _effectiveDate = '2026 年 7 月 21';
static const _effectiveDate = '2026 年 7 月 25';
@override
Widget build(BuildContext context) {
@@ -83,7 +83,7 @@ class LegalDocumentPage extends StatelessWidget {
),
_LegalSection(
'二、语音、图片与 AI 数据',
'使用语音记账时,麦克风音频由设备系统语音识别能力处理,记之接收识别后的文字;使用拍照或相册识别时,只有你主动选择的图片会用于本次识别。开启无障碍事件识别后,记之会在微信、支付宝疑似支付流程结束时按需截取当前页面,并由设备内置 OCR 在内存中识别,图片不落盘且处理后立即释放。只有你另行开启 AI 截图补全时,当前支付页图片才会发送至我们配置的火山方舟大模型服务。我们不会将完整账单历史无差别发送给模型,也不会把这些数据用于广告画像。',
'使用语音记账时,麦克风音频由设备系统语音识别能力处理,记之接收识别后的文字;使用拍照或相册识别时,只有你主动选择的图片会用于本次识别。开启无障碍事件识别后,记之会在微信、支付宝疑似支付流程结束时按需截取当前页面,并由设备内置 OCR 在内存中识别。只有你另行开启 AI 批次对账时,支付结果页截图与本地候选才会按 30 秒空闲窗口成批发送至我们配置的火山方舟大模型服务,单批最长等待 2 分钟或累计 10 条。AI 仅可对当前批次执行保留、修正、补全或剔除,不能改动批次外账单。待提交图片只在设备上临时加密保存,批次完成或失败回退后立即删除。我们不会将完整账单历史无差别发送给模型,也不会把这些数据用于广告画像。',
),
_LegalSection(
'三、设备、网络与日志',
@@ -91,7 +91,7 @@ class LegalDocumentPage extends StatelessWidget {
),
_LegalSection(
'四、存储期限与安全',
'账号数据在你使用服务期间保存。你删除的账单进入 30 天回收站;截屏识别文件在完成、取消或失败后清理,遗留文件会在超过 24 小时后清理。账号注销进入 15 天后悔期,到期后永久删除账号关联数据。导出文件由你主动分享,应用会在分享完成或失败后清理临时副本。',
'账号数据在你使用服务期间保存。你删除的账单进入 30 天回收站;AI 批次图片在批次完成或失败回退后立即删除,最近 7 天仅保留不含图片的批次操作摘要用于核对与恢复。手动截屏识别文件在完成、取消或失败后清理,遗留文件会在超过 24 小时后清理。账号注销进入 15 天后悔期,到期后永久删除账号关联数据。导出文件由你主动分享,应用会在分享完成或失败后清理临时副本。',
),
_LegalSection(
'五、你的权利',
@@ -138,7 +138,7 @@ class LegalDocumentPage extends StatelessWidget {
),
_LegalSection(
'无障碍服务',
'可选权限。用于快捷磁贴静默截屏,以及在你主动开启“无障碍事件识别”后,仅处理微信、支付宝的支付流程事件、当前页面可见文字和按需本地截图 OCR。不会监听或拦截音量键,不会保存完整控件树或本地 OCR 截图;关闭后基础手工记账仍可使用。',
'可选权限。用于快捷磁贴静默截屏,以及在你主动开启“无障碍事件识别”后,仅处理微信、支付宝的支付流程事件、当前页面可见文字和按需本地截图 OCR。不会监听或拦截音量键,不会保存完整控件树;未开启 AI 批次对账时,本地 OCR 截图只在内存中处理。开启 AI 批次对账后,支付结果页会临时加密保存并按批上传,批次结束立即删除。关闭后基础手工记账仍可使用。',
),
_LegalSection(
'屏幕录制 / 截屏授权',
@@ -176,7 +176,7 @@ class LegalDocumentPage extends StatelessWidget {
),
_LegalSection(
'火山方舟大模型服务(字节跳动)',
'用于 AI 对话、账单文本解析、图片识别及预算草稿调整。会处理完成对应请求所需的文本、图片和最小化财务上下文,不接收密码、JWT 或 API Key。',
'用于 AI 对话、账单文本解析、图片识别、无障碍支付结果批次对账及预算草稿调整。会处理完成对应请求所需的文本、图片和最小化财务上下文,不接收密码、JWT 或 API Key。批次对账仅处理当前批次,图片在处理结束后由应用删除。',
),
_LegalSection(
'Android / iOS 系统能力',
@@ -0,0 +1,316 @@
import 'package:flutter/material.dart';
import 'package:miaoji_zhang/shared/api/api_client.dart';
import 'package:miaoji_zhang/shared/services/recognition_import_service.dart';
import 'package:miaoji_zhang/shared/services/screenshot_channel.dart';
import 'package:miaoji_zhang/shared/services/shanghai_time.dart';
import 'package:miaoji_zhang/shared/theme/app_theme.dart';
import 'package:miaoji_zhang/shared/widgets/app_controls.dart';
class RecognitionBatchPage extends StatefulWidget {
final String? initialBatchId;
const RecognitionBatchPage({super.key, this.initialBatchId});
@override
State<RecognitionBatchPage> createState() => _RecognitionBatchPageState();
}
class _RecognitionBatchPageState extends State<RecognitionBatchPage> {
List<RecognitionBatch> _batches = const [];
bool _loading = true;
String? _error;
String? _restoringId;
String? _confirmingId;
@override
void initState() {
super.initState();
_load();
}
Future<void> _load() async {
try {
final batches = await ScreenshotChannel.listRecognitionBatches();
if (!mounted) return;
setState(() {
_batches = batches;
_loading = false;
_error = null;
});
} catch (error) {
if (!mounted) return;
setState(() {
_loading = false;
_error = apiErrorMessage(error);
});
}
}
Future<void> _restore(RecognitionBatchItem item) async {
setState(() => _restoringId = item.candidateId);
try {
final restored = await ScreenshotChannel.restoreDroppedRecognition(
item.candidateId,
);
if (!restored) throw StateError('这条候选已恢复或已过期');
await RecognitionImportService.importAutomatic();
await _load();
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('候选已恢复并入账')));
}
} catch (error) {
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text(apiErrorMessage(error))));
}
} finally {
if (mounted) setState(() => _restoringId = null);
}
}
Future<void> _confirm(RecognitionBatchItem item) async {
setState(() => _confirmingId = item.candidateId);
try {
await RecognitionImportService.handleAction(context, {
'action': 'recognition_confirm',
'candidateId': item.candidateId,
});
await _load();
} finally {
if (mounted) setState(() => _confirmingId = null);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('最近 AI 对账')),
body: RefreshIndicator(onRefresh: _load, child: _body()),
);
}
Widget _body() {
if (_loading) {
return const Center(child: CircularProgressIndicator());
}
if (_error != null) {
return ListView(
children: [
SizedBox(height: MediaQuery.sizeOf(context).height * 0.28),
Center(
child: Text(_error!, style: TextStyle(color: context.jz.text2)),
),
const SizedBox(height: 12),
Center(
child: JzActionButton(
label: '重试',
secondary: true,
onPressed: _load,
),
),
],
);
}
if (_batches.isEmpty) {
return ListView(
children: [
SizedBox(height: MediaQuery.sizeOf(context).height * 0.3),
Icon(Icons.fact_check_outlined, size: 42, color: context.jz.text3),
const SizedBox(height: 12),
Center(
child: Text(
'暂无 AI 对账记录',
style: TextStyle(color: context.jz.text3),
),
),
],
);
}
return ListView.separated(
padding: const EdgeInsets.fromLTRB(16, 8, 16, 28),
itemCount: _batches.length,
separatorBuilder: (_, _) => const SizedBox(height: 10),
itemBuilder: (_, index) => _batchCard(_batches[index]),
);
}
Widget _batchCard(RecognitionBatch batch) {
final count = batch.kept + batch.updated + batch.created + batch.dropped;
return Card(
clipBehavior: Clip.antiAlias,
child: ExpansionTile(
initiallyExpanded: batch.id == widget.initialBatchId,
leading: Icon(
batch.fallback
? Icons.offline_bolt_outlined
: Icons.auto_fix_high_rounded,
color: batch.fallback ? AppTheme.orange : AppTheme.primary,
),
title: Text(
'${ShanghaiTime.formatDateTime(batch.completedAt)} · $count 条候选',
style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w700),
),
subtitle: Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(
batch.fallback
? 'AI 不可用,已按本地识别结果处理'
: '保留 ${batch.kept} · 修正 ${batch.updated} · 补全 ${batch.created} · 剔除 ${batch.dropped}',
style: TextStyle(fontSize: 11.5, color: context.jz.text2),
),
),
children: [
Divider(height: 1, color: context.jz.line),
for (var index = 0; index < batch.items.length; index++) ...[
_batchItem(batch.items[index]),
if (index != batch.items.length - 1)
Divider(height: 1, indent: 54, color: context.jz.line),
],
if (batch.items.isEmpty)
Padding(
padding: const EdgeInsets.all(16),
child: Text(
'本批次没有候选明细',
style: TextStyle(color: context.jz.text3),
),
),
],
),
);
}
Widget _batchItem(RecognitionBatchItem item) {
final action = _actionDisplay(item.action);
final restoring = _restoringId == item.candidateId;
final confirming = _confirmingId == item.candidateId;
return Padding(
padding: const EdgeInsets.fromLTRB(16, 13, 12, 13),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 30,
height: 30,
child: Icon(action.icon, size: 19, color: action.color),
),
const SizedBox(width: 8),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
item.merchant?.trim().isNotEmpty == true
? item.merchant!.trim()
: '智能识别账单',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
fontSize: 13,
fontWeight: FontWeight.w700,
),
),
),
const SizedBox(width: 8),
Text(
'${item.type == 'income' ? '+' : '-'}¥${item.amount.toStringAsFixed(2)}',
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w700,
color: item.type == 'income'
? AppTheme.primary
: AppTheme.red,
),
),
],
),
const SizedBox(height: 4),
Text(
'${action.label}${item.reason.isEmpty ? '' : ' · ${item.reason}'}',
style: TextStyle(
fontSize: 11.5,
height: 1.45,
color: context.jz.text2,
),
),
],
),
),
if (item.canRestore) ...[
const SizedBox(width: 8),
IconButton(
tooltip: '恢复并入账',
onPressed: restoring ? null : () => _restore(item),
icon: restoring
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.restore_rounded),
),
] else if (item.state == 'pending_confirm') ...[
const SizedBox(width: 8),
IconButton(
tooltip: '确认入账',
onPressed: confirming ? null : () => _confirm(item),
icon: confirming
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.check_rounded),
),
],
],
),
);
}
_ActionDisplay _actionDisplay(String action) => switch (action) {
'update' => const _ActionDisplay(
'AI 已修正',
Icons.edit_note_rounded,
AppTheme.orange,
),
'create' => const _ActionDisplay(
'AI 已补全',
Icons.add_circle_outline_rounded,
AppTheme.primary,
),
'drop' => const _ActionDisplay(
'AI 已剔除',
Icons.remove_circle_outline_rounded,
AppTheme.red,
),
'restored' => const _ActionDisplay(
'已手动恢复',
Icons.restore_rounded,
AppTheme.primary,
),
'fallback' => const _ActionDisplay(
'本地结果',
Icons.offline_bolt_outlined,
AppTheme.orange,
),
_ => const _ActionDisplay(
'AI 已保留',
Icons.check_circle_outline_rounded,
AppTheme.primary,
),
};
}
class _ActionDisplay {
final String label;
final IconData icon;
final Color color;
const _ActionDisplay(this.label, this.icon, this.color);
}
@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:miaoji_zhang/shared/services/screenshot_channel.dart';
import 'package:miaoji_zhang/shared/services/session_store.dart';
@@ -147,6 +148,9 @@ class _ScreenshotSettingsPageState extends State<ScreenshotSettingsPage>
!await _confirmLocalOcrConsent()) {
return;
}
if (enabled && key == 'ai_screenshot' && !await _confirmAiBatchConsent()) {
return;
}
if (!enabled) {
_pendingAuthorizationKey = null;
final changed = await ScreenshotChannel.setRecognitionToggle(key, false);
@@ -275,6 +279,71 @@ class _ScreenshotSettingsPageState extends State<ScreenshotSettingsPage>
return false;
}
Future<bool> _confirmAiBatchConsent() async {
final preferences = await SharedPreferences.getInstance();
if (preferences.getBool('ai_batch_consent_v2') == true) return true;
if (!mounted) return false;
final accepted = await showModalBottomSheet<bool>(
context: context,
useSafeArea: true,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (sheetContext) => Container(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
decoration: BoxDecoration(
color: sheetContext.jz.card,
borderRadius: const BorderRadius.vertical(top: Radius.circular(24)),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const JzSheetHeader(
title: '启用 AI 批次对账',
subtitle: '请确认支付页截图的批量处理方式',
),
const SizedBox(height: 12),
const _ConsentPoint(
icon: Icons.schedule_rounded,
text: '支付结果会按 30 秒空闲窗口归为一批,最长等待 2 分钟或累计 10 条。',
),
const _ConsentPoint(
icon: Icons.auto_fix_high_rounded,
text: '本批截图和本地候选会发送给 AI,仅允许在当前批次内保留、修正、补全或剔除。',
),
const _ConsentPoint(
icon: Icons.enhanced_encryption_outlined,
text: '待提交图片仅在本机临时加密保存,批次完成或回退后立即删除,不进入最近对账记录。',
),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: JzActionButton(
label: '暂不开启',
secondary: true,
onPressed: () => Navigator.pop(sheetContext, false),
),
),
const SizedBox(width: 10),
Expanded(
child: JzActionButton(
label: '同意并开启',
onPressed: () => Navigator.pop(sheetContext, true),
),
),
],
),
],
),
),
);
if (accepted == true) {
await preferences.setBool('ai_batch_consent_v2', true);
return true;
}
return false;
}
void _showMessage(String message) {
ScaffoldMessenger.of(
context,
@@ -413,24 +482,46 @@ class _ScreenshotSettingsPageState extends State<ScreenshotSettingsPage>
_RecognitionCard(
icon: Icons.document_scanner_outlined,
title: 'AI 截图补全',
description: '本地 OCR 已确认支付成功但字段仍不足时才在线分析。截图完成、失败或超时后立即释放',
description: '支付结果按 30 秒空闲窗口批量提交,AI 只在本批次内纠错、补全或剔除。处理结束立即删除图片',
authorized: aiAvailable,
connected: aiAvailable && status.accessibilityConnected,
statusLabel: !aiAvailable ? 'AI 不可用' : null,
onOpenSettings: status.accessibilityAuthorized
? null
: ScreenshotChannel.openAccessibilitySettings,
child: JzSwitchTile(
value: status.aiScreenshot,
title: '补全开关',
subtitle: !aiAvailable
? '需要登录且账号具备 AI 权限'
: !status.accessibilityAuthorized
? '需要先授权无障碍截屏能力'
: '默认关闭,仅在支付应用前台运行',
onChanged: !aiAvailable
? null
: (value) => _toggle('ai_screenshot', value),
child: Column(
children: [
JzSwitchTile(
value: status.aiScreenshot,
title: '批次对账开关',
subtitle: !aiAvailable
? '需要登录且账号具备 AI 权限'
: !status.accessibilityAuthorized
? '需要先授权无障碍截屏能力'
: '默认关闭,仅在支付应用前台运行',
onChanged: !aiAvailable
? null
: (value) => _toggle('ai_screenshot', value),
),
Divider(height: 1, color: context.jz.line),
InkWell(
onTap: () => context.push('/recognition-batches'),
child: const Padding(
padding: EdgeInsets.symmetric(
horizontal: 4,
vertical: 13,
),
child: Row(
children: [
Icon(Icons.fact_check_outlined, size: 20),
SizedBox(width: 10),
Expanded(child: Text('最近 AI 对账')),
Icon(Icons.chevron_right_rounded),
],
),
),
),
],
),
),
if (status.latestDiagnostic != null) ...[
@@ -487,7 +578,7 @@ class _ScreenshotSettingsPageState extends State<ScreenshotSettingsPage>
const SizedBox(width: 9),
Expanded(
child: Text(
'银行密码页等安全窗口由系统禁止截屏,记之不会绕过限制。本地 OCR 图片仅在内存中处理;只有你开启 AI 截图补全时才会上传当前支付页',
'银行密码页等安全窗口由系统禁止截屏,记之不会绕过限制。本地 OCR 图片仅在内存中处理;开启 AI 批次对账后,支付结果页会临时加密并按批上传,处理结束立即删除',
style: TextStyle(
color: palette.text2,
fontSize: 11.5,