Commit f7abcbf9 by Liu

fix:更新接口传参方式,不存入sesstionStorage

parent 13311e18
......@@ -73,17 +73,15 @@ export const TacticsChat: React.FC = () => {
const createdFromClearRef = useRef(false)
const lastSentQuestionRef = useRef<string>('')
const abortControllerRef = useRef<AbortController | null>(null)
// 读取 from=tactics 的额外参数
const tacticsMetaFromState = (location.state as { tacticsMeta?: any } | null)?.tacticsMeta
const tacticsMetaFromQuery = useMemo(() => {
// 读取 from=tactics 的额外参数(直接从 query 读取,不再使用 sessionStorage)
const tacticsMeta = useMemo(() => {
const from = searchParams.get('from')
const version = searchParams.get('version')
if (from !== 'tactics') {
return undefined
}
const taskId = searchParams.get('taskId') || undefined
return {
version,
version: searchParams.get('version'),
taskId,
pinBeginTime: searchParams.get('pinBeginTime') || undefined,
pinEndTime: searchParams.get('pinEndTime') || undefined,
......@@ -95,25 +93,9 @@ export const TacticsChat: React.FC = () => {
busiId: taskId,
}
}, [searchParams])
const tacticsMetaFromStorage = useMemo(() => {
const raw = sessionStorage.getItem('tacticsMeta')
if (!raw) {
return undefined
}
try {
const parsed = JSON.parse(raw)
return parsed
}
catch {
// ignore
}
return undefined
}, [])
const tacticsMeta = useMemo(() => tacticsMetaFromState || tacticsMetaFromQuery || tacticsMetaFromStorage, [tacticsMetaFromState, tacticsMetaFromQuery, tacticsMetaFromStorage])
// 读取 place=user 时的 userMeta 参数
const userMetaFromState = (location.state as { userMeta?: any } | null)?.userMeta
const userMetaFromQuery = useMemo(() => {
// 读取 place=user 时的 userMeta 参数(直接从 query 读取,不再使用 sessionStorage)
const userMeta = useMemo(() => {
const place = searchParams.get('place')
if (place !== 'user') {
return undefined
......@@ -125,23 +107,6 @@ export const TacticsChat: React.FC = () => {
numberType: searchParams.get('numberType') || undefined,
}
}, [searchParams])
const userMetaFromStorage = useMemo(() => {
const raw = sessionStorage.getItem('userMeta')
if (!raw) {
return undefined
}
try {
const parsed = JSON.parse(raw)
if (parsed?.place === 'user') {
return parsed
}
}
catch {
// ignore
}
return undefined
}, [])
const userMeta = useMemo(() => userMetaFromState || userMetaFromQuery || userMetaFromStorage, [userMetaFromState, userMetaFromQuery, userMetaFromStorage])
// 仅用于创建会话的额外参数(create_conversation)
const getConversationExtra = useCallback(() => {
......@@ -688,9 +653,9 @@ export const TacticsChat: React.FC = () => {
await dispatch(fetchTacticsConversations())
if (currentIdRef.current) {
logFullUrl('before navigate reanalyze')
navigate(`/tactics/chat/${currentIdRef.current}`)
navigate(`/tactics/chat/${currentIdRef.current}${location.search}`)
}
}, [dispatch, navigate, handleSubmitQuestion, userMeta, getNumberTypeWithUserMeta, isLoading, isAsking, logFullUrl])
}, [dispatch, navigate, handleSubmitQuestion, userMeta, getNumberTypeWithUserMeta, isLoading, isAsking, logFullUrl, location.search])
const handleCancelClear = useCallback(() => {
setShowClearConfirm(false)
......@@ -792,17 +757,15 @@ export const TacticsChat: React.FC = () => {
useEffect(() => {
if (shouldNavigateToNewConversation && currentConversationId) {
logFullUrl('before navigate create new conversation')
navigate(`/tactics/chat/${currentConversationId}`, {
navigate(`/tactics/chat/${currentConversationId}${location.search}`, {
state: {
shouldSendQuestion: shouldSendQuestionFromState,
tacticsMeta,
userMeta,
},
replace: true,
})
dispatch(clearTacticsNavigationFlag())
}
}, [shouldNavigateToNewConversation, currentConversationId, navigate, dispatch, shouldSendQuestionFromState, tacticsMeta, userMeta, logFullUrl])
}, [shouldNavigateToNewConversation, currentConversationId, navigate, dispatch, shouldSendQuestionFromState, location.search, logFullUrl])
// 处理shouldSendQuestion的变化 - 自动发送问题
useEffect(() => {
......
......@@ -25,8 +25,8 @@ export const TacticsHome: React.FC = () => {
defaultValue: '',
})
// from=tactics 时读取并缓存额外参数(无论 version 是否为 2),用于新建会话
const tacticsMetaFromSearch = useMemo(() => {
// from=tactics 时读取额外参数,用于新建会话(直接从 query 读取,不再使用 sessionStorage)
const tacticsMeta = useMemo(() => {
const from = searchParams.get('from')
if (from !== 'tactics') {
return undefined
......@@ -45,26 +45,9 @@ export const TacticsHome: React.FC = () => {
busiId: taskId,
}
}, [searchParams])
const tacticsMetaFromStorage = useMemo(() => {
const raw = sessionStorage.getItem('tacticsMeta')
if (!raw) {
return undefined
}
try {
const parsed = JSON.parse(raw)
if (parsed?.from === 'tactics') {
return parsed
}
}
catch {
// ignore
}
return undefined
}, [])
const tacticsMeta = useMemo(() => tacticsMetaFromSearch || tacticsMetaFromStorage, [tacticsMetaFromSearch, tacticsMetaFromStorage])
// 读取 place=user 时的 userMeta 参数
const userMetaFromSearch = useMemo(() => {
// 读取 place=user 时的 userMeta 参数(直接从 query 读取,不再使用 sessionStorage)
const userMeta = useMemo(() => {
const place = searchParams.get('place')
if (place !== 'user') {
return undefined
......@@ -78,23 +61,6 @@ export const TacticsHome: React.FC = () => {
busiId: searchParams.get('userId') || undefined,
}
}, [searchParams])
const userMetaFromStorage = useMemo(() => {
const raw = sessionStorage.getItem('userMeta')
if (!raw) {
return undefined
}
try {
const parsed = JSON.parse(raw)
if (parsed?.place === 'user') {
return parsed
}
}
catch {
// ignore
}
return undefined
}, [])
const userMeta = useMemo(() => userMetaFromSearch || userMetaFromStorage, [userMetaFromSearch, userMetaFromStorage])
// 仅用于创建会话的额外参数(对应 create_conversation)
const getConversationExtra = useCallback(() => {
......@@ -113,20 +79,6 @@ export const TacticsHome: React.FC = () => {
return {}
}, [tacticsMeta, userMeta])
// 同步到 sessionStorage,便于跳转后读取;仅在有 meta 时写入,避免覆盖重定向缓存
useEffect(() => {
if (tacticsMeta) {
sessionStorage.setItem('tacticsMeta', JSON.stringify(tacticsMeta))
}
}, [tacticsMeta])
// 同步 userMeta 到 sessionStorage
useEffect(() => {
if (userMeta) {
sessionStorage.setItem('userMeta', JSON.stringify(userMeta))
}
}, [userMeta])
const initTacticsConversation = () => {
const fromCollect = location.state?.fromCollect
// 只有在访问问答首页时才创建新对话
......@@ -161,11 +113,9 @@ export const TacticsHome: React.FC = () => {
const handleCreateConversation = useCallback(async (question: string) => {
// 如果已有会话,直接调用 submit 接口提交问题,然后跳转到聊天页面
if (currentConversationId) {
navigate(`/tactics/chat/${currentConversationId}`, {
navigate(`/tactics/chat/${currentConversationId}${location.search}`, {
state: {
shouldSendQuestion: question,
tacticsMeta,
userMeta,
},
})
return
......@@ -178,23 +128,20 @@ export const TacticsHome: React.FC = () => {
shouldSendQuestion: question,
}),
)
}, [dispatch, currentConversationId, navigate, tacticsMeta, userMeta, getConversationExtra])
}, [dispatch, currentConversationId, navigate, location.search, getConversationExtra])
// 监听导航到新对话
useEffect(() => {
if (shouldNavigateToNewConversation && currentConversationId) {
const url = `/tactics/chat/${currentConversationId}`
navigate(url, {
navigate(`/tactics/chat/${currentConversationId}${location.search}`, {
state: {
shouldSendQuestion,
tacticsMeta,
userMeta,
},
replace: true,
})
dispatch(clearTacticsNavigationFlag())
}
}, [shouldNavigateToNewConversation, currentConversationId, shouldSendQuestion, navigate, dispatch, tacticsMeta, userMeta])
}, [shouldNavigateToNewConversation, currentConversationId, shouldSendQuestion, navigate, dispatch, location.search])
const login = useCallback(async () => {
if (hasFetched.current) {
......
......@@ -22,46 +22,10 @@ export function withRouteChangeHandler(WrappedComponent: React.ComponentType) {
if (location.pathname === '/home') {
const searchParams = new URLSearchParams(location.search)
const from = searchParams.get('from')
// 获取place参数值
const place = searchParams.get('place')
if (from === 'tactics') {
// 在重定向前缓存查询参数,供 /tactics 页读取(避免 from 被清理导致丢失)
try {
if (place) {
const userMeta = {
cstId: searchParams.get('cstId') ?? undefined,
userId: searchParams.get('userId') ?? undefined,
numberType: searchParams.get('numberType') ?? undefined,
place,
busiType: '02',
busiId: searchParams.get('userId') ?? undefined,
}
sessionStorage.setItem('userMeta', JSON.stringify(userMeta))
}
else {
const meta = {
version: searchParams.get('version') || undefined,
taskId: searchParams.get('taskId') || undefined,
pinBeginTime: searchParams.get('pinBeginTime') || undefined,
pinEndTime: searchParams.get('pinEndTime') || undefined,
partOrAll: searchParams.get('partOrAll') || undefined,
channel: searchParams.get('channel') || undefined,
channelName: searchParams.get('channelName') || undefined,
from,
busiType: '02',
busiId: searchParams.get('taskId') || undefined,
}
sessionStorage.setItem('tacticsMeta', JSON.stringify(meta))
}
}
catch {
// ignore
}
// 重定向到 /tactics,保留其他查询参数
const newSearchParams = new URLSearchParams(location.search)
newSearchParams.delete('from')
const newSearch = newSearchParams.toString()
navigate(`/tactics${newSearch ? `?${newSearch}` : ''}`, { replace: true })
// 重定向到 /tactics,保留所有查询参数(不再使用 sessionStorage)
const newSearch = location.search
navigate(`/tactics${newSearch}`, { replace: true })
return
}
}
......
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