新增提示Toast组件
This commit is contained in:
+90
-17
@@ -49,6 +49,7 @@ import type { EvaluationPointGroup } from "~/models/evaluation_point_groups";
|
||||
// 导入RuleContext上下文
|
||||
import { RuleContext } from "~/contexts/RuleContext";
|
||||
import { postgrestGet, postgrestPost, postgrestPut } from "~/api/postgrest-client";
|
||||
import { toastService } from '~/components/ui/Toast';
|
||||
|
||||
export const meta: MetaFunction = () => {
|
||||
return [
|
||||
@@ -65,7 +66,21 @@ export function links() {
|
||||
}
|
||||
|
||||
export const handle = {
|
||||
breadcrumb: "评查点管理"
|
||||
breadcrumb: "评查点管理",
|
||||
previousRoute: () => {
|
||||
if (typeof window !== 'undefined') {
|
||||
const searchParams = new URLSearchParams(window.location.search);
|
||||
const mode = searchParams.get('mode');
|
||||
const id = searchParams.get('id');
|
||||
if (mode || id) {
|
||||
return {
|
||||
title: "评查点列表",
|
||||
to: `/rules`
|
||||
};
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
// 添加规则配置接口
|
||||
@@ -266,13 +281,13 @@ export default function RuleNew() {
|
||||
setInstanceKey(`edit_${id}_${Date.now()}`);
|
||||
} catch (jsonError) {
|
||||
console.error('JSON处理错误:', jsonError);
|
||||
alert(`数据处理错误: ${jsonError instanceof Error ? jsonError.message : '未知错误'}`);
|
||||
toastService.error(`数据处理错误: ${jsonError instanceof Error ? jsonError.message : '未知错误'}`);
|
||||
resetFormData();
|
||||
navigate('/rules');
|
||||
}
|
||||
} else {
|
||||
console.error('获取数据失败: 返回数据为空');
|
||||
alert('获取数据失败: 返回数据为空');
|
||||
toastService.error('获取数据失败: 返回数据为空');
|
||||
resetFormData();
|
||||
navigate('/rules');
|
||||
}
|
||||
@@ -281,7 +296,7 @@ export default function RuleNew() {
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取评查点数据失败:', error);
|
||||
alert(`获取评查点数据失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
||||
toastService.error(`获取评查点数据失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
||||
// 获取数据失败时返回上一页
|
||||
resetFormData();
|
||||
navigate('/rules');
|
||||
@@ -305,7 +320,7 @@ export default function RuleNew() {
|
||||
} catch (error) {
|
||||
console.error('获取评查点组数据失败:', error);
|
||||
// 显示错误提示但不影响应用继续使用
|
||||
alert(`获取评查点组数据失败: ${error instanceof Error ? error.message : '未知错误'}\n将使用默认数据`);
|
||||
toastService.error(`获取评查点组数据失败: ${error instanceof Error ? error.message : '未知错误'}\n将使用默认数据`);
|
||||
}
|
||||
}, []);
|
||||
|
||||
@@ -314,12 +329,12 @@ export default function RuleNew() {
|
||||
|
||||
// 验证必填字段
|
||||
if (!formData.name?.trim()) {
|
||||
alert("评查点名称不能为空");
|
||||
toastService.warning("评查点名称不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!formData.code?.trim()) {
|
||||
alert("评查点编码不能为空");
|
||||
toastService.warning("评查点编码不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -379,6 +394,23 @@ export default function RuleNew() {
|
||||
score: formData.score !== undefined ? Number(formData.score) : 0
|
||||
};
|
||||
|
||||
// 获取当前所有有效的抽取字段
|
||||
const currentExtractionFields = extractionFields.map(field => {
|
||||
// 处理字段名,去掉类型后缀(如果有)
|
||||
if (field.includes('_')) {
|
||||
return field.split('_')[0];
|
||||
}
|
||||
return field;
|
||||
});
|
||||
|
||||
// 去重,确保不会有重复字段
|
||||
const validFields = [...new Set(currentExtractionFields)];
|
||||
console.log("当前有效的抽取字段:", validFields);
|
||||
|
||||
// 重要:这段代码解决了字段删除后,评查配置中仍保留已删除字段的问题
|
||||
// 在保存前,我们会确保所有规则中引用的字段都是当前有效的抽取字段
|
||||
// 这样即使用户在界面操作中删除了某个抽取字段,相关的评查规则也会被自动清理
|
||||
|
||||
// 确保rules中的每个配置对象都被正确处理
|
||||
if (cleanedData.evaluation_config && Array.isArray(cleanedData.evaluation_config.rules)) {
|
||||
cleanedData.evaluation_config.rules = cleanedData.evaluation_config.rules
|
||||
@@ -390,6 +422,12 @@ export default function RuleNew() {
|
||||
switch (rule.type) {
|
||||
case 'exists':
|
||||
if (!Array.isArray((config as ExistsRuleConfig).fields)) (config as ExistsRuleConfig).fields = [];
|
||||
// 过滤掉不存在于当前抽取字段中的字段
|
||||
if (Array.isArray((config as ExistsRuleConfig).fields)) {
|
||||
(config as ExistsRuleConfig).fields = (config as ExistsRuleConfig).fields.filter(
|
||||
field => validFields.includes(field)
|
||||
);
|
||||
}
|
||||
if (!(config as ExistsRuleConfig).logic) (config as ExistsRuleConfig).logic = 'and';
|
||||
// 删除不必要的字段
|
||||
delete (config as ExistsRuleConfig & {availableFields?: string}).availableFields;
|
||||
@@ -399,6 +437,12 @@ export default function RuleNew() {
|
||||
|
||||
case 'consistency':
|
||||
if (!Array.isArray((config as ConsistencyRuleConfig).pairs)) (config as ConsistencyRuleConfig).pairs = [];
|
||||
// 过滤掉包含不存在于当前抽取字段中的字段的配对
|
||||
if (Array.isArray((config as ConsistencyRuleConfig).pairs)) {
|
||||
(config as ConsistencyRuleConfig).pairs = (config as ConsistencyRuleConfig).pairs.filter(
|
||||
pair => validFields.includes(pair.sourceField) && validFields.includes(pair.targetField)
|
||||
);
|
||||
}
|
||||
if (!(config as ConsistencyRuleConfig).logic) (config as ConsistencyRuleConfig).logic = 'and';
|
||||
delete (config as ConsistencyRuleConfig & {availableFields?: string}).availableFields;
|
||||
delete (config as ConsistencyRuleConfig).logicRelation;
|
||||
@@ -408,6 +452,10 @@ export default function RuleNew() {
|
||||
break;
|
||||
|
||||
case 'format':
|
||||
// 检查字段是否存在于当前抽取字段中
|
||||
if ((config as FormatRuleConfig).field && !validFields.includes((config as FormatRuleConfig).field)) {
|
||||
(config as FormatRuleConfig).field = '';
|
||||
}
|
||||
if (!(config as FormatRuleConfig).field) (config as FormatRuleConfig).field = '';
|
||||
if (!(config as FormatRuleConfig).formatType) (config as FormatRuleConfig).formatType = 'date';
|
||||
if (!(config as FormatRuleConfig).parameters) (config as FormatRuleConfig).parameters = '';
|
||||
@@ -418,6 +466,12 @@ export default function RuleNew() {
|
||||
|
||||
case 'logic':
|
||||
if (!Array.isArray((config as LogicRuleConfig).conditions)) (config as LogicRuleConfig).conditions = [];
|
||||
// 过滤掉包含不存在于当前抽取字段中的字段的条件
|
||||
if (Array.isArray((config as LogicRuleConfig).conditions)) {
|
||||
(config as LogicRuleConfig).conditions = (config as LogicRuleConfig).conditions.filter(
|
||||
condition => validFields.includes(condition.field)
|
||||
);
|
||||
}
|
||||
if (!(config as LogicRuleConfig).logic) (config as LogicRuleConfig).logic = 'and';
|
||||
delete (config as LogicRuleConfig & {availableFields?: string}).availableFields;
|
||||
delete (config as LogicRuleConfig).logicRelation;
|
||||
@@ -427,6 +481,10 @@ export default function RuleNew() {
|
||||
break;
|
||||
|
||||
case 'regex':
|
||||
// 检查字段是否存在于当前抽取字段中
|
||||
if ((config as RegexRuleConfig).field && !validFields.includes((config as RegexRuleConfig).field)) {
|
||||
(config as RegexRuleConfig).field = '';
|
||||
}
|
||||
if (!(config as RegexRuleConfig).field) (config as RegexRuleConfig).field = '';
|
||||
if (!(config as RegexRuleConfig).pattern) (config as RegexRuleConfig).pattern = '';
|
||||
if (!(config as RegexRuleConfig).matchType) (config as RegexRuleConfig).matchType = 'match';
|
||||
@@ -456,6 +514,8 @@ export default function RuleNew() {
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// console.log("当前评查配置-----------------:", formData.evaluation_config);
|
||||
|
||||
// 如果是新建模式,则删除id字段
|
||||
if (!isEditMode) {
|
||||
@@ -499,14 +559,19 @@ export default function RuleNew() {
|
||||
}
|
||||
|
||||
if (response.error) {
|
||||
alert(`系统繁忙: ${response.error}`);
|
||||
if (response.error.includes('evaluation_points_code_key')) {
|
||||
toastService.error('在基本信息中:评查点编码已存在,请修改后保存。');
|
||||
} else {
|
||||
toastService.error(`系统繁忙: ${response.error}`);
|
||||
}
|
||||
setIsLoading(false);
|
||||
} else if (response.data && Array.isArray(response.data) && response.data.length > 0) {
|
||||
// 获取新创建或更新的评查点ID
|
||||
const savedPointId = response.data[0]?.id;
|
||||
|
||||
if (savedPointId) {
|
||||
// 显示成功消息
|
||||
alert(`评查点${isEditMode ? '更新' : '创建'}成功!`);
|
||||
toastService.success(`评查点${isEditMode ? '更新' : '创建'}成功!`);
|
||||
|
||||
// 保存成功后跳转到编辑页面并重新加载数据
|
||||
navigate(`/rules-new?id=${savedPointId}`, { replace: true });
|
||||
@@ -514,22 +579,22 @@ export default function RuleNew() {
|
||||
await fetchEvaluationPoint(savedPointId);
|
||||
} else {
|
||||
// 无法获取ID的情况
|
||||
alert(`评查点${isEditMode ? '更新' : '创建'}成功,但无法获取ID。正在返回列表页面。`);
|
||||
toastService.warning(`评查点${isEditMode ? '更新' : '创建'}成功,但无法获取ID。正在返回列表页面。`);
|
||||
navigate('/rules');
|
||||
}
|
||||
} else {
|
||||
alert(`系统繁忙`);
|
||||
toastService.error('系统繁忙');
|
||||
}
|
||||
} catch (jsonError) {
|
||||
console.error("JSON处理错误:", jsonError);
|
||||
alert(`数据处理错误: ${jsonError instanceof Error ? jsonError.message : '未知错误'}`);
|
||||
toastService.error(`数据处理错误: ${jsonError instanceof Error ? jsonError.message : '未知错误'}`);
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error("数据处理错误:", error);
|
||||
alert(`数据处理错误: ${error instanceof Error ? error.message : '未知错误'}`);
|
||||
toastService.error(`数据处理错误: ${error instanceof Error ? error.message : '未知错误'}`);
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
@@ -586,17 +651,21 @@ export default function RuleNew() {
|
||||
customLogic: '',
|
||||
rules: []
|
||||
};
|
||||
|
||||
// console.log("当前评查配置:", currentConfig);
|
||||
// console.log("变更评查配置:", data.evaluation_config);
|
||||
|
||||
// 合并评查配置数据
|
||||
const mergedConfig = {
|
||||
...currentConfig,
|
||||
...(data.evaluation_config as object)
|
||||
};
|
||||
|
||||
// console.log("合并评查配置:", data.evaluation_config);
|
||||
// 更新表单数据
|
||||
setFormData(prev => ({
|
||||
...prev,
|
||||
evaluation_config: mergedConfig
|
||||
// evaluation_config: data.evaluation_config as typeof prev.evaluation_config
|
||||
}));
|
||||
} else {
|
||||
// 处理其他普通字段
|
||||
@@ -634,11 +703,15 @@ export default function RuleNew() {
|
||||
*/
|
||||
useEffect(() => {
|
||||
const searchParams = new URLSearchParams(location.search);
|
||||
const id = searchParams.get('id');
|
||||
const id = searchParams.get('id');
|
||||
const mode = searchParams.get('mode');
|
||||
|
||||
// 设置编辑模式
|
||||
const newIsEditMode = !!id;
|
||||
setIsEditMode(newIsEditMode);
|
||||
if (mode && mode === 'copy') {
|
||||
setIsEditMode(false);
|
||||
} else {
|
||||
setIsEditMode(!!id);
|
||||
}
|
||||
|
||||
if (id) {
|
||||
// 编辑模式:获取数据
|
||||
|
||||
Reference in New Issue
Block a user