Commit 36e5fdc6 by weiyudumei

feat: 工作流卡片支持 firstQaFlag 自动填充和 workFlowSeeionId 传递

- 添加 firstQaFlag 字段支持,当为 true 时从 URL query 自动填充表单并自动提交
- 添加 workFlowSeeionId 字段,处理模式与 recordId 一致
- 第一次调用返回 workFlowSeeionId,后续调用传递上次返回的值
- 更新 CardWorkflowForm、Chat、TacticsChat 相关逻辑
parent ef46856d
......@@ -198,7 +198,7 @@ export const Chat: React.FC = () => {
question: string,
productCode?: string,
toolId?: string,
extraParams?: { paramMap?: Record<string, string>, recordId?: string, recordType?: string },
extraParams?: { paramMap?: Record<string, string>, recordId?: string, recordType?: string, workFlowSeeionId?: string },
) => {
// 优先读取缓存中的 toolId,再回退到传参或 Redux
const sessionToolId = sessionStorage.getItem('currentToolId') ?? undefined
......@@ -253,13 +253,16 @@ export const Chat: React.FC = () => {
toolId: resolvedToolId ?? '',
}
// 如果是 workflow 提交,添加 paramMap 和 recordId
// 如果是 workflow 提交,添加 paramMap、recordId 和 workFlowSeeionId
if (extraParams?.paramMap) {
requestBody.paramMap = extraParams.paramMap
}
if (extraParams?.recordId) {
requestBody.recordId = extraParams.recordId
}
if (extraParams?.workFlowSeeionId) {
requestBody.workFlowSeeionId = extraParams.workFlowSeeionId
}
// 如果传入了 recordType,优先使用传入的值(workflow 提交场景)
if (extraParams?.recordType) {
......@@ -398,11 +401,12 @@ export const Chat: React.FC = () => {
}
/** 处理 workflow 表单提交 */
const handleWorkflowSubmit = async (paramMap: Record<string, string>, recordId?: string) => {
// workflow 提交时,question 可以为空字符串,但需要传递 paramMap 和 recordId
const handleWorkflowSubmit = async (paramMap: Record<string, string>, recordId?: string, workFlowSeeionId?: string) => {
// workflow 提交时,question 可以为空字符串,但需要传递 paramMap、recordId 和 workFlowSeeionId
await handleSubmitQuestion('', undefined, undefined, {
paramMap,
recordId,
workFlowSeeionId,
recordType: 'A01',
})
}
......
import React, { useEffect, useState } from 'react'
import React, { useEffect, useMemo, useRef, useState } from 'react'
import { Button, Input } from '@heroui/react'
import { useSearchParams } from 'react-router-dom'
import type { Answer, Attachment } from '@/types/chat'
interface CardWorkflowFormProps {
attachment: Attachment
answer: Answer
onSubmitWorkflow: (paramMap: Record<string, string>, recordId?: string) => void
onSubmitWorkflow: (paramMap: Record<string, string>, recordId?: string, workFlowSeeionId?: string) => void
isSubmitting?: boolean
isCompleted?: boolean
}
......@@ -18,18 +19,68 @@ export const CardWorkflowForm: React.FC<CardWorkflowFormProps> = ({
isCompleted = false,
}) => {
const [formValues, setFormValues] = useState<Record<string, string>>({})
const [searchParams] = useSearchParams()
const hasAutoSubmittedRef = useRef(false) // 防止重复自动提交
// 获取 text 类型的字段
const textFields = attachment.content?.fieldList?.filter(field => field.type === 'text') || []
// 获取 text 类型的字段,使用 useMemo 稳定引用
const textFields = useMemo(() => {
return attachment.content?.fieldList?.filter(field => field.type === 'text') || []
}, [attachment.content?.fieldList])
// 初始化表单值
// 初始化表单值,如果 firstQaFlag 为 true,从 URL query 中读取值
useEffect(() => {
const initialValues: Record<string, string> = {}
textFields.forEach((field) => {
initialValues[field.key] = ''
})
if (answer.firstQaFlag === true) {
// 从 URL query 参数中读取值,匹配 fieldList 中的 key
textFields.forEach((field) => {
const queryValue = searchParams.get(field.key)
initialValues[field.key] = queryValue || ''
})
}
else {
// 正常初始化,所有字段为空
textFields.forEach((field) => {
initialValues[field.key] = ''
})
}
setFormValues(initialValues)
}, [attachment])
// 重置自动提交标志,当 firstQaFlag 变化时重新评估
if (answer.firstQaFlag !== true) {
hasAutoSubmittedRef.current = false
}
}, [attachment, answer.firstQaFlag, searchParams, textFields])
// 如果 firstQaFlag 为 true,自动提交
useEffect(() => {
if (answer.firstQaFlag === true && !hasAutoSubmittedRef.current && !isSubmitting && !isCompleted) {
// 检查是否所有字段都有值(从 URL query 中读取的)
const allFieldsHaveValues = textFields.every((field) => {
const queryValue = searchParams.get(field.key)
return queryValue && queryValue.trim() !== ''
})
if (allFieldsHaveValues) {
hasAutoSubmittedRef.current = true
// 构建 paramMap
const paramMap: Record<string, string> = {}
textFields.forEach((field) => {
const queryValue = searchParams.get(field.key)
paramMap[field.key] = queryValue || ''
})
// 获取 recordId 和 workFlowSeeionId(从 answer 中获取,第一次可能没有)
const recordId = answer.recordId
const workFlowSeeionId = answer.workFlowSeeionId
// 延迟一下再提交,确保表单值已经设置完成
setTimeout(() => {
onSubmitWorkflow(paramMap, recordId, workFlowSeeionId)
}, 100)
}
}
}, [answer.firstQaFlag, answer.recordId, answer.workFlowSeeionId, isSubmitting, isCompleted, searchParams, textFields, onSubmitWorkflow])
const handleInputChange = (key: string, value: string) => {
setFormValues(prev => ({
......@@ -45,10 +96,11 @@ export const CardWorkflowForm: React.FC<CardWorkflowFormProps> = ({
paramMap[field.key] = formValues[field.key] || ''
})
// 获取 recordId(从 answer 中获取,第一次可能没有)
// 获取 recordId 和 workFlowSeeionId(从 answer 中获取,第一次可能没有)
const recordId = answer.recordId
const workFlowSeeionId = answer.workFlowSeeionId
onSubmitWorkflow(paramMap, recordId)
onSubmitWorkflow(paramMap, recordId, workFlowSeeionId)
}
// 检查是否有 text 类型的字段
......@@ -65,7 +117,7 @@ export const CardWorkflowForm: React.FC<CardWorkflowFormProps> = ({
label={field.label}
value={formValues[field.key] || ''}
onChange={e => handleInputChange(field.key, e.target.value)}
isDisabled={isSubmitting || isCompleted}
isDisabled={isSubmitting || isCompleted || (answer.firstQaFlag === true && hasAutoSubmittedRef.current)}
variant="bordered"
className="w-full"
/>
......@@ -74,7 +126,7 @@ export const CardWorkflowForm: React.FC<CardWorkflowFormProps> = ({
<Button
color="primary"
onPress={handleSubmit}
isDisabled={isSubmitting || isCompleted}
isDisabled={isSubmitting || isCompleted || (answer.firstQaFlag === true && hasAutoSubmittedRef.current)}
isLoading={isSubmitting}
className="mt-[8px]"
>
......
......@@ -17,7 +17,7 @@ interface ChatAnswerAttachmentProps {
answer: Answer
isLastAnswer?: boolean
onSubmitQuestion?: (question: string, productCode?: string) => void
onWorkflowSubmit?: (paramMap: Record<string, string>, recordId?: string) => void
onWorkflowSubmit?: (paramMap: Record<string, string>, recordId?: string, workFlowSeeionId?: string) => void
isWorkflowSubmitting?: boolean
isAsking?: boolean
}
......
......@@ -339,7 +339,7 @@ export const TacticsChat: React.FC = () => {
question: string | undefined,
productCode?: string,
toolId?: string,
extra?: { busiType?: string, recordType?: string, numberType?: string, includeQuestion?: boolean, includeTacticsMeta?: boolean, includeUserMeta?: boolean, includeOrderMeta?: boolean, isReanalyze?: boolean, paramMap?: Record<string, string>, recordId?: string },
extra?: { busiType?: string, recordType?: string, numberType?: string, includeQuestion?: boolean, includeTacticsMeta?: boolean, includeUserMeta?: boolean, includeOrderMeta?: boolean, isReanalyze?: boolean, paramMap?: Record<string, string>, recordId?: string, workFlowSeeionId?: string },
) => {
// 优先读取缓存中的 toolId,再回退到传参
const sessionToolId = sessionStorage.getItem('currentToolId') ?? undefined
......@@ -486,13 +486,16 @@ export const TacticsChat: React.FC = () => {
if (shouldSendQuestion || (question && extra?.includeQuestion === false)) {
requestBody.question = question ?? ''
}
// 如果是 workflow 提交,添加 paramMap 和 recordId
// 如果是 workflow 提交,添加 paramMap、recordId 和 workFlowSeeionId
if (extra?.paramMap) {
requestBody.paramMap = extra.paramMap
}
if (extra?.recordId) {
requestBody.recordId = extra.recordId
}
if (extra?.workFlowSeeionId) {
requestBody.workFlowSeeionId = extra.workFlowSeeionId
}
// 调试日志:检查最终请求参数
console.log('[TacticsChat] handleSubmitQuestion: final requestBody', {
workOrderIds: requestBody.workOrderIds,
......@@ -615,12 +618,13 @@ export const TacticsChat: React.FC = () => {
}
/** 处理 workflow 表单提交 */
const handleWorkflowSubmit = async (paramMap: Record<string, string>, recordId?: string) => {
// workflow 提交时,question 可以为空字符串,但需要传递 paramMap 和 recordId
const handleWorkflowSubmit = async (paramMap: Record<string, string>, recordId?: string, workFlowSeeionId?: string) => {
// workflow 提交时,question 可以为空字符串,但需要传递 paramMap、recordId 和 workFlowSeeionId
await handleSubmitQuestion(undefined, undefined, undefined, {
includeQuestion: false,
paramMap,
recordId,
workFlowSeeionId,
recordType: 'A01',
})
}
......
......@@ -81,6 +81,8 @@ export interface Answer {
ordType?: string
extra?: AnswerExtra
qaTime?: number | string
firstQaFlag?: boolean
workFlowSeeionId?: string
}
export interface OriginalRecord {
......
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