Commit e259f8d3 by Liu

fix:获取工具列表&&提问参数增加id

parent f520e296
...@@ -7,3 +7,11 @@ import http from '@/utils/request' ...@@ -7,3 +7,11 @@ import http from '@/utils/request'
export function fetchQuestionList(data: any) { export function fetchQuestionList(data: any) {
return http.post('/config-center/api/commonconfig/mobile/v1/query_config_list', data) return http.post('/config-center/api/commonconfig/mobile/v1/query_config_list', data)
} }
/**
* 查询工具列表
* @params
*/
export function fetchToolList() {
return http.post('/config-center/api/tool/mobile/v1/get_tool_list')
}
...@@ -8,11 +8,12 @@ import SendIcon from '@/assets/svg/send.svg?react' ...@@ -8,11 +8,12 @@ import SendIcon from '@/assets/svg/send.svg?react'
import efficiencyIcon from '@/assets/efficiency-icon.png' import efficiencyIcon from '@/assets/efficiency-icon.png'
import { type WithAuthProps, withAuth } from '@/auth/withAuth' import { type WithAuthProps, withAuth } from '@/auth/withAuth'
import { useAppSelector } from '@/store/hook' import { useAppSelector } from '@/store/hook'
import { fetchToolList } from '@/api/home'
interface ChatEditorProps { interface ChatEditorProps {
onChange?: (value: string) => void onChange?: (value: string) => void
onFocus?: () => void onFocus?: () => void
onSubmit?: (value: string) => void onSubmit?: (value: string, toolId?: string) => void
placeholders: string[] placeholders: string[]
showContentTips?: boolean showContentTips?: boolean
initialValue?: string initialValue?: string
...@@ -26,6 +27,21 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -26,6 +27,21 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
const intervalRef = useRef<NodeJS.Timeout | null>(null) const intervalRef = useRef<NodeJS.Timeout | null>(null)
const [isOpenLoginModal, isOpenLoginModalActions] = useToggle() const [isOpenLoginModal, isOpenLoginModalActions] = useToggle()
const isAsking = useAppSelector((state: RootState) => state.chat.isAsking) const isAsking = useAppSelector((state: RootState) => state.chat.isAsking)
const [toolList, setToolList] = useState<any[]>([])
const [selectedToolId, setSelectedToolId] = useState<string | null>(null)
// 获取工具列表
const getToolList = async () => {
try {
const res = await fetchToolList()
if (res?.data) {
setToolList(res.data)
}
}
catch (error) {
console.error('获取工具列表失败:', error)
}
}
const startAnimation = () => { const startAnimation = () => {
intervalRef.current = setInterval(() => { intervalRef.current = setInterval(() => {
...@@ -55,7 +71,18 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -55,7 +71,18 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
return return
if (checkAuth()) { if (checkAuth()) {
if (content.trim()) { if (content.trim()) {
onSubmit?.(content.trim()) // 根据选择的工具或默认工具获取toolId
let toolId: string | undefined
if (selectedToolId) {
// 如果选择了"提质增效"工具,使用对应的toolId
toolId = selectedToolId
}
else if (toolList.length > 0) {
// 如果没有选择工具,使用第一个工具的toolId
toolId = toolList[0].toolId
}
onSubmit?.(content.trim(), toolId)
setContent('') setContent('')
if (editorRef.current) { if (editorRef.current) {
editorRef.current.textContent = '' editorRef.current.textContent = ''
...@@ -82,6 +109,21 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -82,6 +109,21 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
document.execCommand('insertText', false, text) document.execCommand('insertText', false, text)
} }
// 处理工具按钮点击
const handleToolClick = () => {
// 查找"提质增效"工具
const efficiencyTool = toolList.find((tool: any) => tool.toolName === '提质增效')
if (efficiencyTool) {
setSelectedToolId(efficiencyTool.toolId)
}
else {
// 如果没有找到"提质增效"工具,使用第一个工具
if (toolList.length > 0) {
setSelectedToolId(toolList[0].toolId)
}
}
}
useEffect(() => { useEffect(() => {
startAnimation() startAnimation()
document.addEventListener('visibilitychange', handleVisibilityChange) document.addEventListener('visibilitychange', handleVisibilityChange)
...@@ -101,6 +143,11 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -101,6 +143,11 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
} }
}, [content]) }, [content])
// 组件加载时获取工具列表
useEffect(() => {
getToolList()
}, [])
// 处理initialValue的变化 // 处理initialValue的变化
useEffect(() => { useEffect(() => {
if (initialValue && editorRef.current) { if (initialValue && editorRef.current) {
...@@ -196,6 +243,7 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -196,6 +243,7 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
radius="full" radius="full"
variant="bordered" variant="bordered"
startContent={<img src={efficiencyIcon} alt="提质增效" className="w-5 h-5" />} startContent={<img src={efficiencyIcon} alt="提质增效" className="w-5 h-5" />}
onPress={handleToolClick}
> >
提质增效 提质增效
</Button> </Button>
......
...@@ -113,7 +113,7 @@ export const Chat: React.FC = () => { ...@@ -113,7 +113,7 @@ export const Chat: React.FC = () => {
} }
/** 提交问题 */ /** 提交问题 */
const handleSubmitQuestion = async (question: string, productCode?: string) => { const handleSubmitQuestion = async (question: string, productCode?: string, toolId?: string) => {
// 停止之前的请求 // 停止之前的请求
if (abortControllerRef.current) { if (abortControllerRef.current) {
abortControllerRef.current.abort() abortControllerRef.current.abort()
...@@ -162,6 +162,7 @@ export const Chat: React.FC = () => { ...@@ -162,6 +162,7 @@ export const Chat: React.FC = () => {
conversationId: currentIdRef.current, conversationId: currentIdRef.current,
stream: true, stream: true,
productCode, productCode,
toolId,
}, },
(msg) => { (msg) => {
// 检查是否已被取消 // 检查是否已被取消
...@@ -293,7 +294,7 @@ export const Chat: React.FC = () => { ...@@ -293,7 +294,7 @@ export const Chat: React.FC = () => {
{allItems.map((record, index) => ( {allItems.map((record, index) => (
<div <div
className="w-full chatItem mx-auto" className="w-full chatItem mx-auto"
key={`${record.role}-${record.id || index}-${ key={`${record.role}-${index}-${
record.question || record.answerList?.[0]?.answer || '' record.question || record.answerList?.[0]?.answer || ''
}`} }`}
> >
...@@ -331,7 +332,7 @@ export const Chat: React.FC = () => { ...@@ -331,7 +332,7 @@ export const Chat: React.FC = () => {
</motion.div> </motion.div>
</div> </div>
<ChatEditor onSubmit={handleSubmitQuestion} placeholders={[]} /> <ChatEditor onSubmit={(question, toolId) => handleSubmitQuestion(question, undefined, toolId)} placeholders={[]} />
<div className="hidden sm:block w-full text-center mt-[12px] text-[#3333334d] text-[12px]"> <div className="hidden sm:block w-full text-center mt-[12px] text-[#3333334d] text-[12px]">
内容由AI模型生成,其准确性和完整性无法保证,仅供参考 内容由AI模型生成,其准确性和完整性无法保证,仅供参考
</div> </div>
......
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