Commit 0e49191f by Liu

fix:chat页面增加常见问题调用

parent ddfe6d87
...@@ -34,7 +34,11 @@ function getQueryBeforeSecondQuestion(): string { ...@@ -34,7 +34,11 @@ function getQueryBeforeSecondQuestion(): string {
} }
} }
export function getUserRolesFromRouteAndStore(): string[] { /**
* 从路由获取 userRoles(不存储到 localStorage)
* @returns 返回获取到的 userRoles 数组
*/
export function getUserRolesFromRoute(): string[] {
try { try {
const sanitizedSearch = getQueryBeforeSecondQuestion() const sanitizedSearch = getQueryBeforeSecondQuestion()
const searchParams = new URLSearchParams(sanitizedSearch || window.location.search) const searchParams = new URLSearchParams(sanitizedSearch || window.location.search)
...@@ -57,7 +61,22 @@ export function getUserRolesFromRouteAndStore(): string[] { ...@@ -57,7 +61,22 @@ export function getUserRolesFromRouteAndStore(): string[] {
} }
} }
// 如果获取到了 userRoles,存储到 localStorage return userRoles
}
catch {
return []
}
}
/**
* @deprecated 已废弃,请使用 getUserRolesFromRoute
* 从路由获取 userRoles 并存储到 localStorage
* @returns 返回获取到的 userRoles 数组
*/
export function getUserRolesFromRouteAndStore(): string[] {
const userRoles = getUserRolesFromRoute()
// 如果获取到了 userRoles,存储到 localStorage(向后兼容)
if (userRoles.length > 0) { if (userRoles.length > 0) {
try { try {
localStorage.setItem(USER_ROLES_STORAGE_KEY, JSON.stringify(userRoles)) localStorage.setItem(USER_ROLES_STORAGE_KEY, JSON.stringify(userRoles))
...@@ -68,10 +87,6 @@ export function getUserRolesFromRouteAndStore(): string[] { ...@@ -68,10 +87,6 @@ export function getUserRolesFromRouteAndStore(): string[] {
} }
return userRoles return userRoles
}
catch {
return []
}
} }
/** /**
...@@ -95,13 +110,11 @@ export function getUserRolesFromStorage(): string[] { ...@@ -95,13 +110,11 @@ export function getUserRolesFromStorage(): string[] {
} }
/** /**
* 获取 userRoles(先同步路由到 localStorage,然后读取 * 获取 userRoles(从路由获取,确保使用最新的路由参数
* 这是推荐的统一方法,确保调用 fetchToolList 时能获取到正确的 userRoles * 这是推荐的统一方法,确保调用 fetchToolList 时能获取到正确的 userRoles
* @returns 返回 userRoles 数组,如果没有则返回空数组 * @returns 返回 userRoles 数组,如果没有则返回空数组
*/ */
export function getUserRolesForApi(): string[] { export function getUserRolesForApi(): string[] {
// 先同步路由中的 userRoles 到 localStorage(如果路由中有的话) // 直接从路由中获取 userRoles(路由参数是唯一真实来源)
getUserRolesFromRouteAndStore() return getUserRolesFromRoute()
// 然后从 localStorage 读取(localStorage.setItem 是同步的,所以可以立即读取)
return getUserRolesFromStorage()
} }
...@@ -12,6 +12,7 @@ import { useAppDispatch } from '@/store/hook' ...@@ -12,6 +12,7 @@ import { useAppDispatch } from '@/store/hook'
import { fetchEfficiencyQuestionList } from '@/api/home' import { fetchEfficiencyQuestionList } from '@/api/home'
import SdreamLoading from '@/components/SdreamLoading' import SdreamLoading from '@/components/SdreamLoading'
import { fetchLoginByToken, fetchLoginByUid } from '@/api/common' import { fetchLoginByToken, fetchLoginByUid } from '@/api/common'
import { getUserRolesFromRoute } from '@/lib/utils'
function getAnimationProps(delay: number) { function getAnimationProps(delay: number) {
return { return {
...@@ -105,6 +106,8 @@ export const Home: React.FC = () => { ...@@ -105,6 +106,8 @@ export const Home: React.FC = () => {
// 仅在工具模式下才使用回退逻辑,避免通用模式误用上一次的 toolId // 仅在工具模式下才使用回退逻辑,避免通用模式误用上一次的 toolId
finalToolId = storedToolId || urlToolId finalToolId = storedToolId || urlToolId
} }
// 调用真实 API 获取常见问题列表
const res = await fetchEfficiencyQuestionList({ toolId: finalToolId }) const res = await fetchEfficiencyQuestionList({ toolId: finalToolId })
if (res && res.data && res.data.questions) { if (res && res.data && res.data.questions) {
setOtherQuestions((prev: any) => ({ setOtherQuestions((prev: any) => ({
...@@ -230,13 +233,18 @@ export const Home: React.FC = () => { ...@@ -230,13 +233,18 @@ export const Home: React.FC = () => {
} }
}, [setToken, dispatch]) }, [setToken, dispatch])
// 监听路由参数变化,提取 userRoles(确保路由参数被正确解析)
useEffect(() => {
getUserRolesFromRoute()
}, [location.search])
useEffect(() => { useEffect(() => {
login() login()
// 首页首次挂载时重置为通用模式: // 首页首次挂载时重置为通用模式:
// 1. 清除 Redux 中的 currentToolId // 1. 清除 Redux 中的 currentToolId
dispatch(clearCurrentToolId()) dispatch(clearCurrentToolId())
// 2. 清除 sessionStorage 中可能残留的 currentToolId,避免沿用上一次工具模式 // 2. 清除 sessionStorage 中可能残留的 currentToolId,避免沿用上一次工具模式
sessionStorage.removeItem('currentToolId') // sessionStorage.removeItem('currentToolId')
// 3. 首页首次挂载时强制忽略路由中的 toolId,只按通用模式拉常见问题(toolId: '') // 3. 首页首次挂载时强制忽略路由中的 toolId,只按通用模式拉常见问题(toolId: '')
_handleToolClick(false, '', true) _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