bb4f17cf7a
后端: - 新增 GET /version 版本检查端点(version.go + version.yaml) - 新增 GET /license/info 接口,返回门店授权信息 - 修复 GenerateOrderNo 并发重复单号:事务内加 FOR UPDATE 行锁 - 修复 ApproveStockOut 超卖竞态:预检和库存更新均加 FOR UPDATE - 修复 Product Create 并发 code 冲突:加重试逻辑,schema 加 UNIQUE KEY - 修复 Product Update 全字段覆盖:改用 selective Updates() - 挂载 ReadOnly 中间件(全局)+ AdminOnly(用户管理路由) - version.go 配置缺失时返回 500 而非静默降级 前端: - 新增自动更新检测(update_provider.dart)+ shell 更新 banner/弹窗 - 新增系统设置"关于"标签页:版本、授权、开发信息、意见反馈 - 新增离线缓存:所有 AsyncNotifierProvider 支持断网浏览历史数据 - 新增门店信息弹窗(点击左上角 logo 或右上角门店号触发) - 提取 AppConfig 统一管理 BASE_URL,支持 --dart-define 注入 - update_provider.dart 加 kIsWeb 保护,修复 Web 平台崩溃 - dev.sh 新增 stop 命令,修复 stop 误杀前端进程问题 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
483 lines
16 KiB
Dart
483 lines
16 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../../core/models/page_result.dart';
|
|
import '../../core/theme/app_theme.dart';
|
|
import '../../models/partner.dart';
|
|
import '../../providers/partner_provider.dart';
|
|
import '../../widgets/data_table_card.dart';
|
|
import '../../widgets/page_scaffold.dart';
|
|
|
|
class PartnersScreen extends ConsumerStatefulWidget {
|
|
const PartnersScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<PartnersScreen> createState() => _PartnersScreenState();
|
|
}
|
|
|
|
class _PartnersScreenState extends ConsumerState<PartnersScreen> {
|
|
final _supplierSearchCtrl = TextEditingController();
|
|
final _customerSearchCtrl = TextEditingController();
|
|
Timer? _supplierDebounce;
|
|
Timer? _customerDebounce;
|
|
|
|
@override
|
|
void dispose() {
|
|
_supplierSearchCtrl.dispose();
|
|
_customerSearchCtrl.dispose();
|
|
_supplierDebounce?.cancel();
|
|
_customerDebounce?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return PageScaffold(
|
|
title: '往来单位',
|
|
tabs: const [
|
|
Tab(text: '供应商'),
|
|
Tab(text: '客户'),
|
|
],
|
|
tabViews: [
|
|
_buildSupplierTab(),
|
|
_buildCustomerTab(),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildSupplierTab() {
|
|
final asyncPartners = ref.watch(supplierListProvider);
|
|
return asyncPartners.when(
|
|
loading: () => const Center(child: CircularProgressIndicator()),
|
|
error: (e, _) => Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Icon(Icons.cloud_off, size: 40, color: AppTheme.textSecondary),
|
|
const SizedBox(height: 12),
|
|
const Text('暂无数据,网络不可用',
|
|
style: const TextStyle(color: AppTheme.textSecondary)),
|
|
const SizedBox(height: 12),
|
|
ElevatedButton(
|
|
onPressed: () =>
|
|
ref.read(supplierListProvider.notifier).reload(),
|
|
child: const Text('重试'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
data: (result) => _buildPartnerList(
|
|
result,
|
|
isSupplier: true,
|
|
searchCtrl: _supplierSearchCtrl,
|
|
onSearchChanged: (v) {
|
|
_supplierDebounce?.cancel();
|
|
_supplierDebounce = Timer(const Duration(milliseconds: 300), () {
|
|
ref.read(supplierListProvider.notifier).setKeyword(v);
|
|
});
|
|
},
|
|
onPageChanged: (p) =>
|
|
ref.read(supplierListProvider.notifier).setPage(p),
|
|
onAdd: () => _showPartnerDialog(context, isSupplier: true),
|
|
onEdit: (p) =>
|
|
_showPartnerDialog(context, isSupplier: true, partner: p),
|
|
onDelete: (p) => _confirmDelete(context, p, isSupplier: true),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildCustomerTab() {
|
|
final asyncPartners = ref.watch(customerListProvider);
|
|
return asyncPartners.when(
|
|
loading: () => const Center(child: CircularProgressIndicator()),
|
|
error: (e, _) => Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Icon(Icons.cloud_off, size: 40, color: AppTheme.textSecondary),
|
|
const SizedBox(height: 12),
|
|
const Text('暂无数据,网络不可用',
|
|
style: const TextStyle(color: AppTheme.textSecondary)),
|
|
const SizedBox(height: 12),
|
|
ElevatedButton(
|
|
onPressed: () =>
|
|
ref.read(customerListProvider.notifier).reload(),
|
|
child: const Text('重试'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
data: (result) => _buildPartnerList(
|
|
result,
|
|
isSupplier: false,
|
|
searchCtrl: _customerSearchCtrl,
|
|
onSearchChanged: (v) {
|
|
_customerDebounce?.cancel();
|
|
_customerDebounce = Timer(const Duration(milliseconds: 300), () {
|
|
ref.read(customerListProvider.notifier).setKeyword(v);
|
|
});
|
|
},
|
|
onPageChanged: (p) =>
|
|
ref.read(customerListProvider.notifier).setPage(p),
|
|
onAdd: () => _showPartnerDialog(context, isSupplier: false),
|
|
onEdit: (p) =>
|
|
_showPartnerDialog(context, isSupplier: false, partner: p),
|
|
onDelete: (p) => _confirmDelete(context, p, isSupplier: false),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildPartnerList(
|
|
PageResult<Partner> result, {
|
|
required bool isSupplier,
|
|
required TextEditingController searchCtrl,
|
|
required void Function(String) onSearchChanged,
|
|
required void Function(int) onPageChanged,
|
|
required VoidCallback onAdd,
|
|
required void Function(Partner) onEdit,
|
|
required void Function(Partner) onDelete,
|
|
}) {
|
|
final partners = result.data;
|
|
return DataTableCard(
|
|
totalCount: result.total,
|
|
page: result.page,
|
|
onPageChanged: onPageChanged,
|
|
toolbar: Row(
|
|
children: [
|
|
ElevatedButton.icon(
|
|
onPressed: onAdd,
|
|
icon: const Icon(Icons.add, size: 16),
|
|
label: Text(isSupplier ? '新建' : '新建'),
|
|
),
|
|
const Spacer(),
|
|
SizedBox(
|
|
width: 200,
|
|
child: TextField(
|
|
controller: searchCtrl,
|
|
decoration: InputDecoration(
|
|
hintText: isSupplier ? '搜索供应商名称/编码' : '搜索客户名称/编码',
|
|
prefixIcon: const Icon(Icons.search, size: 16),
|
|
hintStyle: const TextStyle(fontSize: 13),
|
|
),
|
|
onChanged: onSearchChanged,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
columns: [
|
|
const DataColumn(label: Text('编码')),
|
|
const DataColumn(label: Text('名称')),
|
|
const DataColumn(label: Text('联系人')),
|
|
const DataColumn(label: Text('联系电话')),
|
|
const DataColumn(label: Text('地址')),
|
|
const DataColumn(label: Text('操作')),
|
|
],
|
|
rows: partners.isEmpty
|
|
? [
|
|
DataRow(cells: [
|
|
const DataCell(SizedBox()),
|
|
DataCell(Text(isSupplier ? '暂无供应商' : '暂无客户',
|
|
style:
|
|
const TextStyle(color: AppTheme.textSecondary))),
|
|
const DataCell(SizedBox()),
|
|
const DataCell(SizedBox()),
|
|
const DataCell(SizedBox()),
|
|
const DataCell(SizedBox()),
|
|
])
|
|
]
|
|
: partners
|
|
.map((p) => DataRow(
|
|
cells: [
|
|
DataCell(Text(p.code ?? '-',
|
|
style: const TextStyle(
|
|
fontFamily: 'monospace',
|
|
fontSize: 12,
|
|
color: AppTheme.textSecondary))),
|
|
DataCell(SizedBox(
|
|
width: 180,
|
|
child: Text(p.name,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.w500)),
|
|
)),
|
|
DataCell(Text(p.contact ?? '-')),
|
|
DataCell(Text(p.phone ?? '-')),
|
|
DataCell(SizedBox(
|
|
width: 160,
|
|
child: Text(p.address ?? '-',
|
|
overflow: TextOverflow.ellipsis),
|
|
)),
|
|
DataCell(Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextButton(
|
|
key: Key('btn_edit_${p.id}'),
|
|
onPressed: () => onEdit(p),
|
|
child: const Text('编辑',
|
|
style: TextStyle(fontSize: 12)),
|
|
),
|
|
TextButton(
|
|
key: Key('btn_delete_${p.id}'),
|
|
onPressed: () => onDelete(p),
|
|
child: const Text('删除',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: AppTheme.danger)),
|
|
),
|
|
],
|
|
)),
|
|
],
|
|
))
|
|
.toList(),
|
|
);
|
|
}
|
|
|
|
void _showPartnerDialog(BuildContext context,
|
|
{required bool isSupplier, Partner? partner}) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) => _PartnerFormDialog(
|
|
isSupplier: isSupplier,
|
|
partner: partner,
|
|
onSaved: () {
|
|
if (isSupplier) {
|
|
ref.read(supplierListProvider.notifier).reload();
|
|
} else {
|
|
ref.read(customerListProvider.notifier).reload();
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _confirmDelete(BuildContext context, Partner partner,
|
|
{required bool isSupplier}) async {
|
|
final confirmed = await showDialog<bool>(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text('确认删除'),
|
|
content: Text('确认删除「${partner.name}」?此操作不可恢复。'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(ctx).pop(false),
|
|
child: const Text('取消'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () => Navigator.of(ctx).pop(true),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: AppTheme.danger,
|
|
foregroundColor: Colors.white),
|
|
child: const Text('删除'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (confirmed == true && mounted) {
|
|
try {
|
|
if (isSupplier) {
|
|
await ref
|
|
.read(supplierListProvider.notifier)
|
|
.deletePartner(partner.id);
|
|
} else {
|
|
await ref
|
|
.read(customerListProvider.notifier)
|
|
.deletePartner(partner.id);
|
|
}
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
|
|
content: Text('删除成功'),
|
|
backgroundColor: AppTheme.success));
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text('删除失败:$e'),
|
|
backgroundColor: AppTheme.danger));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class _PartnerFormDialog extends ConsumerStatefulWidget {
|
|
final bool isSupplier;
|
|
final Partner? partner;
|
|
final VoidCallback onSaved;
|
|
|
|
const _PartnerFormDialog({
|
|
required this.isSupplier,
|
|
this.partner,
|
|
required this.onSaved,
|
|
});
|
|
|
|
@override
|
|
ConsumerState<_PartnerFormDialog> createState() =>
|
|
_PartnerFormDialogState();
|
|
}
|
|
|
|
class _PartnerFormDialogState extends ConsumerState<_PartnerFormDialog> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
late final TextEditingController _nameCtrl;
|
|
late final TextEditingController _codeCtrl;
|
|
late final TextEditingController _contactCtrl;
|
|
late final TextEditingController _phoneCtrl;
|
|
late final TextEditingController _addressCtrl;
|
|
late final TextEditingController _remarkCtrl;
|
|
bool _saving = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final p = widget.partner;
|
|
_nameCtrl = TextEditingController(text: p?.name ?? '');
|
|
_codeCtrl = TextEditingController(text: p?.code ?? '');
|
|
_contactCtrl = TextEditingController(text: p?.contact ?? '');
|
|
_phoneCtrl = TextEditingController(text: p?.phone ?? '');
|
|
_addressCtrl = TextEditingController(text: p?.address ?? '');
|
|
_remarkCtrl = TextEditingController(text: p?.remark ?? '');
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_nameCtrl.dispose();
|
|
_codeCtrl.dispose();
|
|
_contactCtrl.dispose();
|
|
_phoneCtrl.dispose();
|
|
_addressCtrl.dispose();
|
|
_remarkCtrl.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _save() async {
|
|
if (!_formKey.currentState!.validate()) return;
|
|
setState(() => _saving = true);
|
|
final data = {
|
|
'name': _nameCtrl.text.trim(),
|
|
'type': widget.isSupplier ? 'supplier' : 'customer',
|
|
if (_codeCtrl.text.trim().isNotEmpty) 'code': _codeCtrl.text.trim(),
|
|
if (_contactCtrl.text.trim().isNotEmpty)
|
|
'contact': _contactCtrl.text.trim(),
|
|
if (_phoneCtrl.text.trim().isNotEmpty) 'phone': _phoneCtrl.text.trim(),
|
|
if (_addressCtrl.text.trim().isNotEmpty)
|
|
'address': _addressCtrl.text.trim(),
|
|
if (_remarkCtrl.text.trim().isNotEmpty) 'remark': _remarkCtrl.text.trim(),
|
|
};
|
|
try {
|
|
final notifier = widget.isSupplier
|
|
? ref.read(supplierListProvider.notifier)
|
|
: ref.read(customerListProvider.notifier);
|
|
if (widget.partner != null) {
|
|
await notifier.updatePartner(widget.partner!.id, data);
|
|
} else {
|
|
await notifier.createPartner(data);
|
|
}
|
|
if (mounted) {
|
|
Navigator.of(context).pop();
|
|
widget.onSaved();
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(widget.partner != null ? '更新成功' : '创建成功'),
|
|
backgroundColor: AppTheme.success,
|
|
),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('保存失败:$e'),
|
|
backgroundColor: AppTheme.danger),
|
|
);
|
|
}
|
|
} finally {
|
|
if (mounted) setState(() => _saving = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final isEdit = widget.partner != null;
|
|
final typeName = widget.isSupplier ? '供应商' : '客户';
|
|
return Dialog(
|
|
child: Container(
|
|
width: 520,
|
|
padding: const EdgeInsets.all(24),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(isEdit ? '编辑$typeName' : '新建$typeName',
|
|
style: const TextStyle(
|
|
fontSize: 18, fontWeight: FontWeight.w600)),
|
|
const SizedBox(height: 20),
|
|
TextFormField(
|
|
controller: _nameCtrl,
|
|
decoration: InputDecoration(labelText: '$typeName名称'),
|
|
validator: (v) =>
|
|
(v == null || v.isEmpty) ? '不能为空' : null,
|
|
),
|
|
const SizedBox(height: 12),
|
|
Row(children: [
|
|
if (isEdit) ...[
|
|
Expanded(
|
|
child: TextFormField(
|
|
controller: _codeCtrl,
|
|
decoration: const InputDecoration(labelText: '编码'),
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
],
|
|
Expanded(
|
|
child: TextFormField(
|
|
controller: _contactCtrl,
|
|
decoration: const InputDecoration(labelText: '联系人'),
|
|
),
|
|
),
|
|
]),
|
|
const SizedBox(height: 12),
|
|
TextFormField(
|
|
controller: _phoneCtrl,
|
|
decoration: const InputDecoration(labelText: '联系电话'),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextFormField(
|
|
controller: _addressCtrl,
|
|
decoration: const InputDecoration(labelText: '地址'),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextFormField(
|
|
controller: _remarkCtrl,
|
|
decoration: const InputDecoration(labelText: '备注'),
|
|
maxLines: 2,
|
|
),
|
|
const SizedBox(height: 24),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
children: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text('取消'),
|
|
),
|
|
const SizedBox(width: 8),
|
|
ElevatedButton(
|
|
onPressed: _saving ? null : _save,
|
|
child: _saving
|
|
? const SizedBox(
|
|
width: 16,
|
|
height: 16,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2, color: Colors.white))
|
|
: const Text('保存'),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|