Files
jiu/client/lib/providers/inventory_provider.dart
T
wangjia 3888cf2d65 feat(client): 慢请求监控(>500ms 记日志+上报);拆除列表 provider 的 _cache 失败兜底
- SlowRequestInterceptor:单次 API 调用超 500ms 即 debugPrint long-request
  日志并经 ErrorReporter 上报(error_type=slow_api,按 method+归一路径 5 分钟
  节流)。服务端 GIN 日志只见自身处理耗时,网络往返段只有客户端可观测。
- 拆除 10 个列表 provider 的 _cache 失败兜底:实测线上接口客户端视角典型
  50-120ms、最坏约 270ms(列表类全部 <500ms),失败静默端旧数据弊大于利
  (曾放大跨账号残留问题),改为明确进入错误态由用户重试。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 00:49:29 +08:00

170 lines
4.3 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../core/api/api_client.dart';
import '../core/auth/auth_state.dart';
import '../core/config/app_constants.dart';
import '../core/models/page_result.dart';
import '../models/inventory.dart';
import '../repositories/inventory_repository.dart';
import 'connectivity_provider.dart';
final inventoryRepositoryProvider = Provider<InventoryRepository>((ref) {
return InventoryRepository(ref.watch(apiClientProvider));
});
final inventoryListProvider =
AsyncNotifierProvider<InventoryListNotifier, PageResult<Inventory>>(
InventoryListNotifier.new,
);
/// 全店库存 KPI 汇总(不随分页;换店/网络恢复自动刷新)。
final inventorySummaryProvider =
FutureProvider.autoDispose<InventorySummary>((ref) {
ref.watch(authStateProvider.select((s) => s.user?.shopId));
ref.watch(networkRecoveryCountProvider);
return ref.read(inventoryRepositoryProvider).getSummary();
});
class InventoryListNotifier extends AsyncNotifier<PageResult<Inventory>> {
int _page = 1;
int _pageSize = AppConstants.defaultPageSize;
int? _warehouseId;
String _keyword = '';
String _code = ''; // 商品编码独立筛选(原型 codeInput)
String _status = ''; // 三态筛选(空=后端默认排除已售)
String _sortBy = ''; // 列头排序(空=默认 id 倒序)
bool _sortAsc = true;
List<String> _series = [];
List<String> _spec = [];
@override
Future<PageResult<Inventory>> build() async {
ref.watch(authStateProvider.select((s) => s.user?.shopId));
ref.watch(networkRecoveryCountProvider); // 网络恢复时自动刷新
return _fetch();
}
Future<PageResult<Inventory>> _fetch() {
return ref.read(inventoryRepositoryProvider).listInventory(
warehouseId: _warehouseId,
keyword: _keyword.isEmpty ? null : _keyword,
code: _code.isEmpty ? null : _code,
status: _status.isEmpty ? null : _status,
sortBy: _sortBy.isEmpty ? null : _sortBy,
sortAsc: _sortAsc,
series: _series.isEmpty ? null : _series,
spec: _spec.isEmpty ? null : _spec,
page: _page,
pageSize: _pageSize,
);
}
void setPage(int page) {
_page = page;
reload();
}
void setPageSize(int pageSize) {
_pageSize = pageSize;
_page = 1;
reload();
}
void setWarehouseId(int? id) {
_warehouseId = id;
_page = 1;
reload();
}
void setKeyword(String keyword) {
_keyword = keyword;
_page = 1;
reload();
}
void setCode(String code) {
_code = code;
_page = 1;
reload();
}
void setStatus(String status) {
_status = status;
_page = 1;
reload();
}
void setSort(String sortBy, bool asc) {
_sortBy = sortBy;
_sortAsc = asc;
_page = 1;
reload();
}
void setSeries(List<String> series) {
_series = series;
_page = 1;
reload();
}
void setSpec(List<String> spec) {
_spec = spec;
_page = 1;
reload();
}
void reload() {
// 保留上一次数据(isReloading)→ 屏幕渲染旧内容 + 半透明进度,不白屏。
state = const AsyncValue<PageResult<Inventory>>.loading()
.copyWithPrevious(state);
_fetch().then((result) {
state = AsyncValue.data(result);
}, onError: (e, st) {
state = AsyncValue.error(e, st);
});
}
}
final inventoryLogProvider =
AsyncNotifierProvider<InventoryLogNotifier, PageResult<InventoryLog>>(
InventoryLogNotifier.new,
);
class InventoryLogNotifier extends AsyncNotifier<PageResult<InventoryLog>> {
int _page = 1;
int _pageSize = AppConstants.defaultPageSize;
@override
Future<PageResult<InventoryLog>> build() async {
ref.watch(authStateProvider.select((s) => s.user?.shopId));
ref.watch(networkRecoveryCountProvider);
return _fetch();
}
Future<PageResult<InventoryLog>> _fetch() {
return ref.read(inventoryRepositoryProvider).listLogs(
page: _page,
pageSize: _pageSize,
);
}
void setPage(int page) {
_page = page;
reload();
}
void setPageSize(int pageSize) {
_pageSize = pageSize;
_page = 1;
reload();
}
void reload() {
state = const AsyncValue.loading();
_fetch().then((result) {
state = AsyncValue.data(result);
}, onError: (e, st) {
state = AsyncValue.error(e, st);
});
}
}