fix(client): 统一打印错误处理,失败时上报并弹提示
新增 safePrint 封装,所有打印调用点统一 catch 任意异常(含字体加载失败), 自动上报 error_reporter 并展示 SnackBar,不再静默失败。 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -43,6 +43,16 @@ cd client && flutter analyze --no-fatal-infos --no-fatal-warnings
|
|||||||
|
|
||||||
只保留有内容的分类(新功能/改进/修复),根据 commit message 的类型(feat/fix/refactor 等)归类。
|
只保留有内容的分类(新功能/改进/修复),根据 commit message 的类型(feat/fix/refactor 等)归类。
|
||||||
|
|
||||||
|
**CHANGELOG 撰写原则:**
|
||||||
|
- 站在**用户视角**总结,描述对使用体验的实际影响,而非技术实现
|
||||||
|
- **忽略**以下类型的提交(产品感知弱):
|
||||||
|
- 发版工具、CI/CD、部署脚本相关(chore: release、ci:、devops:)
|
||||||
|
- 内部文档、开发规范、CLAUDE.md 更新(docs(claude):、docs(internal):)
|
||||||
|
- 代码格式、lint、重构(style:、refactor: 纯内部)
|
||||||
|
- 依赖升级、版本号 bump(chore: bump、chore: upgrade)
|
||||||
|
- **保留并重点描述**:feat、fix、perf 类,以及用户能感知到的 refactor(如界面改版、交互优化)
|
||||||
|
- 每条描述用一句话说清楚"用户能做什么"或"修了什么问题",不堆砌技术术语
|
||||||
|
|
||||||
## 5. 提交代码
|
## 5. 提交代码
|
||||||
|
|
||||||
将所有改动(包括 CHANGELOG.md)用以下格式提交到 main:
|
将所有改动(包括 CHANGELOG.md)用以下格式提交到 main:
|
||||||
|
|||||||
+1
-4
@@ -7,11 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [1.0.2] - 2026-05-25
|
## [1.0.2] - 2026-05-25
|
||||||
|
|
||||||
### 改进
|
|
||||||
- 添加 /release 本地发版 slash command,一键完成构建、测试、打 tag 和推送
|
|
||||||
|
|
||||||
### 修复
|
### 修复
|
||||||
- Web 版打印弹窗被浏览器拦截时,由静默失败改为给出明确提示,并自动上报错误
|
- 打印入库单/出库单/商品标签时,若浏览器拦截了弹窗,现在会给出明确提示,不再静默失败
|
||||||
|
|
||||||
## [1.0.1] - 2026-05-25
|
## [1.0.1] - 2026-05-25
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
import '../../models/stock_in.dart';
|
import '../../models/stock_in.dart';
|
||||||
import '../../models/stock_out.dart';
|
import '../../models/stock_out.dart';
|
||||||
|
import '../errors/error_reporter.dart';
|
||||||
import 'print_util_stub.dart'
|
import 'print_util_stub.dart'
|
||||||
if (dart.library.js_interop) 'print_util_web.dart';
|
if (dart.library.js_interop) 'print_util_web.dart';
|
||||||
|
|
||||||
@@ -36,3 +38,18 @@ Future<void> printStockInOrder(StockInOrder order) =>
|
|||||||
|
|
||||||
Future<void> printStockOutOrder(StockOutOrder order) =>
|
Future<void> printStockOutOrder(StockOutOrder order) =>
|
||||||
printStockOutOrderImpl(order);
|
printStockOutOrderImpl(order);
|
||||||
|
|
||||||
|
/// 统一打印入口:catch 任意异常,上报并弹 SnackBar 给用户。
|
||||||
|
/// 用法:await safePrint(context, () => printStockInOrder(order));
|
||||||
|
Future<void> safePrint(BuildContext context, Future<void> Function() fn) async {
|
||||||
|
try {
|
||||||
|
await fn();
|
||||||
|
} catch (e, st) {
|
||||||
|
reportError(e, st);
|
||||||
|
if (context.mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('打印失败:$e'), backgroundColor: Colors.red),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -190,7 +190,6 @@ Future<void> printProductLabelImpl({
|
|||||||
),
|
),
|
||||||
));
|
));
|
||||||
|
|
||||||
try {
|
|
||||||
await Printing.layoutPdf(onLayout: (_) async => doc.save());
|
await Printing.layoutPdf(onLayout: (_) async => doc.save());
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
reportError(e, st);
|
reportError(e, st);
|
||||||
@@ -430,7 +429,6 @@ Future<void> printStockInOrderImpl(StockInOrder order) async {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
));
|
));
|
||||||
try {
|
|
||||||
await Printing.layoutPdf(onLayout: (_) async => doc.save());
|
await Printing.layoutPdf(onLayout: (_) async => doc.save());
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
reportError(e, st);
|
reportError(e, st);
|
||||||
|
|||||||
@@ -485,12 +485,12 @@ class _InventoryListScreenState extends ConsumerState<InventoryListScreen> {
|
|||||||
item.productId != null
|
item.productId != null
|
||||||
? TextButton(
|
? TextButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
try {
|
|
||||||
final qrBytes = await ref
|
final qrBytes = await ref
|
||||||
.read(productRepositoryProvider)
|
.read(productRepositoryProvider)
|
||||||
.getQRCodeBytes(item.productId!);
|
.getQRCodeBytes(item.productId!);
|
||||||
final shopInfo = ref.read(shopInfoProvider).valueOrNull;
|
final shopInfo = ref.read(shopInfoProvider).valueOrNull;
|
||||||
await printProductLabel(
|
if (context.mounted) {
|
||||||
|
await safePrint(context, () => printProductLabel(
|
||||||
qrBytes: qrBytes,
|
qrBytes: qrBytes,
|
||||||
name: item.productName,
|
name: item.productName,
|
||||||
code: item.productCode,
|
code: item.productCode,
|
||||||
@@ -502,14 +502,7 @@ class _InventoryListScreenState extends ConsumerState<InventoryListScreen> {
|
|||||||
shopName: shopInfo?.name ?? '',
|
shopName: shopInfo?.name ?? '',
|
||||||
shopAddress: shopInfo?.address ?? '',
|
shopAddress: shopInfo?.address ?? '',
|
||||||
shopPhone: shopInfo?.phone ?? '',
|
shopPhone: shopInfo?.phone ?? '',
|
||||||
);
|
));
|
||||||
} catch (e) {
|
|
||||||
if (context.mounted) {
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
SnackBar(content: Text('打印失败:$e'),
|
|
||||||
backgroundColor: AppTheme.danger),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: const Text('打标签',
|
child: const Text('打标签',
|
||||||
|
|||||||
@@ -528,10 +528,10 @@ class _QRCodeDialogState extends ConsumerState<_QRCodeDialog> {
|
|||||||
Future<void> _print() async {
|
Future<void> _print() async {
|
||||||
if (_bytes == null || _printing) return;
|
if (_bytes == null || _printing) return;
|
||||||
setState(() { _printing = true; _printStatus = '正在打印...'; });
|
setState(() { _printing = true; _printStatus = '正在打印...'; });
|
||||||
try {
|
|
||||||
final p = widget.product;
|
final p = widget.product;
|
||||||
final shopInfo = ref.read(shopInfoProvider).valueOrNull;
|
final shopInfo = ref.read(shopInfoProvider).valueOrNull;
|
||||||
await printProductLabel(
|
try {
|
||||||
|
await safePrint(context, () => printProductLabel(
|
||||||
qrBytes: _bytes!,
|
qrBytes: _bytes!,
|
||||||
name: p.name,
|
name: p.name,
|
||||||
code: p.code,
|
code: p.code,
|
||||||
@@ -540,13 +540,7 @@ class _QRCodeDialogState extends ConsumerState<_QRCodeDialog> {
|
|||||||
shopName: shopInfo?.name ?? '',
|
shopName: shopInfo?.name ?? '',
|
||||||
shopAddress: shopInfo?.address ?? '',
|
shopAddress: shopInfo?.address ?? '',
|
||||||
shopPhone: shopInfo?.phone ?? '',
|
shopPhone: shopInfo?.phone ?? '',
|
||||||
);
|
));
|
||||||
} catch (e) {
|
|
||||||
if (mounted) {
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
SnackBar(content: Text('打印失败:$e'), backgroundColor: Colors.red),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
if (mounted) setState(() { _printing = false; _printStatus = ''; });
|
if (mounted) setState(() { _printing = false; _printStatus = ''; });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ class _StockInFormScreenState extends ConsumerState<StockInFormScreen> {
|
|||||||
|
|
||||||
Future<void> _printOrder() async {
|
Future<void> _printOrder() async {
|
||||||
if (_loadedOrder == null) return;
|
if (_loadedOrder == null) return;
|
||||||
await printStockInOrder(_loadedOrder!);
|
await safePrint(context, () => printStockInOrder(_loadedOrder!));
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _submit(bool asDraft) async {
|
Future<void> _submit(bool asDraft) async {
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import '../../core/utils/dialog_util.dart';
|
import '../../core/utils/dialog_util.dart';
|
||||||
|
import '../../core/errors/error_reporter.dart';
|
||||||
|
import '../../core/utils/print_util.dart' show safePrint, printStockInOrder, printProductLabel;
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
@@ -247,15 +249,9 @@ class _StockInListScreenState extends ConsumerState<StockInListScreen> {
|
|||||||
),
|
),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
try {
|
|
||||||
final order = await ref.read(stockInRepositoryProvider).get(o.id);
|
final order = await ref.read(stockInRepositoryProvider).get(o.id);
|
||||||
await printStockInOrder(order);
|
|
||||||
} catch (e) {
|
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
await safePrint(context, () => printStockInOrder(order));
|
||||||
SnackBar(content: Text(e.toString()), backgroundColor: Colors.red),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: const Text('打印',
|
child: const Text('打印',
|
||||||
@@ -698,17 +694,7 @@ class _StockInDetailDialogState extends ConsumerState<_StockInDetailDialog> {
|
|||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.print_outlined, color: Colors.white),
|
icon: const Icon(Icons.print_outlined, color: Colors.white),
|
||||||
tooltip: '打印',
|
tooltip: '打印',
|
||||||
onPressed: () async {
|
onPressed: () => safePrint(context, () => printStockInOrder(_loadedOrder!)),
|
||||||
try {
|
|
||||||
await printStockInOrder(_loadedOrder!);
|
|
||||||
} catch (e) {
|
|
||||||
if (context.mounted) {
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
SnackBar(content: Text(e.toString()), backgroundColor: Colors.red),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.close, color: Colors.white),
|
icon: const Icon(Icons.close, color: Colors.white),
|
||||||
@@ -966,7 +952,8 @@ class _LabelPrintDialogState extends State<_LabelPrintDialog> {
|
|||||||
);
|
);
|
||||||
done++;
|
done++;
|
||||||
if (mounted) setState(() => _status = '已打印 $done 张...');
|
if (mounted) setState(() => _status = '已打印 $done 张...');
|
||||||
} catch (e) {
|
} catch (e, st) {
|
||||||
|
reportError(e, st);
|
||||||
if (mounted) setState(() => _status = '第${i + 1}行打印失败:$e');
|
if (mounted) setState(() => _status = '第${i + 1}行打印失败:$e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ class _StockOutFormScreenState extends ConsumerState<StockOutFormScreen> {
|
|||||||
|
|
||||||
Future<void> _printOrder() async {
|
Future<void> _printOrder() async {
|
||||||
if (_loadedOrder == null) return;
|
if (_loadedOrder == null) return;
|
||||||
await printStockOutOrder(_loadedOrder!);
|
await safePrint(context, () => printStockOutOrder(_loadedOrder!));
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _submit(bool asDraft) async {
|
Future<void> _submit(bool asDraft) async {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import '../../repositories/product_repository.dart';
|
import '../../repositories/product_repository.dart';
|
||||||
import '../../core/utils/dialog_util.dart';
|
import '../../core/utils/dialog_util.dart';
|
||||||
|
import '../../core/errors/error_reporter.dart';
|
||||||
|
import '../../core/utils/print_util.dart' show safePrint, printStockOutOrder, printProductLabel;
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
@@ -254,15 +256,9 @@ class _StockOutListScreenState extends ConsumerState<StockOutListScreen> {
|
|||||||
),
|
),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
try {
|
|
||||||
final order = await ref.read(stockOutRepositoryProvider).get(o.id);
|
final order = await ref.read(stockOutRepositoryProvider).get(o.id);
|
||||||
await printStockOutOrder(order);
|
|
||||||
} catch (e) {
|
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
await safePrint(context, () => printStockOutOrder(order));
|
||||||
SnackBar(content: Text(e.toString()), backgroundColor: Colors.red),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: const Text('打印',
|
child: const Text('打印',
|
||||||
@@ -690,17 +686,7 @@ class _StockOutDetailDialogState extends ConsumerState<_StockOutDetailDialog> {
|
|||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.print_outlined, color: Colors.white),
|
icon: const Icon(Icons.print_outlined, color: Colors.white),
|
||||||
tooltip: '打印',
|
tooltip: '打印',
|
||||||
onPressed: () async {
|
onPressed: () => safePrint(context, () => printStockOutOrder(_loadedOrder!)),
|
||||||
try {
|
|
||||||
await printStockOutOrder(_loadedOrder!);
|
|
||||||
} catch (e) {
|
|
||||||
if (context.mounted) {
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
SnackBar(content: Text(e.toString()), backgroundColor: Colors.red),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.close, color: Colors.white),
|
icon: const Icon(Icons.close, color: Colors.white),
|
||||||
@@ -960,10 +946,9 @@ class _LabelPrintDialogState extends State<_LabelPrintDialog> {
|
|||||||
);
|
);
|
||||||
done++;
|
done++;
|
||||||
if (mounted) setState(() => _status = '已打印 $done 张...');
|
if (mounted) setState(() => _status = '已打印 $done 张...');
|
||||||
} catch (e) {
|
} catch (e, st) {
|
||||||
if (mounted) {
|
reportError(e, st);
|
||||||
setState(() => _status = '第${i + 1}行打印失败:$e');
|
if (mounted) setState(() => _status = '第${i + 1}行打印失败:$e');
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
|
|||||||
Reference in New Issue
Block a user