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
+7
View File
@@ -19,6 +19,7 @@ import 'package:miaoji_zhang/features/settings/category_manage_page.dart';
import 'package:miaoji_zhang/features/settings/companion_page.dart';
import 'package:miaoji_zhang/features/settings/me_page.dart';
import 'package:miaoji_zhang/features/settings/recycle_bin_page.dart';
import 'package:miaoji_zhang/features/settings/recognition_batch_page.dart';
import 'package:miaoji_zhang/features/settings/legal_document_page.dart';
import 'package:miaoji_zhang/features/settings/screenshot_settings_page.dart';
import 'package:miaoji_zhang/features/settings/sync_conflicts_page.dart';
@@ -91,6 +92,12 @@ final router = GoRouter(
path: '/screenshot-settings',
builder: (_, __) => const ScreenshotSettingsPage(),
),
GoRoute(
path: '/recognition-batches',
builder: (_, state) => RecognitionBatchPage(
initialBatchId: state.uri.queryParameters['batchId'],
),
),
StatefulShellRoute.indexedStack(
builder: (_, __, shell) => MainShell(shell: shell),
branches: [
@@ -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,
+101
View File
@@ -185,6 +185,27 @@ class PeriodStats {
analysis = json['analysis'] as String?;
}
class RecognitionBatchDraft {
final String candidateId, clientRequestId, type, source;
final int categoryId;
final double amount;
final DateTime occurredAt;
final String? note, paymentMethod, sourceText;
const RecognitionBatchDraft({
required this.candidateId,
required this.clientRequestId,
required this.categoryId,
required this.type,
required this.amount,
required this.occurredAt,
required this.source,
this.note,
this.paymentMethod,
this.sourceText,
});
}
class TxApi {
static final _dio = ApiClient.instance.dio;
@@ -361,6 +382,86 @@ class TxApi {
}
}
static Future<Map<String, TxItem>> createRecognitionBatch(
String batchId,
List<RecognitionBatchDraft> drafts,
) async {
if (drafts.isEmpty) return const {};
final localPayloads = drafts
.map(
(draft) => <String, dynamic>{
'ledgerId': _ledgerId,
'categoryId': draft.categoryId,
'type': draft.type,
'amount': draft.amount,
'note': draft.note,
'paymentMethod': draft.paymentMethod,
'source': draft.source,
'sourceText': draft.sourceText,
'occurredAt': ShanghaiTime.civilToUtc(
draft.occurredAt,
).toIso8601String(),
'clientRequestId': draft.clientRequestId,
},
)
.toList(growable: false);
Map<String, TxItem> createLocal() {
final values = LocalDatabase.instance.createTransactionsBatch(
localPayloads,
enqueueSyncChanges: _queueOfflineChanges,
);
return {
for (var index = 0; index < drafts.length; index++)
drafts[index].candidateId: TxItem.fromJson(values[index]),
};
}
final session = SessionStore.instance;
if (session.shouldUseLocalOnly ||
_ledgerId < 0 ||
drafts.any((draft) => draft.categoryId < 0)) {
return createLocal();
}
try {
final response = await _dio.post(
'/api/transactions/recognition-batch',
data: {
'batchId': batchId,
'ledgerId': _ledgerId,
'items': [
for (var index = 0; index < drafts.length; index++)
{
'candidateId': drafts[index].candidateId,
'clientRequestId': drafts[index].clientRequestId,
'categoryId': drafts[index].categoryId,
'type': drafts[index].type,
'amount': drafts[index].amount,
'note': drafts[index].note,
'paymentMethod': drafts[index].paymentMethod,
'occurredAt': localPayloads[index]['occurredAt'],
'source': drafts[index].source,
'sourceText': drafts[index].sourceText,
},
],
},
);
final result = <String, TxItem>{};
for (final raw in response.data as List<dynamic>) {
final item = Map<String, dynamic>.from(raw as Map);
final transaction = Map<String, dynamic>.from(
item['transaction'] as Map,
);
LocalDatabase.instance.cacheTransaction(transaction);
result[item['candidateId'].toString()] = TxItem.fromJson(transaction);
}
return result;
} catch (error) {
if (!isConnectivityError(error) || !session.isAccount) rethrow;
return createLocal();
}
}
static Future<void> delete(int id) async {
final baseUpdatedAt = LocalDatabase.instance.transaction(
id,
@@ -951,6 +951,37 @@ class LocalDatabase {
return transaction(id, includeDeleted: true)!;
}
List<Map<String, dynamic>> createTransactionsBatch(
List<Map<String, dynamic>> values, {
bool enqueueSyncChanges = false,
}) {
_db.execute('BEGIN');
try {
final created = <Map<String, dynamic>>[];
for (final value in values) {
final clientRequestId = value['clientRequestId'] as String?;
final existed =
clientRequestId != null &&
clientRequestId.isNotEmpty &&
_db.select(
'SELECT 1 FROM transactions WHERE client_request_id = ? LIMIT 1',
[clientRequestId],
).isNotEmpty;
final transaction = createTransaction(value);
created.add(transaction);
final id = (transaction['id'] as num).toInt();
if (enqueueSyncChanges && id < 0 && !existed) {
enqueueSync('transaction', id, 'create', value);
}
}
_db.execute('COMMIT');
return created;
} catch (_) {
_db.execute('ROLLBACK');
rethrow;
}
}
Map<String, dynamic>? transaction(int id, {bool includeDeleted = false}) {
final rows = _db.select(
'''
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:miaoji_zhang/features/home/pages/transaction_edit_page.dart';
import 'package:miaoji_zhang/shared/api/api_client.dart';
import 'package:miaoji_zhang/shared/api/business_api.dart';
@@ -10,14 +11,20 @@ import 'package:miaoji_zhang/shared/services/shanghai_time.dart';
import 'package:miaoji_zhang/shared/services/transaction_events.dart';
import 'package:miaoji_zhang/shared/theme/app_theme.dart';
import 'package:miaoji_zhang/shared/widgets/app_controls.dart';
import 'package:shared_preferences/shared_preferences.dart';
class RecognitionImportService {
RecognitionImportService._();
static bool _processing = false;
static Future<void>? _activeImport;
static bool _rerunRequested = false;
static Future<void> configureNativeContext() async {
final session = SessionStore.instance;
final preferences = await SharedPreferences.getInstance();
if (preferences.getBool('ai_batch_consent_v2') != true) {
await ScreenshotChannel.setRecognitionToggle('ai_screenshot', false);
}
await ScreenshotChannel.configureRecognitionContext(
hasAccount: session.isAccount,
aiAllowed: session.aiEnabled,
@@ -26,17 +33,46 @@ class RecognitionImportService {
);
}
static Future<void> importAutomatic() async {
if (_processing || !SessionStore.instance.hasSession) return;
_processing = true;
static Future<void> importAutomatic() {
if (!SessionStore.instance.hasSession) return Future.value();
final active = _activeImport;
if (active != null) {
_rerunRequested = true;
return active;
}
final operation = _runAutomaticImports();
_activeImport = operation;
return operation.whenComplete(() {
if (identical(_activeImport, operation)) _activeImport = null;
});
}
static Future<void> _runAutomaticImports() async {
try {
await CurrentLedgerStore.instance.ensureLoaded();
final candidates = await ScreenshotChannel.drainRecognitionCandidates();
for (final candidate in candidates.where((item) => item.canAutoImport)) {
await _import(candidate);
}
do {
_rerunRequested = false;
await CurrentLedgerStore.instance.ensureLoaded();
final candidates = await ScreenshotChannel.drainRecognitionCandidates();
final automatic = candidates
.where((item) => item.canAutoImport)
.toList();
final batches = <String, List<RecognitionCandidate>>{};
for (final candidate in automatic) {
final batchId = candidate.batchId;
if (batchId == null || batchId.isEmpty) continue;
batches.putIfAbsent(batchId, () => []).add(candidate);
}
for (final entry in batches.entries) {
await _importBatch(entry.key, entry.value);
}
for (final candidate in automatic.where(
(item) => item.batchId == null || item.batchId!.isEmpty,
)) {
await _import(candidate);
}
} while (_rerunRequested);
} finally {
_processing = false;
_rerunRequested = false;
}
}
@@ -49,6 +85,20 @@ class RecognitionImportService {
await importAutomatic();
return;
}
if (kind == 'recognition_batch_review') {
await importAutomatic();
if (!context.mounted) return;
final batchId = action['batchId']?.toString();
context.push(
Uri(
path: '/recognition-batches',
queryParameters: batchId == null || batchId.isEmpty
? null
: {'batchId': batchId},
).toString(),
);
return;
}
if (kind == 'recognition_undo') {
final transactionId = (action['transactionId'] as num?)?.toInt();
final candidateId = action['candidateId']?.toString();
@@ -130,17 +180,7 @@ class RecognitionImportService {
}
static Future<TxItem> _import(RecognitionCandidate candidate) async {
if (candidate.type != 'income' && candidate.type != 'expense') {
throw StateError('识别结果缺少明确的收支类型');
}
final categories = await TxApi.categories(candidate.type);
if (categories.isEmpty) throw StateError('当前账本没有可用分类');
final category =
categories
.where((item) => item.name == candidate.categoryHint)
.firstOrNull ??
categories.where((item) => item.name == '其他').firstOrNull ??
categories.first;
final category = await _resolveCategory(candidate, {});
final occurredUtc = validOccurredAtUtc(candidate.occurredAtEpochMs);
final transaction = await TxApi.create(
categoryId: category.id,
@@ -164,6 +204,73 @@ class RecognitionImportService {
return transaction;
}
static Future<void> _importBatch(
String batchId,
List<RecognitionCandidate> candidates,
) async {
final categoryCache = <String, List<CategoryItem>>{};
final drafts = <RecognitionBatchDraft>[];
for (final candidate in candidates) {
final category = await _resolveCategory(candidate, categoryCache);
final occurredUtc = validOccurredAtUtc(candidate.occurredAtEpochMs);
drafts.add(
RecognitionBatchDraft(
candidateId: candidate.id,
clientRequestId: candidate.clientRequestId,
categoryId: category.id,
type: candidate.type,
amount: candidate.amount,
note: candidate.merchant?.trim().isNotEmpty == true
? candidate.merchant!.trim()
: (candidate.note ?? '智能识别'),
paymentMethod: candidate.appName,
source: candidate.source,
sourceText: candidate.sourceText,
occurredAt: ShanghaiTime.toCivil(occurredUtc),
),
);
}
final transactions = await TxApi.createRecognitionBatch(batchId, drafts);
if (transactions.length != drafts.length) {
throw StateError('批次入账结果不完整,请稍后重试');
}
for (final candidate in candidates) {
final transaction = transactions[candidate.id];
if (transaction == null) {
throw StateError('批次入账缺少候选 ${candidate.id}');
}
final acknowledged =
await ScreenshotChannel.acknowledgeRecognitionCandidate(
candidate.id,
'imported',
transactionId: transaction.id,
);
if (!acknowledged) throw StateError('批次状态确认失败,请稍后重试');
}
TransactionEvents.notifyChanged();
}
static Future<CategoryItem> _resolveCategory(
RecognitionCandidate candidate,
Map<String, List<CategoryItem>> cache,
) async {
if (candidate.type != 'income' && candidate.type != 'expense') {
throw StateError('识别结果缺少明确的收支类型');
}
final categories = cache[candidate.type] ??= await TxApi.categories(
candidate.type,
);
if (categories.isEmpty) throw StateError('当前账本没有可用分类');
return categories
.where((item) => item.id == candidate.categoryId)
.firstOrNull ??
categories
.where((item) => item.name == candidate.categoryHint)
.firstOrNull ??
categories.where((item) => item.name == '其他').firstOrNull ??
categories.first;
}
static DateTime validOccurredAtUtc(int epochMs, {DateTime? now}) {
final current = (now ?? DateTime.now()).toUtc();
final parsed = DateTime.fromMillisecondsSinceEpoch(epochMs, isUtc: true);
@@ -138,6 +138,8 @@ class RecognitionCandidate {
final String? merchant, orderId, sourceText, note;
final String recognitionKind, amountSource;
final String? categoryHint, resultFingerprint;
final String? batchId, aiAction, aiReason;
final int? categoryId;
final int occurredAtEpochMs;
RecognitionCandidate.fromJson(Map<String, dynamic> value)
@@ -155,13 +157,68 @@ class RecognitionCandidate {
note = value['note'] as String?,
recognitionKind = value['recognitionKind']?.toString() ?? 'payment',
categoryHint = value['categoryHint']?.toString(),
categoryId = (value['categoryId'] as num?)?.toInt(),
amountSource = value['amountSource']?.toString() ?? 'result',
resultFingerprint = value['resultFingerprint']?.toString(),
batchId = value['batchId']?.toString(),
aiAction = value['aiAction']?.toString(),
aiReason = value['aiReason']?.toString(),
occurredAtEpochMs = (value['occurredAtEpochMs'] as num).toInt();
bool get canAutoImport => state == 'auto_ready' && confidence == 'auto';
}
class RecognitionBatchItem {
final String candidateId, action, reason, state, type;
final double amount;
final String? merchant;
final bool canRestore;
RecognitionBatchItem.fromJson(Map<String, dynamic> value)
: candidateId = value['candidateId']?.toString() ?? '',
action = value['action']?.toString() ?? 'keep',
reason = value['reason']?.toString() ?? '',
state = value['state']?.toString() ?? '',
type = value['type']?.toString() ?? 'expense',
amount = (value['amount'] as num?)?.toDouble() ?? 0,
merchant = value['merchant']?.toString(),
canRestore = value['canRestore'] as bool? ?? false;
}
class RecognitionBatch {
final String id, state;
final DateTime openedAt, completedAt;
final int kept, updated, created, dropped;
final bool fallback;
final String? failureReason;
final List<RecognitionBatchItem> items;
RecognitionBatch.fromJson(Map<String, dynamic> value)
: id = value['id']?.toString() ?? '',
state = value['state']?.toString() ?? '',
openedAt = DateTime.fromMillisecondsSinceEpoch(
(value['openedAt'] as num?)?.toInt() ?? 0,
isUtc: true,
),
completedAt = DateTime.fromMillisecondsSinceEpoch(
(value['completedAt'] as num?)?.toInt() ?? 0,
isUtc: true,
),
kept = ((value['summary'] as Map?)?['kept'] as num?)?.toInt() ?? 0,
updated = ((value['summary'] as Map?)?['updated'] as num?)?.toInt() ?? 0,
created = ((value['summary'] as Map?)?['created'] as num?)?.toInt() ?? 0,
dropped = ((value['summary'] as Map?)?['dropped'] as num?)?.toInt() ?? 0,
fallback = ((value['summary'] as Map?)?['fallback'] as bool?) ?? false,
failureReason = value['failureReason']?.toString(),
items = (value['items'] as List<dynamic>? ?? const [])
.map(
(item) => RecognitionBatchItem.fromJson(
Map<String, dynamic>.from(item as Map),
),
)
.toList(growable: false);
}
class SpeechEvent {
final String type;
final String? text;
@@ -398,6 +455,37 @@ class ScreenshotChannel {
}
}
static Future<List<RecognitionBatch>> listRecognitionBatches() async {
try {
final values =
await _channel.invokeMethod<List<Object?>>(
'listRecognitionBatches',
) ??
const [];
return values
.whereType<String>()
.map(
(value) => RecognitionBatch.fromJson(
jsonDecode(value) as Map<String, dynamic>,
),
)
.toList(growable: false);
} on MissingPluginException {
return const [];
}
}
static Future<bool> restoreDroppedRecognition(String candidateId) async {
try {
return await _channel.invokeMethod<bool>('restoreDroppedRecognition', {
'candidateId': candidateId,
}) ??
false;
} on MissingPluginException {
return false;
}
}
static Future<bool> requestNotificationPermission() async {
try {
return await _channel.invokeMethod<bool>(