|
|
import http from '../../../utils/api'Page({ data: { // 当前时间
currentTime: '', // 聊天消息
messages: [], // 输入框相关
inputValue: '', autoFocus: false, // 症状选择
quickSymptoms: [], wzsearch: {}, selectedSymptoms: [], showSymptomSelector: false, // 状态控制
isAIThinking: false, isLoading: false, loadingText: '', showMoreMenu: false },
onLoad() { this.initData(); this.getInquiry() },
// AI问诊快捷字列表
getInquiry() { http.inquiry({ data: {}, success: res => { console.log(1111, res); this.setData({ quickSymptoms: res.rows }) } }) },
onShow() { this.updateCurrentTime(); this.setData({ autoFocus: true }); },
// 初始化数据
initData() { // 设置当前时间
this.updateCurrentTime();
// 定时更新当前时间
setInterval(() => { this.updateCurrentTime(); }, 60000); },
// 更新当前时间
updateCurrentTime() { const now = new Date(); const timeString = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`; this.setData({ currentTime: timeString }); },
// 输入框变化
onInput(e) { this.setData({ inputValue: e.detail.value }); },
// 发送消息
sendMessage() { const message = this.data.inputValue.trim(); if (!message) return;
// 添加用户消息
const userMessage = { id: Date.now(), type: 'user', content: message, time: this.getCurrentTime() };
this.setData({ messages: [...this.data.messages, userMessage], inputValue: '', autoFocus: true });
console.log(666, this.data.messages)
// 先显示AI思考状态
this.setData({ isAIThinking: true });
// 模拟AI思考时间(1.5-2.5秒)
setTimeout(() => { this.generateAIResponse(message); }, 1500 + Math.random() * 1000); },
// 获取当前时间
getCurrentTime() { const now = new Date(); return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`; },
// 生成AI响应
generateAIResponse(userMessage) { console.log(888, userMessage); http.search({ data: { keyword: userMessage }, success: res => { console.log('查询结果', res); let aiMessage; if (res.rows && res.rows.length > 0) { const wzsearch = res.rows[0]; aiMessage = { id: Date.now() + 1, type: 'assistant', content:'根据您的描述,' + wzsearch.title || '已收到您的症状描述', diagnosis: wzsearch, time: this.getCurrentTime() }; } else { // 如果没有查询到结果
aiMessage = { id: Date.now() + 1, type: 'assistant', content: '已收到您的症状描述', diagnosis: { possibleDiseases: '暂无匹配诊断', severityLevel: '未知', suggestions: '建议您提供更详细的症状描述,或直接咨询专业兽医' }, time: this.getCurrentTime() }; } // 添加AI消息到聊天记录并隐藏思考状态
this.setData({ messages: [...this.data.messages, aiMessage], isAIThinking: false }); console.log(789, this.data.messages); }, fail: err => { console.error('API请求失败:', err); // API失败时的默认响应
const aiMessage = { id: Date.now() + 1, type: 'assistant', content: '已收到您的症状描述', diagnosis: { disease: '网络请求失败', severity: 'unknown', severityText: '未知', suggestion: '请检查网络连接后重试,或直接联系兽医' }, time: this.getCurrentTime() }; this.setData({ messages: [...this.data.messages, aiMessage], isAIThinking: false }); }, complete: () => { // 确保无论如何都会隐藏思考状态
if (this.data.isAIThinking) { this.setData({ isAIThinking: false }); } } }); },
// 选择快捷症状
selectQuickSymptom(e) { const symptom = e.currentTarget.dataset.symptom.keywords; this.setData({ inputValue: symptom }); this.sendMessage(); },
// 返回
goBack() { wx.navigateBack(); },
// 显示更多菜单
showMoreMenu() { this.setData({ showMoreMenu: true }); },
// 关闭更多菜单
closeMoreMenu() { this.setData({ showMoreMenu: false }); },
// 阻止事件冒泡
stopPropagation() {},
// 清空记录
clearHistory() { wx.showModal({ title: '提示', content: '确定要清空所有聊天记录吗?', success: (res) => { if (res.confirm) { this.setData({ messages: [{ id: 1, type: 'assistant', content: '您好!我是AI健康助手,有什么可以帮您?\n\n请描述您或牲畜的健康状况,我会为您提供专业的分析和建议。', time: this.getCurrentTime() }], selectedSymptoms: [] }); this.closeMoreMenu(); } } }); },
// 导出记录
exportChat() { wx.showToast({ title: '记录已保存到本地', icon: 'success' }); this.closeMoreMenu(); },
// 联系兽医
contactDoctor() { wx.showModal({ title: '联系兽医', content: '确定要拨打兽医热线吗?', success: (res) => { if (res.confirm) { wx.makePhoneCall({ phoneNumber: '400-123-4567' }); } } }); this.closeMoreMenu(); },
// 显示使用说明
showInstructions() { wx.showModal({ title: '使用说明', content: '1. 描述您或牲畜的症状2. AI助手会分析并提供建议3. 可使用快捷症状选择4. 诊断结果仅供参考,请及时咨询专业兽医', showCancel: false }); this.closeMoreMenu(); }});
|