Commit efae372f by weiyudumei

fix: 修复自动提交逻辑,确保流式返回时能正常触发自动提交并添加调试日志

parent 7db07d6f
......@@ -57,24 +57,41 @@ export const CardWorkflowForm: React.FC<CardWorkflowFormProps> = ({
if (answer.firstQaFlag !== true) {
hasAutoSubmittedRef.current = false
lastFirstQaFlagRef.current = false // 重置记录的值,确保下次变为 true 时能正确触发
} else {
// 如果 firstQaFlag 为 true,初始化 lastFirstQaFlagRef(如果还没有设置)
// 这样可以处理组件首次渲染时 firstQaFlag 已经是 true 的情况
if (lastFirstQaFlagRef.current === undefined) {
lastFirstQaFlagRef.current = false // 设置为 false,让自动提交逻辑能正确触发
}
}
}, [attachment, answer.firstQaFlag, searchParams, textFields])
// 如果 firstQaFlag 为 true,自动提交(仅对流式返回的新记录,历史记录不会渲染此组件)
useEffect(() => {
// 检查 firstQaFlag 是否从 false/undefined 变为 true(首次变为 true)
const isFirstQaFlagChangedToTrue = answer.firstQaFlag === true && lastFirstQaFlagRef.current !== true
// 更新记录的值
lastFirstQaFlagRef.current = answer.firstQaFlag
// 如果 firstQaFlag 为 true,尝试自动提交(不检查是否从 false 变为 true,因为组件可能是在 firstQaFlag 已经是 true 时才渲染的)
if (answer.firstQaFlag === true && !hasAutoSubmittedRef.current && !isSubmitting && !isCompleted) {
// 检查是否有字段(textFields 不为空)
if (textFields.length === 0) {
console.log('[CardWorkflowForm] 自动提交:textFields 为空,跳过')
return
}
// 只在 firstQaFlag 首次变为 true 且尚未自动提交时执行
if (isFirstQaFlagChangedToTrue && !hasAutoSubmittedRef.current && !isSubmitting && !isCompleted) {
// 检查是否所有字段都有值(从 URL query 中读取的)
const allFieldsHaveValues = textFields.every((field) => {
const queryValue = searchParams.get(field.key)
return queryValue && queryValue.trim() !== ''
})
console.log('[CardWorkflowForm] 自动提交检查:', {
firstQaFlag: answer.firstQaFlag,
hasAutoSubmitted: hasAutoSubmittedRef.current,
isSubmitting,
isCompleted,
textFieldsCount: textFields.length,
allFieldsHaveValues,
textFields: textFields.map(f => ({ key: f.key, label: f.label })),
})
if (allFieldsHaveValues) {
hasAutoSubmittedRef.current = true
// 构建 paramMap
......@@ -89,12 +106,21 @@ export const CardWorkflowForm: React.FC<CardWorkflowFormProps> = ({
const recordId = answer.recordId
const workFlowSessionId = answer.workFlowSessionId
console.log('[CardWorkflowForm] 自动提交:准备提交', { paramMap, recordId, workFlowSessionId })
// 延迟一下再提交,确保表单值已经设置完成
setTimeout(() => {
console.log('[CardWorkflowForm] 自动提交:执行提交')
onSubmitWorkflowRef.current(paramMap, recordId, workFlowSessionId)
}, 100)
} else {
console.log('[CardWorkflowForm] 自动提交:字段值不完整,跳过', {
fields: textFields.map(f => ({ key: f.key, value: searchParams.get(f.key) })),
})
}
}
// 更新记录的值
lastFirstQaFlagRef.current = answer.firstQaFlag
// 移除 onSubmitWorkflow 作为依赖项,使用 useRef 保存引用,避免函数引用变化导致重复触发
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [answer.firstQaFlag, isSubmitting, isCompleted, searchParams, textFields])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment