分数,评查点分组数据对接上
This commit is contained in:
@@ -2,7 +2,8 @@ import React, { useState, useEffect } from 'react';
|
||||
|
||||
interface BasicInfoProps {
|
||||
onChange?: (data: Record<string, unknown>) => void;
|
||||
initialData?: Record<string, any>;
|
||||
initialData?: FormDataType;
|
||||
evaluationPointGroups?: Array<{id: number, pid: number, code: string, name: string, is_enabled: boolean}>;
|
||||
}
|
||||
|
||||
// 定义表单数据类型
|
||||
@@ -18,10 +19,12 @@ interface FormDataType {
|
||||
content: string;
|
||||
};
|
||||
evaluation_point_groups_id: number | null;
|
||||
evaluation_point_groups_pid: number | null;
|
||||
type: string;
|
||||
id?: number;
|
||||
}
|
||||
|
||||
export function BasicInfo({ onChange, initialData }: BasicInfoProps) {
|
||||
export function BasicInfo({ onChange, initialData, evaluationPointGroups = [] }: BasicInfoProps) {
|
||||
const [formData, setFormData] = useState<FormDataType>({
|
||||
name: '',
|
||||
code: '',
|
||||
@@ -34,11 +37,13 @@ export function BasicInfo({ onChange, initialData }: BasicInfoProps) {
|
||||
content: ''
|
||||
},
|
||||
evaluation_point_groups_id: null,
|
||||
evaluation_point_groups_pid: null,
|
||||
type: ''
|
||||
});
|
||||
|
||||
const [isDescExpanded, setIsDescExpanded] = useState(false);
|
||||
const [lawArticlesInput, setLawArticlesInput] = useState('');
|
||||
const [filteredRuleGroups, setFilteredRuleGroups] = useState<Array<{id: number, pid: number, code: string, name: string, is_enabled: boolean}>>([]);
|
||||
|
||||
// 当initialData变化时更新表单数据
|
||||
useEffect(() => {
|
||||
@@ -55,6 +60,7 @@ export function BasicInfo({ onChange, initialData }: BasicInfoProps) {
|
||||
content: ''
|
||||
},
|
||||
evaluation_point_groups_id: initialData.evaluation_point_groups_id || null,
|
||||
evaluation_point_groups_pid: initialData.evaluation_point_groups_pid || null,
|
||||
type: initialData.type || ''
|
||||
};
|
||||
|
||||
@@ -76,6 +82,113 @@ export function BasicInfo({ onChange, initialData }: BasicInfoProps) {
|
||||
}
|
||||
}, [initialData]);
|
||||
|
||||
// 当评查点类型或评查点组数据变化时,过滤规则组列表
|
||||
useEffect(() => {
|
||||
if (evaluationPointGroups && evaluationPointGroups.length > 0) {
|
||||
console.log("评查点组数据更新,当前类型:", formData.type);
|
||||
console.log("评查点组数据:", evaluationPointGroups);
|
||||
|
||||
if (formData.type) {
|
||||
// 获取所选评查点类型的组ID
|
||||
const typeGroup = evaluationPointGroups.find(group =>
|
||||
group.pid === 0 &&
|
||||
group.code === formData.type
|
||||
);
|
||||
|
||||
console.log("找到的类型组:", typeGroup);
|
||||
|
||||
if (typeGroup) {
|
||||
// 更新评查点类型组ID
|
||||
if (formData.evaluation_point_groups_pid !== typeGroup.id) {
|
||||
const newData = {
|
||||
...formData,
|
||||
evaluation_point_groups_pid: typeGroup.id
|
||||
};
|
||||
setFormData(newData);
|
||||
if (onChange) onChange(newData);
|
||||
}
|
||||
|
||||
// 过滤出属于该类型的规则组(pid等于类型组ID的项)
|
||||
const groups = evaluationPointGroups.filter(group =>
|
||||
group.pid === typeGroup.id &&
|
||||
group.is_enabled
|
||||
);
|
||||
console.log("过滤后的规则组:", groups);
|
||||
setFilteredRuleGroups(groups);
|
||||
|
||||
// 如果当前选择的规则组不在过滤结果中,重置选择
|
||||
if (formData.evaluation_point_groups_id &&
|
||||
!groups.some(group => group.id === formData.evaluation_point_groups_id)) {
|
||||
console.log("当前选择的规则组不在过滤结果中,重置选择");
|
||||
const newData = {
|
||||
...formData,
|
||||
evaluation_point_groups_id: null
|
||||
};
|
||||
setFormData(newData);
|
||||
if (onChange) onChange(newData);
|
||||
}
|
||||
} else {
|
||||
console.log("未找到对应的类型组");
|
||||
setFilteredRuleGroups([]);
|
||||
|
||||
// 重置评查点类型组ID
|
||||
if (formData.evaluation_point_groups_pid !== null) {
|
||||
const newData = {
|
||||
...formData,
|
||||
evaluation_point_groups_pid: null
|
||||
};
|
||||
setFormData(newData);
|
||||
if (onChange) onChange(newData);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log("未选择评查点类型");
|
||||
setFilteredRuleGroups([]);
|
||||
|
||||
// 重置评查点类型组ID
|
||||
if (formData.evaluation_point_groups_pid !== null) {
|
||||
const newData = {
|
||||
...formData,
|
||||
evaluation_point_groups_pid: null
|
||||
};
|
||||
setFormData(newData);
|
||||
if (onChange) onChange(newData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [formData.type, evaluationPointGroups, formData.evaluation_point_groups_id, formData.evaluation_point_groups_pid, onChange]);
|
||||
|
||||
// 获取评查点类型选项(pid=0的数据)
|
||||
const getCheckpointTypeOptions = () => {
|
||||
if (!evaluationPointGroups || evaluationPointGroups.length === 0) {
|
||||
console.log("无评查点组数据,使用默认类型选项");
|
||||
return (
|
||||
<>
|
||||
<option value="">请选择评查点类型</option>
|
||||
<option value="essential">基本要素类</option>
|
||||
<option value="content">内容合规类</option>
|
||||
<option value="format">格式规范类</option>
|
||||
<option value="legal">法律风险类</option>
|
||||
<option value="business">业务专项类</option>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
const typeGroups = evaluationPointGroups.filter(group => group.pid === 0 && group.is_enabled);
|
||||
console.log("可用的评查点类型:", typeGroups);
|
||||
|
||||
return (
|
||||
<>
|
||||
<option value="">请选择评查点类型</option>
|
||||
{typeGroups.map(group => (
|
||||
<option key={group.id} value={group.code}>
|
||||
{group.name}
|
||||
</option>
|
||||
))}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
const handleToggleDescription = () => {
|
||||
setIsDescExpanded(!isDescExpanded);
|
||||
};
|
||||
@@ -115,6 +228,19 @@ export function BasicInfo({ onChange, initialData }: BasicInfoProps) {
|
||||
break;
|
||||
case 'checkpoint-type':
|
||||
newData.type = value;
|
||||
// 重置规则组选择
|
||||
newData.evaluation_point_groups_id = null;
|
||||
|
||||
// 设置评查点类型组ID
|
||||
if (value) {
|
||||
const typeGroup = evaluationPointGroups.find(group =>
|
||||
group.pid === 0 &&
|
||||
group.code === value
|
||||
);
|
||||
newData.evaluation_point_groups_pid = typeGroup ? typeGroup.id : null;
|
||||
} else {
|
||||
newData.evaluation_point_groups_pid = null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -207,32 +333,37 @@ export function BasicInfo({ onChange, initialData }: BasicInfoProps) {
|
||||
value={formData.type}
|
||||
onChange={handleInputChange}
|
||||
>
|
||||
<option value="">请选择评查点类型</option>
|
||||
<option value="essential">基本要素类</option>
|
||||
<option value="content">内容合规类</option>
|
||||
<option value="format">格式规范类</option>
|
||||
<option value="legal">法律风险类</option>
|
||||
<option value="business">业务专项类</option>
|
||||
{getCheckpointTypeOptions()}
|
||||
</select>
|
||||
<div className="form-tip">评查点类型用于分类管理,便于规则统一调用</div>
|
||||
</div>
|
||||
<div>
|
||||
<label className="form-label" htmlFor="evaluation-point-group">
|
||||
所属规则组
|
||||
所属规则组 <span className="required-mark">*</span>
|
||||
</label>
|
||||
<select
|
||||
id="evaluation-point-group"
|
||||
className="form-select"
|
||||
className={`form-select ${!formData.type || filteredRuleGroups.length === 0 ? 'bg-gray-100 cursor-not-allowed' : ''}`}
|
||||
value={formData.evaluation_point_groups_id?.toString() || ""}
|
||||
onChange={handleInputChange}
|
||||
disabled={!formData.type || filteredRuleGroups.length === 0}
|
||||
>
|
||||
<option value="">请选择规则组</option>
|
||||
<option value="1">合同基本要素检查</option>
|
||||
<option value="2">销售合同专项检查</option>
|
||||
<option value="3">采购合同专项检查</option>
|
||||
<option value="4">专卖许可证审核规则</option>
|
||||
<option value="5">行政处罚规范性检查</option>
|
||||
<option value="">
|
||||
{!formData.type ? "请先选择评查点类型" :
|
||||
filteredRuleGroups.length === 0 ? "该类型下暂无可用规则组" :
|
||||
"请选择规则组"}
|
||||
</option>
|
||||
{filteredRuleGroups.map(group => (
|
||||
<option key={group.id} value={group.id.toString()}>
|
||||
{group.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<div className="form-tip">
|
||||
{!formData.type ? "请先选择评查点类型" :
|
||||
filteredRuleGroups.length === 0 ? "该类型下暂无可用规则组" :
|
||||
"选择评查点所属的规则组"}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label className="form-label" htmlFor="is-enabled">是否启用</label>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useState, KeyboardEvent, FormEvent, useContext, useEffect, useCallback, useRef } from 'react';
|
||||
import { RuleContext } from './ReviewSettings';
|
||||
import { RuleContext } from '~/contexts/RuleContext';
|
||||
import { processFieldName } from '~/utils';
|
||||
|
||||
/**
|
||||
* ExtractionSettings 组件
|
||||
@@ -169,7 +170,7 @@ export function ExtractionSettings({ onChange, initialData }: ExtractionSettings
|
||||
|
||||
// 获取所有字段(不包括regexFields,这部分单独处理)
|
||||
const llm_ocr_fields = fields.llm_ocr || [];
|
||||
const llm_fields = (fields.llm || []).map((field) => field.split('_')[0]);
|
||||
const llm_fields = (fields.llm || []).map(processFieldName);
|
||||
|
||||
// 检查是否在其他类型字段中存在
|
||||
if (llm_ocr_fields.some(f => f.toLowerCase() === fieldNameLower) ||
|
||||
@@ -202,7 +203,7 @@ export function ExtractionSettings({ onChange, initialData }: ExtractionSettings
|
||||
// 收集所有字段名 - 不受当前标签页影响,始终收集所有类型的字段
|
||||
const allFieldNamesList = [
|
||||
...fields.llm_ocr,
|
||||
...fields.llm.map(f => f.split('_')[0]),
|
||||
...fields.llm.map(f => processFieldName(f)),
|
||||
...validRegexFields.map(f => f.fieldName.trim())
|
||||
].filter(name => name); // 过滤空值
|
||||
|
||||
@@ -250,20 +251,7 @@ export function ExtractionSettings({ onChange, initialData }: ExtractionSettings
|
||||
});
|
||||
}
|
||||
|
||||
// 分发自定义事件,确保更新到所有依赖此事件的组件
|
||||
document.dispatchEvent(
|
||||
new CustomEvent('extraction-fields-updated', {
|
||||
detail: {
|
||||
fields: allFields,
|
||||
fieldsData: {
|
||||
llm_ocr: fields.llm_ocr || [],
|
||||
llm: fields.llm || [],
|
||||
regex: validRegexFields.map(f => f.fieldName.trim())
|
||||
},
|
||||
timestamp: Date.now() // 添加时间戳确保事件能被识别为新事件
|
||||
},
|
||||
})
|
||||
);
|
||||
// 不再使用自定义事件,统一通过Context共享数据
|
||||
|
||||
// 更新上次发送的字段列表和时间
|
||||
lastEventFieldsRef.current = [...allFields];
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,26 @@
|
||||
import { createContext } from 'react';
|
||||
|
||||
/**
|
||||
* 规则上下文类型
|
||||
* 用于在抽取设置和评查设置之间共享数据
|
||||
*/
|
||||
export interface RuleContextType {
|
||||
/**
|
||||
* 抽取的字段列表
|
||||
*/
|
||||
extractionFields: string[];
|
||||
|
||||
/**
|
||||
* 更新字段列表的函数
|
||||
*/
|
||||
updateFields: (fields: string[]) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建规则上下文
|
||||
* 用于在抽取设置和评查设置组件之间共享字段数据
|
||||
*/
|
||||
export const RuleContext = createContext<RuleContextType>({
|
||||
extractionFields: [],
|
||||
updateFields: () => {}
|
||||
});
|
||||
@@ -5,14 +5,12 @@
|
||||
*/
|
||||
|
||||
import { RemixBrowser } from "@remix-run/react";
|
||||
import { startTransition, StrictMode } from "react";
|
||||
import { startTransition } from "react";
|
||||
import { hydrateRoot } from "react-dom/client";
|
||||
|
||||
startTransition(() => {
|
||||
hydrateRoot(
|
||||
document,
|
||||
<StrictMode>
|
||||
<RemixBrowser />
|
||||
</StrictMode>
|
||||
);
|
||||
});
|
||||
|
||||
+551
-120
@@ -2,7 +2,8 @@ import { type MetaFunction } from "@remix-run/node";
|
||||
import { useState, useEffect } from "react";
|
||||
import { BasicInfo } from "~/components/rules/new/BasicInfo";
|
||||
import { ExtractionSettings } from "~/components/rules/new/ExtractionSettings";
|
||||
import { ReviewSettings, RuleContext } from "~/components/rules/new/ReviewSettings";
|
||||
import { ReviewSettings } from "~/components/rules/new/ReviewSettings";
|
||||
import { RuleContext } from "~/contexts/RuleContext";
|
||||
import { ActionButtons } from "~/components/rules/new/ActionButtons";
|
||||
import { PageHeader } from "~/components/rules/new/PageHeader";
|
||||
import rulesStyles from "~/styles/rules.css?url";
|
||||
@@ -119,7 +120,10 @@ interface FormDataType {
|
||||
suggestion_message_type: string;
|
||||
post_action: string;
|
||||
action_config: string;
|
||||
type?: string;
|
||||
type: string;
|
||||
evaluation_point_groups_pid: number | null;
|
||||
score: number;
|
||||
scoreDisplay?: string;
|
||||
}
|
||||
|
||||
interface ApiPointData {
|
||||
@@ -135,6 +139,7 @@ interface ApiPointData {
|
||||
content: string;
|
||||
};
|
||||
evaluation_point_groups_id: number | null;
|
||||
evaluation_point_groups_pid?: number | null;
|
||||
extraction_config: ApiExtactionConfigType;
|
||||
evaluation_config: {
|
||||
logicType?: string;
|
||||
@@ -152,6 +157,21 @@ interface ApiPointData {
|
||||
post_action: string;
|
||||
action_config: string;
|
||||
type?: string;
|
||||
meta?: {
|
||||
type: string;
|
||||
pid?: number;
|
||||
[key: string]: unknown;
|
||||
};
|
||||
score?: number;
|
||||
scoreDisplay?: string;
|
||||
}
|
||||
|
||||
// API响应数据类型
|
||||
interface ApiResponse {
|
||||
code?: number;
|
||||
msg?: string;
|
||||
data?: Array<Record<string, unknown>> | Record<string, unknown>;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export default function RuleNew() {
|
||||
@@ -160,6 +180,7 @@ export default function RuleNew() {
|
||||
const [extractionFields, setExtractionFields] = useState<string[]>([]);
|
||||
const [isEditMode, setIsEditMode] = useState<boolean>(false);
|
||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||
const [evaluationPointGroups, setEvaluationPointGroups] = useState<Array<{id: number, pid: number, code: string, name: string, is_enabled: boolean}>>([]);
|
||||
const [formData, setFormData] = useState<FormDataType>({
|
||||
// 基本信息字段
|
||||
name: '',
|
||||
@@ -174,6 +195,7 @@ export default function RuleNew() {
|
||||
},
|
||||
evaluation_point_groups_id: null,
|
||||
type: '',
|
||||
evaluation_point_groups_pid: null,
|
||||
|
||||
// 抽取设置
|
||||
extraction_config: {
|
||||
@@ -211,7 +233,10 @@ export default function RuleNew() {
|
||||
|
||||
// 评查后动作
|
||||
post_action: 'none',
|
||||
action_config: ''
|
||||
action_config: '',
|
||||
|
||||
// 分数
|
||||
score: 0
|
||||
});
|
||||
|
||||
// 页面加载时检查URL中是否有ID参数,如果有则为编辑模式
|
||||
@@ -223,7 +248,95 @@ export default function RuleNew() {
|
||||
setIsEditMode(true);
|
||||
fetchEvaluationPoint(parseInt(id));
|
||||
}
|
||||
}, [location]);
|
||||
|
||||
// 获取评查点组数据
|
||||
fetchEvaluationPointGroups();
|
||||
}, [location.search]);
|
||||
|
||||
// 获取评查点组数据
|
||||
const fetchEvaluationPointGroups = async () => {
|
||||
try {
|
||||
console.log("获取评查点组数据");
|
||||
const response = await fetch("http://127.0.0.1:9000/admin/evaluation_point_groups", {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error(`API响应错误: ${response.status}`, errorText);
|
||||
throw new Error(`获取评查点组数据失败: ${response.status} - ${errorText}`);
|
||||
}
|
||||
|
||||
const responseData = await response.json();
|
||||
console.log("评查点组数据原始响应:", responseData);
|
||||
|
||||
// 适配API响应格式
|
||||
let groupsData: Array<{id: number, pid: number, code: string, name: string, is_enabled: boolean}> = [];
|
||||
if (responseData && typeof responseData === 'object') {
|
||||
if (responseData.code === 0 && responseData.data) {
|
||||
// 新API格式
|
||||
groupsData = Array.isArray(responseData.data) ? responseData.data : [responseData.data];
|
||||
} else if (Array.isArray(responseData)) {
|
||||
// 旧API格式
|
||||
groupsData = responseData;
|
||||
} else if (responseData.data && Array.isArray(responseData.data)) {
|
||||
// 旧API格式
|
||||
groupsData = responseData.data;
|
||||
}
|
||||
}
|
||||
|
||||
// 确保所有项都有必需的字段
|
||||
groupsData = groupsData.filter(item =>
|
||||
item && typeof item === 'object' &&
|
||||
'id' in item &&
|
||||
'pid' in item &&
|
||||
'code' in item &&
|
||||
'name' in item
|
||||
);
|
||||
|
||||
console.log("处理后的评查点组数据:", groupsData);
|
||||
console.log("根级评查点类型:", groupsData.filter(group => group.pid === 0));
|
||||
setEvaluationPointGroups(groupsData);
|
||||
|
||||
// 如果表单数据已加载但类型未设置,尝试根据evaluation_point_groups_pid设置类型
|
||||
if (formData.id && !formData.type && formData.evaluation_point_groups_pid) {
|
||||
console.log("评查点组数据加载后更新类型,当前pid:", formData.evaluation_point_groups_pid);
|
||||
|
||||
const typeGroup = groupsData.find(group => group.id === formData.evaluation_point_groups_pid);
|
||||
if (typeGroup) {
|
||||
console.log("找到对应的类型组:", typeGroup);
|
||||
setFormData(prevData => ({
|
||||
...prevData,
|
||||
type: typeGroup.code
|
||||
}));
|
||||
console.log("根据评查点类型ID更新类型为:", typeGroup.code);
|
||||
} else if (formData.evaluation_point_groups_id) {
|
||||
// 通过规则组查找类型
|
||||
const selectedGroup = groupsData.find(group => group.id === formData.evaluation_point_groups_id);
|
||||
if (selectedGroup && selectedGroup.pid !== 0) {
|
||||
const parentTypeGroup = groupsData.find(group => group.id === selectedGroup.pid);
|
||||
if (parentTypeGroup) {
|
||||
console.log("通过规则组找到类型组:", parentTypeGroup);
|
||||
setFormData(prevData => ({
|
||||
...prevData,
|
||||
type: parentTypeGroup.code,
|
||||
evaluation_point_groups_pid: parentTypeGroup.id
|
||||
}));
|
||||
console.log("根据规则组更新类型为:", parentTypeGroup.code);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取评查点组数据失败:', error);
|
||||
// 显示错误提示但不影响应用继续使用
|
||||
alert(`获取评查点组数据失败: ${error instanceof Error ? error.message : '未知错误'}\n将使用默认数据`);
|
||||
}
|
||||
};
|
||||
|
||||
// 获取评查点数据
|
||||
const fetchEvaluationPoint = async (id: number) => {
|
||||
@@ -233,7 +346,8 @@ export default function RuleNew() {
|
||||
const response = await fetch(`http://127.0.0.1:9000/admin/evaluation_points?id=eq.${id}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
@@ -246,8 +360,27 @@ export default function RuleNew() {
|
||||
const responseData = await response.json();
|
||||
console.log("API响应数据:", responseData);
|
||||
|
||||
// 处理多种可能的响应格式
|
||||
if (Array.isArray(responseData)) {
|
||||
// 新的API响应格式适配
|
||||
if (responseData && typeof responseData === 'object') {
|
||||
if (responseData.code === 0 && responseData.data) {
|
||||
// 符合新的API响应格式
|
||||
if (Array.isArray(responseData.data)) {
|
||||
if (responseData.data.length > 0) {
|
||||
console.log("处理数组数据的第一项");
|
||||
processPointData(responseData.data[0]);
|
||||
} else {
|
||||
console.error("数据数组为空");
|
||||
throw new Error(`未找到ID为${id}的评查点数据`);
|
||||
}
|
||||
} else if (typeof responseData.data === 'object') {
|
||||
console.log("处理单个对象数据");
|
||||
processPointData(responseData.data);
|
||||
}
|
||||
} else if (responseData.code !== 0) {
|
||||
// API返回错误
|
||||
throw new Error(responseData.msg || `获取数据失败,错误码: ${responseData.code}`);
|
||||
} else if (Array.isArray(responseData)) {
|
||||
// 处理旧API格式:数组形式
|
||||
if (responseData.length > 0) {
|
||||
console.log("数组格式响应,使用第一项");
|
||||
processPointData(responseData[0]);
|
||||
@@ -255,9 +388,8 @@ export default function RuleNew() {
|
||||
console.error("数组为空,未找到数据");
|
||||
throw new Error(`未找到ID为${id}的评查点数据`);
|
||||
}
|
||||
} else if (responseData && typeof responseData === 'object') {
|
||||
// 处理可能的包装对象格式
|
||||
if (responseData.data) {
|
||||
} else if (responseData.data) {
|
||||
// 处理旧API格式:包含data字段的对象
|
||||
console.log("包装对象格式响应,使用data字段");
|
||||
if (Array.isArray(responseData.data)) {
|
||||
if (responseData.data.length > 0) {
|
||||
@@ -273,7 +405,7 @@ export default function RuleNew() {
|
||||
throw new Error('数据格式不正确: data字段不是预期的格式');
|
||||
}
|
||||
} else if (responseData.id) {
|
||||
// 可能是直接返回的单个对象
|
||||
// 处理旧API格式:直接返回的对象
|
||||
console.log("单对象格式响应");
|
||||
processPointData(responseData);
|
||||
} else {
|
||||
@@ -287,6 +419,8 @@ export default function RuleNew() {
|
||||
} catch (error) {
|
||||
console.error('获取评查点数据失败:', error);
|
||||
alert(`获取评查点数据失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
||||
// 获取数据失败时返回上一页
|
||||
navigate(-1);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
@@ -308,6 +442,7 @@ export default function RuleNew() {
|
||||
|
||||
console.log("提取配置:", extractionConfig);
|
||||
console.log("评查配置:", evaluationConfig);
|
||||
console.log("分数:", pointData.score);
|
||||
|
||||
// 提取字段列表,用于规则设置
|
||||
const extractedFields: string[] = [];
|
||||
@@ -335,8 +470,36 @@ export default function RuleNew() {
|
||||
console.log("提取的字段:", extractedFields);
|
||||
setExtractionFields(extractedFields);
|
||||
|
||||
// 提取评查点类型ID
|
||||
let pointGroupPid: number | null = null;
|
||||
if (pointData.evaluation_point_groups_pid !== undefined && pointData.evaluation_point_groups_pid !== null) {
|
||||
// 直接使用字段值
|
||||
pointGroupPid = typeof pointData.evaluation_point_groups_pid === 'number'
|
||||
? pointData.evaluation_point_groups_pid
|
||||
: null;
|
||||
console.log("从evaluation_point_groups_pid获取类型ID:", pointGroupPid);
|
||||
} else if (pointData.meta && typeof pointData.meta === 'object' && pointData.meta.pid !== undefined) {
|
||||
// 从meta中提取
|
||||
pointGroupPid = typeof pointData.meta.pid === 'number'
|
||||
? pointData.meta.pid
|
||||
: null;
|
||||
console.log("从meta.pid获取类型ID:", pointGroupPid);
|
||||
}
|
||||
|
||||
console.log("最终确定的类型ID:", pointGroupPid);
|
||||
|
||||
// 处理分数
|
||||
let pointScore = 0;
|
||||
if (pointData.score !== undefined && pointData.score !== null) {
|
||||
pointScore = typeof pointData.score === 'number' ? pointData.score : 0;
|
||||
console.log("从score字段获取分数:", pointScore);
|
||||
} else if (pointData.meta && typeof pointData.meta === 'object' && pointData.meta.score !== undefined) {
|
||||
pointScore = typeof pointData.meta.score === 'number' ? pointData.meta.score : 0;
|
||||
console.log("从meta.score获取分数:", pointScore);
|
||||
}
|
||||
|
||||
// 构建表单数据
|
||||
const newFormData = {
|
||||
const newFormData: FormDataType = {
|
||||
id: pointData.id,
|
||||
name: pointData.name || '',
|
||||
code: pointData.code || '',
|
||||
@@ -349,7 +512,8 @@ export default function RuleNew() {
|
||||
content: ''
|
||||
},
|
||||
evaluation_point_groups_id: pointData.evaluation_point_groups_id || null,
|
||||
type: pointData.type || '',
|
||||
evaluation_point_groups_pid: pointGroupPid,
|
||||
type: '', // 先置空,稍后根据pid推断
|
||||
|
||||
// 将API数据格式转换为内部使用的格式
|
||||
extraction_config: {
|
||||
@@ -456,11 +620,76 @@ export default function RuleNew() {
|
||||
suggestion_message: pointData.suggestion_message || '',
|
||||
suggestion_message_type: pointData.suggestion_message_type || 'warning',
|
||||
post_action: pointData.post_action || 'none',
|
||||
action_config: pointData.action_config || ''
|
||||
action_config: pointData.action_config || '',
|
||||
score: pointScore,
|
||||
scoreDisplay: pointData.scoreDisplay
|
||||
};
|
||||
|
||||
console.log("设置表单数据:", newFormData);
|
||||
setFormData(newFormData);
|
||||
|
||||
// 根据evaluation_point_groups_pid查找对应的评查点类型
|
||||
if (evaluationPointGroups.length > 0) {
|
||||
console.log("开始根据pid查找对应的评查点类型, pid:", newFormData.evaluation_point_groups_pid);
|
||||
console.log("当前评查点组数据:", evaluationPointGroups);
|
||||
|
||||
// 如果有evaluation_point_groups_pid,直接查找对应的类型组
|
||||
if (newFormData.evaluation_point_groups_pid) {
|
||||
const typeGroup = evaluationPointGroups.find(group => group.id === newFormData.evaluation_point_groups_pid);
|
||||
if (typeGroup) {
|
||||
console.log("找到对应的类型组:", typeGroup);
|
||||
const updatedFormData = {
|
||||
...newFormData,
|
||||
type: typeGroup.code
|
||||
};
|
||||
console.log("根据评查点类型ID设置类型:", typeGroup.code);
|
||||
setFormData(updatedFormData);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果评查点组ID存在,尝试查找对应的类型
|
||||
if (newFormData.evaluation_point_groups_id) {
|
||||
console.log("通过规则组ID查找对应的类型组");
|
||||
const selectedGroup = evaluationPointGroups.find(group => group.id === newFormData.evaluation_point_groups_id);
|
||||
console.log("找到的规则组:", selectedGroup);
|
||||
|
||||
if (selectedGroup && selectedGroup.pid !== 0) {
|
||||
const typeGroup = evaluationPointGroups.find(group => group.id === selectedGroup.pid);
|
||||
console.log("找到的类型组:", typeGroup);
|
||||
|
||||
if (typeGroup && typeGroup.code) {
|
||||
// 更新类型和评查点类型ID
|
||||
const updatedFormData = {
|
||||
...newFormData,
|
||||
type: typeGroup.code,
|
||||
evaluation_point_groups_pid: typeGroup.id
|
||||
};
|
||||
console.log("根据规则组ID设置类型:", typeGroup.code, "和评查点类型ID:", typeGroup.id);
|
||||
setFormData(updatedFormData);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log("无评查点组数据,无法推断类型");
|
||||
|
||||
// 尝试从meta或type字段获取类型信息
|
||||
if (pointData.type) {
|
||||
console.log("从type字段获取类型:", pointData.type);
|
||||
const updatedFormData = {
|
||||
...newFormData,
|
||||
type: pointData.type
|
||||
};
|
||||
setFormData(updatedFormData);
|
||||
} else if (pointData.meta && typeof pointData.meta === 'object' && 'type' in pointData.meta) {
|
||||
console.log("从meta.type字段获取类型:", pointData.meta.type);
|
||||
const updatedFormData = {
|
||||
...newFormData,
|
||||
type: pointData.meta.type as string
|
||||
};
|
||||
setFormData(updatedFormData);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("处理评查点数据时出错:", error);
|
||||
throw new Error(`处理评查点数据时出错: ${error instanceof Error ? error.message : '未知错误'}`);
|
||||
@@ -484,9 +713,14 @@ export default function RuleNew() {
|
||||
const handleExtractionSettingsChange = (data: Record<string, unknown>) => {
|
||||
setFormData(prevData => {
|
||||
// 获取数据
|
||||
const extractionMethod = data.extractionMethod as string;
|
||||
const regexFields = data.regexFields as Array<{ id: string; fieldName: string; regex: string }>;
|
||||
const fields = data.fields as Record<string, string[]>;
|
||||
const allFields = data.allFields as string[];
|
||||
|
||||
if (allFields && allFields.length > 0) {
|
||||
// 更新抽取字段列表,用于在规则设置中使用
|
||||
updateExtractionFields(allFields);
|
||||
}
|
||||
|
||||
// 根据抽取方法更新对应字段
|
||||
const updatedExtractionConfig = { ...prevData.extraction_config };
|
||||
@@ -514,15 +748,22 @@ export default function RuleNew() {
|
||||
|
||||
// 更新提示词设置
|
||||
if (data.promptSettings) {
|
||||
const promptSettings = data.promptSettings as Record<string, unknown>;
|
||||
const promptSettings = data.promptSettings as Record<string, Record<string, unknown>>;
|
||||
|
||||
// 确定当前是处理哪种类型的提示词
|
||||
if (extractionMethod === 'llm_ocr') {
|
||||
updatedExtractionConfig.llm_ocr.prompt_setting.type = promptSettings.type as string || 'system';
|
||||
updatedExtractionConfig.llm_ocr.prompt_setting.template = promptSettings.content as string || '';
|
||||
} else if (extractionMethod === 'llm') {
|
||||
updatedExtractionConfig.llm_vl.prompt_setting.type = promptSettings.type as string || 'system';
|
||||
updatedExtractionConfig.llm_vl.prompt_setting.template = promptSettings.content as string || '';
|
||||
// 更新大模型抽取提示词设置
|
||||
if (promptSettings.llm_ocr) {
|
||||
updatedExtractionConfig.llm_ocr.prompt_setting = {
|
||||
type: promptSettings.llm_ocr.type as string || 'system',
|
||||
template: promptSettings.llm_ocr.content as string || ''
|
||||
};
|
||||
}
|
||||
|
||||
// 更新多模态抽取提示词设置
|
||||
if (promptSettings.llm) {
|
||||
updatedExtractionConfig.llm_vl.prompt_setting = {
|
||||
type: promptSettings.llm.type as string || 'system',
|
||||
template: promptSettings.llm.content as string || ''
|
||||
};
|
||||
}
|
||||
} else {
|
||||
// 兼容旧的API
|
||||
@@ -547,6 +788,13 @@ export default function RuleNew() {
|
||||
}
|
||||
}
|
||||
|
||||
// 记录状态更新到控制台以便调试
|
||||
console.log("抽取设置更新:", {
|
||||
fields: fields,
|
||||
regexFields: regexFields,
|
||||
extractionConfig: updatedExtractionConfig
|
||||
});
|
||||
|
||||
return {
|
||||
...prevData,
|
||||
extraction_config: updatedExtractionConfig
|
||||
@@ -559,6 +807,9 @@ export default function RuleNew() {
|
||||
setFormData(prevData => {
|
||||
const updatedData = { ...prevData };
|
||||
|
||||
// 记录所有收到的数据
|
||||
console.log("评查设置更新数据:", data);
|
||||
|
||||
// 更新规则
|
||||
if (data.rules) {
|
||||
updatedData.evaluation_config.rules = data.rules as Rule[];
|
||||
@@ -600,6 +851,17 @@ export default function RuleNew() {
|
||||
updatedData.action_config = data.action_config as string;
|
||||
}
|
||||
|
||||
// 更新分数
|
||||
if (data.score !== undefined) {
|
||||
const scoreValue = parseFloat(data.score as string);
|
||||
updatedData.score = isNaN(scoreValue) ? 0 : scoreValue;
|
||||
}
|
||||
|
||||
// 更新分数显示值
|
||||
if (data.scoreDisplay !== undefined) {
|
||||
updatedData.scoreDisplay = data.scoreDisplay as string;
|
||||
}
|
||||
|
||||
return updatedData;
|
||||
});
|
||||
};
|
||||
@@ -633,7 +895,9 @@ export default function RuleNew() {
|
||||
}
|
||||
},
|
||||
regex: {
|
||||
fields: (formData.extraction_config.ocr_regex.fields || []).map((field: RegexField) => {
|
||||
fields: (formData.extraction_config.ocr_regex.fields || [])
|
||||
.filter(field => field.fieldName && field.fieldName.trim() !== '')
|
||||
.map((field: RegexField) => {
|
||||
return {
|
||||
field: field.fieldName || '',
|
||||
pattern: field.regex || ''
|
||||
@@ -646,14 +910,16 @@ export default function RuleNew() {
|
||||
const evaluationConfig = {
|
||||
logicType: formData.evaluation_config.logicType || 'and',
|
||||
customLogic: formData.evaluation_config.customLogic || '',
|
||||
rules: (formData.evaluation_config.rules || []).map((rule: Rule) => {
|
||||
rules: (formData.evaluation_config.rules || [])
|
||||
.filter((rule: Rule) => rule.type && rule.type.trim() !== '')
|
||||
.map((rule: Rule) => {
|
||||
let config = {};
|
||||
|
||||
// 根据不同的规则类型生成对应的配置
|
||||
switch (rule.type) {
|
||||
case 'exists':
|
||||
config = {
|
||||
fields: rule.config?.selectedFields || [],
|
||||
fields: rule.config?.fields || [],
|
||||
logic: rule.config?.logicRelation || 'and'
|
||||
};
|
||||
break;
|
||||
@@ -697,7 +963,11 @@ export default function RuleNew() {
|
||||
};
|
||||
break;
|
||||
default:
|
||||
config = rule.config || {};
|
||||
config = { ...rule.config };
|
||||
// 清除辅助字段,避免发送无效数据
|
||||
if (typeof config === 'object' && config !== null) {
|
||||
delete (config as Record<string, unknown>).availableFields;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -713,18 +983,20 @@ export default function RuleNew() {
|
||||
code: formData.code,
|
||||
name: formData.name,
|
||||
evaluation_point_groups_id: formData.evaluation_point_groups_id,
|
||||
evaluation_point_groups_pid: formData.evaluation_point_groups_pid,
|
||||
risk: formData.risk,
|
||||
description: formData.description,
|
||||
is_enabled: isDraft ? false : formData.is_enabled,
|
||||
references_laws: formData.references_laws,
|
||||
extraction_config: extractionConfig,
|
||||
evaluation_config: evaluationConfig,
|
||||
pass_message: formData.pass_message,
|
||||
fail_message: formData.fail_message,
|
||||
suggestion_message: formData.suggestion_message,
|
||||
suggestion_message_type: formData.suggestion_message_type,
|
||||
post_action: formData.post_action,
|
||||
action_config: formData.action_config
|
||||
pass_message: formData.pass_message || '文档检查通过,符合规范要求。',
|
||||
fail_message: formData.fail_message || '文档存在以下问题,请修改后重新提交。',
|
||||
suggestion_message: formData.suggestion_message || '',
|
||||
suggestion_message_type: formData.suggestion_message_type || 'warning',
|
||||
post_action: formData.post_action || 'none',
|
||||
action_config: formData.action_config || '',
|
||||
score: Number(formData.score) // 确保是数字类型
|
||||
};
|
||||
|
||||
// 如果是编辑模式,添加ID
|
||||
@@ -738,29 +1010,67 @@ export default function RuleNew() {
|
||||
return evaluationPointData;
|
||||
};
|
||||
|
||||
// 确定使用的HTTP方法和URL
|
||||
const getEndpointAndMethod = (id?: number) => {
|
||||
let method = 'POST';
|
||||
let endpoint = 'http://127.0.0.1:9000/admin/evaluation_points';
|
||||
|
||||
// 如果是编辑模式,使用PATCH更新现有记录
|
||||
if (id) {
|
||||
method = 'PATCH';
|
||||
endpoint = `http://127.0.0.1:9000/admin/evaluation_points?id=eq.${id}`;
|
||||
}
|
||||
|
||||
return { method, endpoint };
|
||||
};
|
||||
|
||||
// 保存评查点
|
||||
const handleSave = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
|
||||
// 基本验证
|
||||
if (!formData.name || !formData.code) {
|
||||
alert("请填写评查点名称和编码,这些是必填项");
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查评查点类型
|
||||
if (!formData.type) {
|
||||
alert("请选择评查点类型");
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查所属规则组
|
||||
if (!formData.evaluation_point_groups_id) {
|
||||
alert("请选择所属规则组");
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查评查规则
|
||||
if (!formData.evaluation_config.rules || formData.evaluation_config.rules.length === 0 ||
|
||||
!formData.evaluation_config.rules.some(rule => rule.type && rule.type.trim() !== '')) {
|
||||
alert("请至少添加一条有效的评查规则");
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const evaluationPointData = formatDataForApi(formData);
|
||||
console.log("保存数据:", evaluationPointData);
|
||||
|
||||
// 确定使用的HTTP方法和URL
|
||||
let method = 'POST';
|
||||
let endpoint = 'http://127.0.0.1:9000/admin/evaluation_points';
|
||||
|
||||
|
||||
// 如果是编辑模式,使用PATCH更新现有记录
|
||||
if (isEditMode && formData.id) {
|
||||
method = 'PATCH';
|
||||
endpoint = `http://127.0.0.1:9000/admin/evaluation_points?id=eq.${formData.id}`;
|
||||
}
|
||||
|
||||
const { method, endpoint } = getEndpointAndMethod(formData.id);
|
||||
console.log(`发送${method}请求到`, endpoint);
|
||||
|
||||
// 发送数据到API
|
||||
const response = await fetch(endpoint, {
|
||||
method: method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(evaluationPointData)
|
||||
});
|
||||
|
||||
@@ -774,10 +1084,10 @@ export default function RuleNew() {
|
||||
// 尝试解析错误响应为JSON
|
||||
const errorJson = JSON.parse(errorText);
|
||||
console.error("解析的错误JSON:", errorJson);
|
||||
throw new Error(`API响应错误: ${errorJson.msg || response.statusText}`);
|
||||
throw new Error(`保存失败: ${errorJson.msg || response.statusText}`);
|
||||
} catch (parseError) {
|
||||
// 如果无法解析为JSON,使用原始文本
|
||||
throw new Error(`API响应错误: ${response.status} - ${errorText}`);
|
||||
throw new Error(`保存失败: ${response.status} - ${errorText}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -789,40 +1099,8 @@ export default function RuleNew() {
|
||||
const responseData = await response.json();
|
||||
console.log("API响应数据:", responseData);
|
||||
|
||||
// 处理多种可能的响应格式
|
||||
if (Array.isArray(responseData)) {
|
||||
if (responseData.length > 0) {
|
||||
console.log("保存成功 (数组响应):", responseData[0]);
|
||||
alert("保存成功!");
|
||||
navigate('/rules');
|
||||
} else {
|
||||
console.warn("响应数组为空");
|
||||
alert("操作已完成,但服务器未返回数据");
|
||||
navigate('/rules');
|
||||
}
|
||||
} else if (responseData && typeof responseData === 'object') {
|
||||
if (responseData.data) {
|
||||
console.log("保存成功 (带data字段):", responseData.data);
|
||||
alert("保存成功!");
|
||||
navigate('/rules');
|
||||
} else if (responseData.id) {
|
||||
console.log("保存成功 (直接对象):", responseData);
|
||||
alert("保存成功!");
|
||||
navigate('/rules');
|
||||
} else if (responseData.code === 0) {
|
||||
console.log("保存成功 (带code字段):", responseData);
|
||||
alert("保存成功!");
|
||||
navigate('/rules');
|
||||
} else {
|
||||
console.warn("响应对象格式不符合预期", responseData);
|
||||
alert("操作已完成,但返回的数据格式不符合预期");
|
||||
navigate('/rules');
|
||||
}
|
||||
} else {
|
||||
console.warn("响应不是数组或对象", responseData);
|
||||
alert("操作已完成,但返回的数据格式不符合预期");
|
||||
navigate('/rules');
|
||||
}
|
||||
// 处理响应
|
||||
await handleApiResponse(responseData, isEditMode);
|
||||
} else {
|
||||
// 非JSON响应
|
||||
const text = await response.text();
|
||||
@@ -842,32 +1120,59 @@ export default function RuleNew() {
|
||||
const handleSaveDraft = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
|
||||
// 基本验证
|
||||
if (!formData.name || !formData.code) {
|
||||
alert("请填写评查点名称和编码,这些是必填项");
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查评查点类型
|
||||
if (!formData.type) {
|
||||
alert("请选择评查点类型");
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查所属规则组
|
||||
if (!formData.evaluation_point_groups_id) {
|
||||
alert("请选择所属规则组");
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const draftData = formatDataForApi(formData, true);
|
||||
console.log("保存草稿数据:", draftData);
|
||||
|
||||
// 确定使用的HTTP方法和URL
|
||||
let method = 'POST';
|
||||
let endpoint = 'http://127.0.0.1:9000/admin/evaluation_points';
|
||||
|
||||
|
||||
// 如果是编辑模式,使用PATCH更新现有记录
|
||||
if (isEditMode && formData.id) {
|
||||
method = 'PATCH';
|
||||
endpoint = `http://127.0.0.1:9000/admin/evaluation_points?id=eq.${formData.id}`;
|
||||
}
|
||||
|
||||
const { method, endpoint } = getEndpointAndMethod(formData.id);
|
||||
console.log(`发送${method}请求到`, endpoint);
|
||||
|
||||
// 发送数据到API
|
||||
const response = await fetch(endpoint, {
|
||||
method: method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(draftData)
|
||||
});
|
||||
|
||||
// 输出完整响应信息
|
||||
console.log("响应状态:", response.status, response.statusText);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error(`API响应错误: ${response.status}`, errorText);
|
||||
throw new Error(`API响应错误: ${response.status} - ${errorText}`);
|
||||
try {
|
||||
// 尝试解析错误响应为JSON
|
||||
const errorJson = JSON.parse(errorText);
|
||||
console.error("解析的错误JSON:", errorJson);
|
||||
throw new Error(`保存失败: ${errorJson.msg || response.statusText}`);
|
||||
} catch (parseError) {
|
||||
// 如果无法解析为JSON,使用原始文本
|
||||
throw new Error(`保存失败: ${response.status} - ${errorText}`);
|
||||
}
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type');
|
||||
@@ -878,30 +1183,110 @@ export default function RuleNew() {
|
||||
const responseData = await response.json();
|
||||
console.log("API响应数据:", responseData);
|
||||
|
||||
// 处理多种可能的响应格式
|
||||
// 处理响应
|
||||
await handleApiResponse(responseData, isEditMode, true);
|
||||
} else {
|
||||
// 非JSON响应
|
||||
const text = await response.text();
|
||||
console.log("非JSON响应:", text);
|
||||
alert("草稿已保存!");
|
||||
navigate('/rules');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('保存草稿失败:', error);
|
||||
alert(`保存草稿失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 处理API响应
|
||||
const handleApiResponse = async (
|
||||
responseData:
|
||||
| ApiResponse
|
||||
| Array<{ id: number; [key: string]: unknown }>,
|
||||
isEditMode: boolean,
|
||||
isDraft: boolean = false
|
||||
) => {
|
||||
// 适配新的API响应格式
|
||||
if (responseData && typeof responseData === 'object') {
|
||||
// 符合新的API规范
|
||||
if ('code' in responseData && responseData.code === 0) {
|
||||
console.log(`${isDraft ? '草稿' : ''}保存成功 (新API格式):`, responseData.data);
|
||||
alert(`${isDraft ? '草稿' : ''}保存成功!`);
|
||||
|
||||
// 根据操作类型重定向
|
||||
if (isEditMode) {
|
||||
// 编辑模式,保留在当前编辑页面
|
||||
navigate(`/rules/new?id=${formData.id}`);
|
||||
} else {
|
||||
// 创建模式,跳转到新创建数据的编辑页面
|
||||
let newId: number | undefined;
|
||||
|
||||
if (responseData.data && Array.isArray(responseData.data) && responseData.data.length > 0) {
|
||||
newId = responseData.data[0].id as number;
|
||||
} else if (responseData.data && typeof responseData.data === 'object' && 'id' in responseData.data) {
|
||||
newId = responseData.data.id as number;
|
||||
}
|
||||
|
||||
if (newId) {
|
||||
navigate(`/rules/new?id=${newId}`);
|
||||
} else {
|
||||
// 无法获取ID,返回列表页
|
||||
navigate('/rules');
|
||||
}
|
||||
}
|
||||
return;
|
||||
} else if ('code' in responseData && responseData.code !== 0) {
|
||||
// API返回错误
|
||||
console.warn("API返回错误:", responseData.msg);
|
||||
throw new Error(responseData.msg as string || "操作失败");
|
||||
}
|
||||
}
|
||||
|
||||
// 兼容处理旧的响应格式
|
||||
if (Array.isArray(responseData)) {
|
||||
if (responseData.length > 0) {
|
||||
console.log("保存草稿成功 (数组响应):", responseData[0]);
|
||||
alert("草稿保存成功!");
|
||||
console.log(`${isDraft ? '草稿' : ''}保存成功 (数组响应):`, responseData[0]);
|
||||
alert(`${isDraft ? '草稿' : ''}保存成功!`);
|
||||
|
||||
// 如果是创建,跳转到编辑页面
|
||||
if (!isEditMode && responseData[0].id) {
|
||||
navigate(`/rules/new?id=${responseData[0].id}`);
|
||||
} else {
|
||||
navigate('/rules');
|
||||
}
|
||||
} else {
|
||||
console.warn("响应数组为空");
|
||||
alert("操作已完成,但服务器未返回数据");
|
||||
navigate('/rules');
|
||||
}
|
||||
} else if (responseData && typeof responseData === 'object') {
|
||||
if (responseData.data) {
|
||||
console.log("保存草稿成功 (带data字段):", responseData.data);
|
||||
alert("草稿保存成功!");
|
||||
if ('data' in responseData && responseData.data) {
|
||||
console.log(`${isDraft ? '草稿' : ''}保存成功 (带data字段):`, responseData.data);
|
||||
alert(`${isDraft ? '草稿' : ''}保存成功!`);
|
||||
|
||||
let newId: number | undefined;
|
||||
if (Array.isArray(responseData.data) && responseData.data.length > 0 && 'id' in responseData.data[0]) {
|
||||
newId = responseData.data[0].id as number;
|
||||
} else if (typeof responseData.data === 'object' && 'id' in responseData.data) {
|
||||
newId = responseData.data.id as number;
|
||||
}
|
||||
|
||||
if (!isEditMode && newId) {
|
||||
navigate(`/rules/new?id=${newId}`);
|
||||
} else {
|
||||
navigate('/rules');
|
||||
} else if (responseData.id) {
|
||||
console.log("保存草稿成功 (直接对象):", responseData);
|
||||
alert("草稿保存成功!");
|
||||
navigate('/rules');
|
||||
} else if (responseData.code === 0) {
|
||||
console.log("保存草稿成功 (带code字段):", responseData);
|
||||
alert("草稿保存成功!");
|
||||
}
|
||||
} else if ('id' in responseData && responseData.id) {
|
||||
console.log(`${isDraft ? '草稿' : ''}保存成功 (直接对象):`, responseData);
|
||||
alert(`${isDraft ? '草稿' : ''}保存成功!`);
|
||||
|
||||
if (!isEditMode) {
|
||||
navigate(`/rules/new?id=${responseData.id as number}`);
|
||||
} else {
|
||||
navigate('/rules');
|
||||
}
|
||||
} else {
|
||||
console.warn("响应对象格式不符合预期", responseData);
|
||||
alert("操作已完成,但返回的数据格式不符合预期");
|
||||
@@ -912,21 +1297,61 @@ export default function RuleNew() {
|
||||
alert("操作已完成,但返回的数据格式不符合预期");
|
||||
navigate('/rules');
|
||||
}
|
||||
} else {
|
||||
// 非JSON响应
|
||||
const text = await response.text();
|
||||
console.log("非JSON响应:", text);
|
||||
alert("操作已完成!");
|
||||
navigate('/rules');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('保存草稿失败:', error);
|
||||
alert(`保存草稿失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 当评查点组数据和表单数据都加载完成后,确保类型信息被正确设置
|
||||
useEffect(() => {
|
||||
// 仅在编辑模式下,且表单数据已加载,评查点组数据也已加载的情况下执行
|
||||
if (
|
||||
isEditMode &&
|
||||
formData.id &&
|
||||
evaluationPointGroups.length > 0 &&
|
||||
(!formData.type || formData.type === '')
|
||||
) {
|
||||
console.log("检测到编辑模式下类型未设置,尝试自动设置类型");
|
||||
|
||||
// 首先尝试通过evaluation_point_groups_pid设置类型
|
||||
if (formData.evaluation_point_groups_pid) {
|
||||
const typeGroup = evaluationPointGroups.find(
|
||||
group => group.id === formData.evaluation_point_groups_pid
|
||||
);
|
||||
|
||||
if (typeGroup) {
|
||||
console.log("通过评查点类型ID找到类型组:", typeGroup);
|
||||
setFormData(prevData => ({
|
||||
...prevData,
|
||||
type: typeGroup.code
|
||||
}));
|
||||
console.log("自动设置类型为:", typeGroup.code);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果无法通过evaluation_point_groups_pid设置,尝试通过evaluation_point_groups_id设置
|
||||
if (formData.evaluation_point_groups_id) {
|
||||
const ruleGroup = evaluationPointGroups.find(
|
||||
group => group.id === formData.evaluation_point_groups_id
|
||||
);
|
||||
|
||||
if (ruleGroup && ruleGroup.pid && ruleGroup.pid !== 0) {
|
||||
const parentGroup = evaluationPointGroups.find(
|
||||
group => group.id === ruleGroup.pid
|
||||
);
|
||||
|
||||
if (parentGroup) {
|
||||
console.log("通过规则组找到父级类型组:", parentGroup);
|
||||
setFormData(prevData => ({
|
||||
...prevData,
|
||||
type: parentGroup.code,
|
||||
evaluation_point_groups_pid: parentGroup.id
|
||||
}));
|
||||
console.log("自动设置类型为:", parentGroup.code);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [isEditMode, formData.id, formData.type, formData.evaluation_point_groups_id, formData.evaluation_point_groups_pid, evaluationPointGroups]);
|
||||
|
||||
return (
|
||||
<div className="container">
|
||||
<PageHeader
|
||||
@@ -942,7 +1367,11 @@ export default function RuleNew() {
|
||||
) : (
|
||||
<>
|
||||
<div className="mb-8">
|
||||
<BasicInfo onChange={handleBasicInfoChange} initialData={formData} />
|
||||
<BasicInfo
|
||||
onChange={handleBasicInfoChange}
|
||||
initialData={formData}
|
||||
evaluationPointGroups={evaluationPointGroups}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="mb-8">
|
||||
@@ -971,7 +1400,9 @@ export default function RuleNew() {
|
||||
suggestion_message: formData.suggestion_message,
|
||||
suggestion_message_type: formData.suggestion_message_type,
|
||||
post_action: formData.post_action,
|
||||
action_config: formData.action_config
|
||||
action_config: formData.action_config,
|
||||
score: formData.score,
|
||||
scoreDisplay: formData.scoreDisplay
|
||||
}}
|
||||
/>
|
||||
</RuleContext.Provider>
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* 工具函数集合
|
||||
* 包含字段处理、防抖等通用功能
|
||||
*/
|
||||
|
||||
/**
|
||||
* 处理字段名,去除类型后缀
|
||||
* 例如: "字段名_类型" -> "字段名"
|
||||
*/
|
||||
export function processFieldName(field: string): string {
|
||||
if (field.includes('_')) {
|
||||
return field.split('_')[0]; // 只保留类型前面的字段名
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理字段数组,去除类型后缀并去重
|
||||
*/
|
||||
export function processFieldNames(fields: string[]): string[] {
|
||||
// 处理字段,去掉类型后缀
|
||||
const processedFields = fields.map(processFieldName);
|
||||
|
||||
// 去重并返回
|
||||
return [...new Set(processedFields)];
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建防抖函数
|
||||
* @param fn 要执行的函数
|
||||
* @param delay 延迟时间(毫秒)
|
||||
*/
|
||||
export function debounce<T extends (...args: unknown[]) => unknown>(
|
||||
fn: T,
|
||||
delay: number
|
||||
): (...args: Parameters<T>) => void {
|
||||
let timer: NodeJS.Timeout | null = null;
|
||||
|
||||
return function(...args: Parameters<T>) {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
|
||||
timer = setTimeout(() => {
|
||||
fn(...args);
|
||||
timer = null;
|
||||
}, delay);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较两个数组是否有实质性不同
|
||||
* 用于避免不必要的状态更新
|
||||
*/
|
||||
export function areArraysDifferent<T>(arr1: T[], arr2: T[]): boolean {
|
||||
return JSON.stringify(arr1) !== JSON.stringify(arr2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找两个数组之间的差异项
|
||||
* @returns 包含新增和删除项的对象
|
||||
*/
|
||||
export function getArrayDifference<T>(current: T[], previous: T[]): { added: T[], removed: T[] } {
|
||||
const added = current.filter(item => !previous.includes(item));
|
||||
const removed = previous.filter(item => !current.includes(item));
|
||||
|
||||
return { added, removed };
|
||||
}
|
||||
Reference in New Issue
Block a user