|
|
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.simulateAIResponse(message); },
// 获取当前时间
getCurrentTime() { const now = new Date(); return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`; },
// 模拟AI响应
simulateAIResponse(userMessage) { this.setData({ isAIThinking: true });
console.log(888, userMessage); // 模拟AI思考时间
setTimeout(() => { const aiMessage = this.generateAIResponse(userMessage); console.log(333, aiMessage); this.setData({ messages: [...this.data.messages, aiMessage], isAIThinking: false }); console.log(789, this.data.messages); }, 1500 + Math.random() * 1000);
},
// 生成AI响应
generateAIResponse(userMessage) { console.log(444, userMessage); http.search({ data: { keyword: userMessage }, success: res => { console.log('查询结果', res) const wzsearch = res.rows[0]
return { id: Date.now() + 1, type: 'assistant', content: wzsearch.title, diagnosis: wzsearch, time: this.getCurrentTime() }; } })
},
// 选择快捷症状
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(); }});
|