Commit ddfe6d87 by Liu

chore: commit staged files

parent cc4a3caf
......@@ -8,39 +8,37 @@ import type { RootState } from '@/store'
import SendIcon from '@/assets/svg/send.svg?react'
import { type WithAuthProps, withAuth } from '@/auth/withAuth'
import { useAppDispatch, useAppSelector } from '@/store/hook'
import { fetchToolList } from '@/api/home'
import { getUserRolesForApi } from '@/lib/utils'
import { clearCurrentToolId, createConversation, setCurrentToolId } from '@/store/conversationSlice'
// 使用本地 mock 工具列表常量,便于离线/联调阶段使用(已废弃,仅保留注释)
// const MOCK_TOOL_LIST = [
// {
// toolId: '6712395743241',
// toolName: '提质增效',
// toolContent: 'https://sit-wechat.guominpension.com/underwrite',
// toolIcon: 'http://p-cf-co-1255000025.cos.bj.csyun001.ccbcos.co/tool-increase.svg',
// toolType: '03',
// userRole: '02',
// showOrder: 8,
// },
// {
// toolId: '6712395743240',
// toolName: '数据助手',
// toolContent: 'https://sit-wechat.guominpension.com/underwrite',
// toolIcon: 'http://p-cf-co-1255000025.cos.bj.csyun001.ccbcos.co/tool-data.svg',
// toolType: '03',
// userRole: '01',
// showOrder: 8,
// },
// {
// toolId: 'general-mode',
// toolName: '通用模式',
// toolContent: 'https://sit-wechat.guominpension.com/underwrite',
// toolIcon: 'http://p-cf-co-1255000025.cos.bj.csyun001.ccbcos.co/tool-normal.svg',
// toolType: '01',
// userRole: '00',
// showOrder: 8,
// },
// ] as const
// 本地 mock 工具列表,离线/联调阶段用于渲染按钮
const MOCK_TOOL_LIST = [
{
toolId: '6712395743241',
toolName: '提质增效',
toolContent: 'https://sit-wechat.guominpension.com/underwrite',
toolIcon: 'http://p-cf-co-1255000025.cos.bj.csyun001.ccbcos.co/tool-increase.svg',
toolType: '03',
userRole: '02',
showOrder: 8,
},
{
toolId: '6712395743240',
toolName: '数据助手',
toolContent: 'https://sit-wechat.guominpension.com/underwrite',
toolIcon: 'http://p-cf-co-1255000025.cos.bj.csyun001.ccbcos.co/tool-data.svg',
toolType: '03',
userRole: '01',
showOrder: 8,
},
{
toolId: 'general-mode',
toolName: '通用模式',
toolContent: 'https://sit-wechat.guominpension.com/underwrite',
toolIcon: 'http://p-cf-co-1255000025.cos.bj.csyun001.ccbcos.co/tool-normal.svg',
toolType: '01',
userRole: '00',
showOrder: 8,
},
] as const
interface ChatEditorProps {
onChange?: (value: string) => void
......@@ -72,27 +70,8 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
// 获取工具列表
const getToolList = async () => {
try {
// 调用真实接口获取工具列表
const userRoles = getUserRolesForApi()
const res = await fetchToolList({ userRoles })
if (res?.data) {
// 根据 toolId 去重,防止重复渲染
const uniqueList = res.data.filter((tool: any, index: number, self: any[]) =>
index === self.findIndex((t: any) => t.toolId === tool.toolId),
)
setToolList(uniqueList)
}
else {
// 当接口无数据时,不使用本地 mock,直接置空
setToolList([])
}
}
catch (error) {
console.error('获取工具列表失败:', error)
// 接口异常时不再回退到本地 mock 常量
setToolList([])
}
// 暂时启用本地 mock 数据,确保在无后端接口时也能渲染
setToolList(MOCK_TOOL_LIST.map(tool => ({ ...tool })))
}
// 根据 currentToolId 以及 sessionStorage 中的记录决定高亮逻辑
......@@ -283,8 +262,17 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
// 处理工具按钮点击:先创建新会话,再切换工具
const handleToolClick = async (tool: any) => {
if (!checkAuth())
return
// if (!checkAuth())
// return
// 调试:点击时记录当前与将要设置的 toolId,排查选中状态是否被覆盖
// eslint-disable-next-line no-console
console.log('[ChatEditor] handleToolClick 点击前:', {
clickToolId: tool.toolId,
toolName: tool.toolName,
selectedToolId,
sessionToolId,
currentToolIdInRedux: currentToolId,
})
if (tool.toolName === '数据助手') {
sessionStorage.setItem('showToolQuestion', 'true')
setShowToolQuestion(true)
......@@ -298,6 +286,13 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
setIsToolBtnActive(false)
sessionStorage.setItem('currentToolId', tool.toolId)
setSessionToolId(tool.toolId)
// eslint-disable-next-line no-console
console.log('[ChatEditor] handleToolClick 点击后(已设置选中):', {
clickToolId: tool.toolId,
toolName: tool.toolName,
selectedToolIdAfterSet: tool.toolId,
sessionToolIdAfterSet: tool.toolId,
})
try {
await dispatch(createConversation({
conversationData: { toolId: tool.toolId },
......
......@@ -107,7 +107,12 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c
const url = currentToolId
? `/chat/${currentConversationId}?toolId=${currentToolId}`
: `/chat/${currentConversationId}`
navigate(url)
// 通过 location.state 传递 toolId,避免在 Chat 页面被误判为“外链残留参数”而强制清空
navigate(url, {
state: {
toolId: currentToolId || null,
},
})
dispatch(clearNavigationFlag())
}
}, [shouldNavigateToNewConversation, currentConversationId, currentToolId, navigate, dispatch])
......
......@@ -28,7 +28,8 @@ export const Chat: React.FC = () => {
const [searchParams, setSearchParams] = useSearchParams()
// 优先从 URL 查询参数读取 toolId(刷新后仍能保留),其次从 location.state 读取
const rawToolIdFromUrl = searchParams.get('toolId')
const shouldForceClearUrlToolId = Boolean(rawToolIdFromUrl)
// 只有在非 SPA 导航(location.state 不存在)且链接携带 toolId 时才清空,避免影响站内点击历史记录
const shouldForceClearUrlToolId = !location.state && Boolean(rawToolIdFromUrl)
const toolIdFromUrl = shouldForceClearUrlToolId ? null : rawToolIdFromUrl
// 添加调试日志,查看 location.state 的实际值
// eslint-disable-next-line no-console
......@@ -54,6 +55,15 @@ export const Chat: React.FC = () => {
const [currentToolName, setCurrentToolName] = useState<string | undefined>(undefined)
// 使用 ref 保存从 location.state 传递的 toolId,避免被异步操作覆盖
const toolIdFromStateRef = useRef<string | null | undefined>(undefined)
// 当外部系统直接以 /chat/:id 链接进入(没有 location.state,且 URL 中也没有 toolId)时,
// 视为一次新的会话入口:重置为通用模式,清除历史遗留的工具模式状态
useEffect(() => {
if (!location.state && !rawToolIdFromUrl) {
dispatch(clearCurrentToolId())
sessionStorage.removeItem('currentToolId')
}
}, [location.state, rawToolIdFromUrl, dispatch])
// 若链接中带有 toolId,进入页面后强制清空(避免沿用历史链接参数)
useEffect(() => {
if (!shouldForceClearUrlToolId)
......
......@@ -85,20 +85,26 @@ export const Home: React.FC = () => {
}))
setIsDataLoaded(false) // 重置加载状态
try {
const storedToolId = sessionStorage.getItem('currentToolId') || ''
const searchParams = new URLSearchParams(location.search)
// 首页初始化加载常见问题时,允许忽略路由中的 toolId,避免带入上一次的工具 ID
const urlToolId = ignoreUrlToolId ? '' : (searchParams.get('toolId') || '')
const shouldForceClearToolId = !storedToolId && !urlToolId
const storedToolId = sessionStorage.getItem('currentToolId') || ''
const searchParams = new URLSearchParams(location.search)
// 首页初始化加载常见问题时,允许忽略路由中的 toolId,避免带入上一次的工具 ID
const urlToolId = ignoreUrlToolId ? '' : (searchParams.get('toolId') || '')
const shouldForceClearToolId = !storedToolId && !urlToolId
let finalToolId = toolId || ''
if (shouldForceClearToolId && !isToolBtn) {
finalToolId = ''
}
else if (!finalToolId && !isToolBtn) {
// 仅在工具模式下才使用回退逻辑,避免通用模式误用上一次的 toolId
finalToolId = storedToolId || urlToolId
}
let finalToolId = toolId || ''
// 场景:首页首次挂载(ignoreUrlToolId === true)且为通用模式(!isToolBtn && !toolId)
// 此时无论 sessionStorage 中是否残留上一次的 toolId,都强制使用空字符串,避免带入历史工具 ID
if (ignoreUrlToolId && !isToolBtn && !toolId) {
finalToolId = ''
}
else if (shouldForceClearToolId && !isToolBtn) {
finalToolId = ''
}
else if (!finalToolId && !isToolBtn) {
// 仅在工具模式下才使用回退逻辑,避免通用模式误用上一次的 toolId
finalToolId = storedToolId || urlToolId
}
const res = await fetchEfficiencyQuestionList({ toolId: finalToolId })
if (res && res.data && res.data.questions) {
setOtherQuestions((prev: any) => ({
......@@ -226,7 +232,12 @@ export const Home: React.FC = () => {
useEffect(() => {
login()
// 首页首次挂载时强制忽略路由中的 toolId,只按通用模式拉常见问题
// 首页首次挂载时重置为通用模式:
// 1. 清除 Redux 中的 currentToolId
dispatch(clearCurrentToolId())
// 2. 清除 sessionStorage 中可能残留的 currentToolId,避免沿用上一次工具模式
sessionStorage.removeItem('currentToolId')
// 3. 首页首次挂载时强制忽略路由中的 toolId,只按通用模式拉常见问题(toolId: '')
_handleToolClick(false, '', true)
}, []) // 依赖数组为空,只在组件挂载时执行一次
......
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