修复:AI问答模块
This commit is contained in:
@@ -2,7 +2,7 @@ import {useEffect, useState, useRef} from "react";
|
||||
import {View, Textarea} from '@tarojs/components';
|
||||
import Taro from '@tarojs/taro';
|
||||
import {Button} from '@nutui/nutui-react-taro';
|
||||
import {User, ArrowUp} from '@nutui/icons-react-taro';
|
||||
import {User, ArrowUp, Home} from '@nutui/icons-react-taro';
|
||||
import {sendAiMessage, stopAiMessage} from '@/api/ai';
|
||||
import {createWebSocket} from '@/utils/websocket';
|
||||
import {getAiToken} from '@/utils/aiToken';
|
||||
@@ -29,6 +29,7 @@ const AiChat = () => {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [currentTaskId, setCurrentTaskId] = useState<string>('');
|
||||
const [wsConnected, setWsConnected] = useState(false);
|
||||
const [isInitialized, setIsInitialized] = useState(false);
|
||||
const messagesEndRef = useRef<HTMLDivElement>(null);
|
||||
const wsRef = useRef<any>(null);
|
||||
|
||||
@@ -45,6 +46,18 @@ const AiChat = () => {
|
||||
return getAiToken();
|
||||
};
|
||||
|
||||
// 调试函数:检查输入框状态
|
||||
const debugInputStatus = () => {
|
||||
console.log('输入框状态调试:', {
|
||||
isInitialized,
|
||||
isLoading,
|
||||
wsConnected,
|
||||
inputValue,
|
||||
inputDisabled: !isInitialized || isLoading,
|
||||
sendButtonDisabled: !isInitialized || !inputValue.trim()
|
||||
});
|
||||
};
|
||||
|
||||
// 滚动到底部
|
||||
const scrollToBottom = () => {
|
||||
// 检查并确保AI_TOKEN存在
|
||||
@@ -57,7 +70,9 @@ const AiChat = () => {
|
||||
|
||||
// 初始化WebSocket连接
|
||||
const initWebSocket = () => {
|
||||
wsRef.current = createWebSocket(WSS_API_URL + "/chat/" + Taro.getStorageSync('AI_TOKEN'));
|
||||
const userToken = Taro.getStorageSync('AI_TOKEN') || 'anonymous';
|
||||
console.log(WSS_API_URL + "/chat/" + userToken, 'wsUrl');
|
||||
wsRef.current = createWebSocket(WSS_API_URL + "/chat/" + userToken);
|
||||
|
||||
wsRef.current.onMessage((data: any) => {
|
||||
console.log('收到WebSocket消息:', data);
|
||||
@@ -145,10 +160,25 @@ const AiChat = () => {
|
||||
|
||||
useEffect(() => {
|
||||
// 初始化时检查并生成AI Token
|
||||
checkAiToken();
|
||||
const token = checkAiToken();
|
||||
console.log('AI Token初始化完成:', token);
|
||||
|
||||
// 检查userToken
|
||||
const userToken = Taro.getStorageSync('AI_TOKEN');
|
||||
console.log('当前UserToken:', userToken || 'anonymous');
|
||||
|
||||
// 初始化WebSocket
|
||||
initWebSocket();
|
||||
Taro.hideTabBar();
|
||||
|
||||
// 标记初始化完成
|
||||
setIsInitialized(true);
|
||||
|
||||
// 延迟调试,确保状态更新完成
|
||||
setTimeout(() => {
|
||||
debugInputStatus();
|
||||
}, 100);
|
||||
|
||||
return () => {
|
||||
if (wsRef.current) {
|
||||
wsRef.current.close();
|
||||
@@ -190,7 +220,7 @@ const AiChat = () => {
|
||||
type: 'user',
|
||||
query: content.trim(),
|
||||
timestamp: Date.now(),
|
||||
user: `${Taro.getStorageSync('AI_TOKEN')}`
|
||||
user: `${Taro.getStorageSync('AI_TOKEN') || 'anonymous'}`
|
||||
};
|
||||
|
||||
// 立即添加用户消息
|
||||
@@ -211,7 +241,7 @@ const AiChat = () => {
|
||||
try {
|
||||
await sendAiMessage({
|
||||
query: content.trim(),
|
||||
user: `${Taro.getStorageSync('AI_TOKEN')}`,
|
||||
user: `${Taro.getStorageSync('AI_TOKEN') || 'anonymous'}`,
|
||||
responseMode: 'streaming',
|
||||
aiToken: aiToken, // 包含AI Token
|
||||
});
|
||||
@@ -257,6 +287,14 @@ const AiChat = () => {
|
||||
|
||||
// 处理快捷问题点击
|
||||
const handleQuickQuestion = (question: string) => {
|
||||
if (!isInitialized) {
|
||||
Taro.showToast({
|
||||
title: '正在初始化,请稍候...',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
return;
|
||||
}
|
||||
handleSendMessage(question);
|
||||
};
|
||||
|
||||
@@ -339,10 +377,13 @@ const AiChat = () => {
|
||||
<Textarea
|
||||
className="message-input"
|
||||
value={inputValue}
|
||||
placeholder={isLoading ? "AI正在回复中..." : "请输入您的问题..."}
|
||||
placeholder={
|
||||
!isInitialized ? "正在初始化..." :
|
||||
isLoading ? "AI正在回复中..." :
|
||||
"请输入您的问题..."
|
||||
}
|
||||
onInput={(e) => setInputValue(e.detail.value)}
|
||||
onConfirm={() => handleSendMessage(inputValue)}
|
||||
disabled={isLoading}
|
||||
autoHeight
|
||||
maxlength={500}
|
||||
/>
|
||||
@@ -360,7 +401,7 @@ const AiChat = () => {
|
||||
<Button
|
||||
type="primary"
|
||||
onClick={() => handleSendMessage(inputValue)}
|
||||
disabled={!inputValue.trim()}
|
||||
disabled={!isInitialized || !inputValue.trim()}
|
||||
icon={<ArrowUp/>}
|
||||
className="send-button"
|
||||
>
|
||||
@@ -368,6 +409,9 @@ const AiChat = () => {
|
||||
)}
|
||||
</View>
|
||||
</View>
|
||||
<View className="fixed top-2 left-3" onClick={() => Taro.reLaunch({'url': '/pages/index/index'})}>
|
||||
<Home/>
|
||||
</View>
|
||||
</View>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -8,77 +8,11 @@ import Menu from "./Menu";
|
||||
import TabBar from "@/components/TabBar";
|
||||
import Image from "./Image";
|
||||
|
||||
export interface Market {
|
||||
// 自增ID
|
||||
id?: number;
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
name?: string;
|
||||
title?: string;
|
||||
}
|
||||
|
||||
function Home() {
|
||||
const [loading, setLoading] = useState<boolean>(false)
|
||||
const [IsLogin, setIsLogin] = useState<boolean>(true)
|
||||
const [search, setSearch] = useState(false)
|
||||
|
||||
// useShareTimeline(() => {
|
||||
// return {
|
||||
// title: '注册即可开通 - webSoft云应用',
|
||||
// path: `/pages/index/index`
|
||||
// };
|
||||
// });
|
||||
|
||||
// useShareAppMessage(() => {
|
||||
// return {
|
||||
// title: '注册即可开通 - webSoft云应用',
|
||||
// path: `/pages/index/index`,
|
||||
// success: function (res) {
|
||||
// console.log('分享成功', res);
|
||||
// },
|
||||
// fail: function (res) {
|
||||
// console.log('分享失败', res);
|
||||
// }
|
||||
// };
|
||||
// });
|
||||
|
||||
// const reloadMore = async () => {
|
||||
// setPage(page + 1)
|
||||
// }
|
||||
|
||||
// const showAuthModal = () => {
|
||||
// Taro.showModal({
|
||||
// title: '授权提示',
|
||||
// content: '需要获取您的用户信息',
|
||||
// confirmText: '去授权',
|
||||
// cancelText: '取消',
|
||||
// success: (res) => {
|
||||
// if (res.confirm) {
|
||||
// // 用户点击确认,打开授权设置页面
|
||||
// openSetting();
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// };
|
||||
|
||||
// const openSetting = () => {
|
||||
// // Taro.openSetting:调起客户端小程序设置界面,返回用户设置的操作结果。设置界面只会出现小程序已经向用户请求过的权限。
|
||||
// Taro.openSetting({
|
||||
// success: (res) => {
|
||||
// if (res.authSetting['scope.userInfo']) {
|
||||
// // 用户授权成功,可以获取用户信息
|
||||
// reload();
|
||||
// } else {
|
||||
// // 用户拒绝授权,提示授权失败
|
||||
// Taro.showToast({
|
||||
// title: '授权失败',
|
||||
// icon: 'none'
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// };
|
||||
|
||||
// 登录成功后回调
|
||||
const handleLogin = () => {
|
||||
setIsLogin(true)
|
||||
@@ -95,6 +29,7 @@ function Home() {
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
Taro.showTabBar()
|
||||
// 获取站点信息
|
||||
getSiteInfo().then((data) => {
|
||||
console.log(data, 'siteInfo')
|
||||
@@ -102,23 +37,6 @@ function Home() {
|
||||
setSearch(false);
|
||||
}
|
||||
})
|
||||
// Taro.getSetting:获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。
|
||||
// Taro.getSetting({
|
||||
// success: (res) => {
|
||||
// if (res.authSetting['scope.userInfo']) {
|
||||
// // 用户已经授权过,可以直接获取用户信息
|
||||
// console.log('用户已经授权过,可以直接获取用户信息')
|
||||
// reload().then(() => {
|
||||
// setLoading(false)
|
||||
// });
|
||||
// } else {
|
||||
// // 用户未授权,需要弹出授权窗口
|
||||
// console.log('用户未授权,需要弹出授权窗口')
|
||||
// // showAuthModal();
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// 获取用户信息
|
||||
}, []);
|
||||
|
||||
return (
|
||||
|
||||
@@ -43,13 +43,13 @@ function UserCard() {
|
||||
}
|
||||
{
|
||||
!IsLogin && (
|
||||
<Avatar size="large" onClick={() => Taro.navigateTo({url: '/passport/sms-login'})}
|
||||
<Avatar size="large"
|
||||
src={userInfo?.avatar} shape="round"/>
|
||||
)
|
||||
}
|
||||
<div className={'user-info flex flex-col px-1'}>
|
||||
{!IsLogin ? (
|
||||
<div className={'p-1 text-sm text-white'}>{'点击登录'}</div>
|
||||
<div className={'p-1 text-sm text-white'}></div>
|
||||
) :
|
||||
<div className={'p-1 text-sm text-white'}>{userInfo?.mobile}</div>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user