106 lines
3.3 KiB
Dart
106 lines
3.3 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
import '../../../shared/notifications/notification_service.dart';
|
|
import '../../../shared/network/api_client.dart';
|
|
import '../../../shared/storage/session_store.dart';
|
|
import '../domain/contract_models.dart';
|
|
|
|
class ContractGenerationController {
|
|
ContractGenerationController({ApiClient? apiClient}) : _apiClient = apiClient ?? const ApiClient();
|
|
|
|
final ApiClient _apiClient;
|
|
|
|
final ValueNotifier<bool> isPreviewing = ValueNotifier<bool>(false);
|
|
final ValueNotifier<bool> isGenerating = ValueNotifier<bool>(false);
|
|
final ValueNotifier<String?> errorMessage = ValueNotifier<String?>(null);
|
|
final ValueNotifier<ContractPreviewItem?> preview = ValueNotifier<ContractPreviewItem?>(null);
|
|
|
|
Future<void> loadPreview({
|
|
required String templateId,
|
|
required Map<String, String> variableValues,
|
|
}) async {
|
|
final session = SessionStore.current;
|
|
if (session == null || !session.isAuthenticated) {
|
|
errorMessage.value = '请先登录后预览合同';
|
|
preview.value = null;
|
|
return;
|
|
}
|
|
|
|
isPreviewing.value = true;
|
|
errorMessage.value = null;
|
|
try {
|
|
preview.value = await _apiClient.previewContract(
|
|
session.accessToken,
|
|
templateId: templateId,
|
|
variableValues: variableValues,
|
|
);
|
|
} on ApiException catch (error) {
|
|
if (error.isUnauthorized) {
|
|
await SessionStore.clear();
|
|
errorMessage.value = '登录已过期,请重新登录';
|
|
} else {
|
|
errorMessage.value = error.message;
|
|
}
|
|
preview.value = null;
|
|
} catch (_) {
|
|
errorMessage.value = '合同预览失败,请稍后重试';
|
|
preview.value = null;
|
|
} finally {
|
|
isPreviewing.value = false;
|
|
}
|
|
}
|
|
|
|
Future<GeneratedContractItem> generate({
|
|
required String templateId,
|
|
required Map<String, String> variableValues,
|
|
String exportFormat = 'docx',
|
|
}) async {
|
|
final session = SessionStore.current;
|
|
if (session == null || !session.isAuthenticated) {
|
|
throw const ContractGenerationException('请先登录后生成合同');
|
|
}
|
|
|
|
isGenerating.value = true;
|
|
errorMessage.value = null;
|
|
try {
|
|
final contract = await _apiClient.generateContract(
|
|
session.accessToken,
|
|
templateId: templateId,
|
|
variableValues: variableValues,
|
|
exportFormat: exportFormat,
|
|
);
|
|
await NotificationService.instance.notifyContractGenerated(contract);
|
|
return contract;
|
|
} on ApiException catch (error) {
|
|
if (error.isUnauthorized) {
|
|
await SessionStore.clear();
|
|
errorMessage.value = '登录已过期,请重新登录';
|
|
throw const ContractGenerationException('登录已过期,请重新登录');
|
|
}
|
|
errorMessage.value = error.message;
|
|
throw ContractGenerationException(error.message);
|
|
} catch (_) {
|
|
errorMessage.value = '合同生成失败,请稍后重试';
|
|
throw const ContractGenerationException('合同生成失败,请稍后重试');
|
|
} finally {
|
|
isGenerating.value = false;
|
|
}
|
|
}
|
|
|
|
void dispose() {
|
|
isPreviewing.dispose();
|
|
isGenerating.dispose();
|
|
errorMessage.dispose();
|
|
preview.dispose();
|
|
}
|
|
}
|
|
|
|
class ContractGenerationException implements Exception {
|
|
const ContractGenerationException(this.message);
|
|
|
|
final String message;
|
|
|
|
@override
|
|
String toString() => message;
|
|
}
|