// diagnosis.js const mockData = require('../../utils/mockData.js') Page({ data: { symptomText: '', commonSymptoms: ['头痛', '发烧', '咳嗽', '流鼻涕', '胃痛', '恶心', '皮肤瘙痒', '关节疼痛'], isDiagnosing: false, diagnosisResult: null, diagnosisHistory: [] }, onLoad() { console.log('智能诊断页面加载完成') // 加载诊断历史 this.loadDiagnosisHistory() }, // 输入症状 onSymptomInput(e) { this.setData({ symptomText: e.detail.value }) }, // 添加常见症状 addSymptom(e) { const symptom = e.currentTarget.dataset.symptom const currentText = this.data.symptomText const newText = currentText ? `${currentText}、${symptom}` : symptom this.setData({ symptomText: newText }) }, // 开始诊断 startDiagnosis() { if (!this.data.symptomText.trim()) { wx.showToast({ title: '请输入症状描述', icon: 'none' }) return } this.setData({ isDiagnosing: true }) // 模拟AI诊断过程 setTimeout(() => { const result = this.analyzeSymptoms(this.data.symptomText) this.setData({ isDiagnosing: false, diagnosisResult: result }) // 保存到历史记录 this.saveToHistory(result) }, 2000) }, // 分析症状(模拟AI诊断) analyzeSymptoms(symptomText) { const symptoms = symptomText.split(/[、,,。.\s]+/).filter(s => s.trim()) // 简单的关键词匹配算法 for (const [condition, data] of Object.entries(mockData.aiDiagnosis)) { const matchedSymptoms = data.symptoms.filter(symptom => symptoms.some(s => s.includes(symptom) || symptom.includes(s)) ) if (matchedSymptoms.length >= Math.min(2, data.symptoms.length)) { return { condition: condition, diagnosis: data.diagnosis, symptoms: matchedSymptoms, suggestion: data.suggestion, recommendedMedicines: data.recommendedMedicines } } } // 如果没有匹配到特定疾病,返回通用建议 return { condition: '一般不适', diagnosis: '症状需要进一步观察', symptoms: symptoms, suggestion: '建议多休息,注意饮食,如症状持续或加重请及时就医', recommendedMedicines: [] } }, // 保存诊断记录 saveToHistory(result) { const historyItem = { id: Date.now(), date: new Date().toLocaleString(), symptoms: this.data.symptomText, diagnosis: result.diagnosis, condition: result.condition, suggestion: result.suggestion } const newHistory = [historyItem, ...this.data.diagnosisHistory].slice(0, 10) // 保留最近10条 this.setData({ diagnosisHistory: newHistory }) // 保存到本地存储 wx.setStorageSync('diagnosisHistory', newHistory) }, // 加载诊断历史 loadDiagnosisHistory() { const history = wx.getStorageSync('diagnosisHistory') || [] this.setData({ diagnosisHistory: history }) }, // 查看历史记录 viewHistory(e) { const item = e.currentTarget.dataset.item this.setData({ diagnosisResult: { diagnosis: item.diagnosis, symptoms: item.symptoms.split(/[、,,。.\s]+/).filter(s => s.trim()), suggestion: item.suggestion, recommendedMedicines: [] } }) }, // 重置诊断 resetDiagnosis() { this.setData({ symptomText: '', diagnosisResult: null }) }, // 保存诊断记录 saveDiagnosis() { if (this.data.diagnosisResult) { wx.showToast({ title: '已保存到诊断历史', icon: 'success' }) } }, // 导航到药品购买页面 navigateToMedicine(e) { const medicineName = e.currentTarget.dataset.medicine wx.showModal({ title: '购买药品', content: `是否要购买"${medicineName}"?`, success: (res) => { if (res.confirm) { // 切换到下单页面并搜索该药品 wx.switchTab({ url: '/pages/order/order' }) // 这里可以传递参数到下单页面进行自动搜索 setTimeout(() => { const pages = getCurrentPages() const orderPage = pages.find(page => page.route === 'pages/order/order') if (orderPage) { orderPage.setData({ currentTab: 1, medicineKeyword: medicineName }) } }, 500) } } }) } })