59 lines
1.7 KiB
Dart
59 lines
1.7 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
import '../../../shared/network/api_client.dart';
|
|
import '../../../shared/storage/session_store.dart';
|
|
import '../domain/contract_models.dart';
|
|
|
|
class ContractTemplateController {
|
|
ContractTemplateController({ApiClient? apiClient})
|
|
: _apiClient = apiClient ?? const ApiClient();
|
|
|
|
final ApiClient _apiClient;
|
|
|
|
final ValueNotifier<bool> isLoading = ValueNotifier<bool>(false);
|
|
final ValueNotifier<String?> errorMessage = ValueNotifier<String?>(null);
|
|
final ValueNotifier<List<ContractTemplateItem>> items =
|
|
ValueNotifier<List<ContractTemplateItem>>(<ContractTemplateItem>[]);
|
|
|
|
Future<void> load({
|
|
String? contractType,
|
|
String? templateCategory,
|
|
String? industryCategory,
|
|
}) async {
|
|
final session = SessionStore.current;
|
|
if (session == null || !session.isAuthenticated) {
|
|
errorMessage.value = '请先登录后查看合同模板';
|
|
return;
|
|
}
|
|
|
|
isLoading.value = true;
|
|
errorMessage.value = null;
|
|
try {
|
|
items.value = await _apiClient.listContractTemplates(
|
|
session.accessToken,
|
|
contractType: contractType,
|
|
templateCategory: templateCategory,
|
|
industryCategory: industryCategory,
|
|
);
|
|
} on ApiException catch (error) {
|
|
if (error.isUnauthorized) {
|
|
await SessionStore.clear();
|
|
errorMessage.value = '登录已过期,请重新登录';
|
|
} else {
|
|
errorMessage.value = error.message;
|
|
}
|
|
} catch (_) {
|
|
errorMessage.value = '获取模板列表失败,请稍后重试';
|
|
items.value = <ContractTemplateItem>[];
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
void dispose() {
|
|
isLoading.dispose();
|
|
errorMessage.dispose();
|
|
items.dispose();
|
|
}
|
|
}
|