3888cf2d65
- 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>
92 lines
2.2 KiB
Dart
92 lines
2.2 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/product.dart';
|
|
import '../repositories/product_repository.dart';
|
|
import 'connectivity_provider.dart';
|
|
|
|
final productRepositoryProvider = Provider<ProductRepository>((ref) {
|
|
return ProductRepository(ref.watch(apiClientProvider));
|
|
});
|
|
|
|
final productListProvider =
|
|
AsyncNotifierProvider<ProductListNotifier, PageResult<Product>>(
|
|
ProductListNotifier.new,
|
|
);
|
|
|
|
class ProductListNotifier extends AsyncNotifier<PageResult<Product>> {
|
|
int _page = 1;
|
|
int _pageSize = AppConstants.defaultPageSize;
|
|
String _keyword = '';
|
|
int? _categoryId;
|
|
|
|
@override
|
|
Future<PageResult<Product>> build() async {
|
|
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
|
ref.watch(networkRecoveryCountProvider);
|
|
return _fetch();
|
|
}
|
|
|
|
Future<PageResult<Product>> _fetch() {
|
|
final repo = ref.read(productRepositoryProvider);
|
|
return repo.list(
|
|
page: _page,
|
|
pageSize: _pageSize,
|
|
keyword: _keyword.isEmpty ? null : _keyword,
|
|
categoryId: _categoryId,
|
|
);
|
|
}
|
|
|
|
void setPage(int page) {
|
|
_page = page;
|
|
reload();
|
|
}
|
|
|
|
void setPageSize(int pageSize) {
|
|
_pageSize = pageSize;
|
|
_page = 1;
|
|
reload();
|
|
}
|
|
|
|
void setKeyword(String keyword) {
|
|
_keyword = keyword;
|
|
_page = 1;
|
|
reload();
|
|
}
|
|
|
|
void setCategoryId(int? categoryId) {
|
|
_categoryId = categoryId;
|
|
_page = 1;
|
|
reload();
|
|
}
|
|
|
|
void reload() {
|
|
state = const AsyncValue.loading();
|
|
_fetch().then((result) {
|
|
state = AsyncValue.data(result);
|
|
}, onError: (e, st) {
|
|
state = AsyncValue.error(e, st);
|
|
});
|
|
}
|
|
|
|
Future<void> createProduct(Map<String, dynamic> data) async {
|
|
final repo = ref.read(productRepositoryProvider);
|
|
await repo.create(data);
|
|
reload();
|
|
}
|
|
|
|
Future<void> updateProduct(int id, Map<String, dynamic> data) async {
|
|
final repo = ref.read(productRepositoryProvider);
|
|
await repo.update(id, data);
|
|
reload();
|
|
}
|
|
|
|
Future<void> deleteProduct(int id) async {
|
|
final repo = ref.read(productRepositoryProvider);
|
|
await repo.delete(id);
|
|
reload();
|
|
}
|
|
}
|