| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- // 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) {
- const result = this.data.diagnosisResult
-
- // 创建病历记录
- const medicalRecord = {
- id: Date.now(),
- date: new Date().toISOString().split('T')[0],
- hospital: '智能诊断系统',
- doctor: 'AI医生',
- department: '全科',
- diagnosis: result.diagnosis,
- symptoms: result.symptoms,
- medicines: result.recommendedMedicines.map((name, index) => ({
- id: index + 1,
- name: name,
- price: 0,
- quantity: 1
- })),
- notes: result.suggestion,
- shared: false
- }
- // 保存到全局变量
- const app = getApp()
- app.addMedicalRecord(medicalRecord)
- 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)
- }
- }
- })
- }
- })
|