diagnosis.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // diagnosis.js
  2. const mockData = require('../../utils/mockData.js')
  3. Page({
  4. data: {
  5. symptomText: '',
  6. commonSymptoms: ['头痛', '发烧', '咳嗽', '流鼻涕', '胃痛', '恶心', '皮肤瘙痒', '关节疼痛'],
  7. isDiagnosing: false,
  8. diagnosisResult: null,
  9. diagnosisHistory: []
  10. },
  11. onLoad() {
  12. console.log('智能诊断页面加载完成')
  13. // 加载诊断历史
  14. this.loadDiagnosisHistory()
  15. },
  16. // 输入症状
  17. onSymptomInput(e) {
  18. this.setData({
  19. symptomText: e.detail.value
  20. })
  21. },
  22. // 添加常见症状
  23. addSymptom(e) {
  24. const symptom = e.currentTarget.dataset.symptom
  25. const currentText = this.data.symptomText
  26. const newText = currentText ? `${currentText}、${symptom}` : symptom
  27. this.setData({
  28. symptomText: newText
  29. })
  30. },
  31. // 开始诊断
  32. startDiagnosis() {
  33. if (!this.data.symptomText.trim()) {
  34. wx.showToast({
  35. title: '请输入症状描述',
  36. icon: 'none'
  37. })
  38. return
  39. }
  40. this.setData({
  41. isDiagnosing: true
  42. })
  43. // 模拟AI诊断过程
  44. setTimeout(() => {
  45. const result = this.analyzeSymptoms(this.data.symptomText)
  46. this.setData({
  47. isDiagnosing: false,
  48. diagnosisResult: result
  49. })
  50. // 保存到历史记录
  51. this.saveToHistory(result)
  52. }, 2000)
  53. },
  54. // 分析症状(模拟AI诊断)
  55. analyzeSymptoms(symptomText) {
  56. const symptoms = symptomText.split(/[、,,。.\s]+/).filter(s => s.trim())
  57. // 简单的关键词匹配算法
  58. for (const [condition, data] of Object.entries(mockData.aiDiagnosis)) {
  59. const matchedSymptoms = data.symptoms.filter(symptom =>
  60. symptoms.some(s => s.includes(symptom) || symptom.includes(s))
  61. )
  62. if (matchedSymptoms.length >= Math.min(2, data.symptoms.length)) {
  63. return {
  64. condition: condition,
  65. diagnosis: data.diagnosis,
  66. symptoms: matchedSymptoms,
  67. suggestion: data.suggestion,
  68. recommendedMedicines: data.recommendedMedicines
  69. }
  70. }
  71. }
  72. // 如果没有匹配到特定疾病,返回通用建议
  73. return {
  74. condition: '一般不适',
  75. diagnosis: '症状需要进一步观察',
  76. symptoms: symptoms,
  77. suggestion: '建议多休息,注意饮食,如症状持续或加重请及时就医',
  78. recommendedMedicines: []
  79. }
  80. },
  81. // 保存诊断记录
  82. saveToHistory(result) {
  83. const historyItem = {
  84. id: Date.now(),
  85. date: new Date().toLocaleString(),
  86. symptoms: this.data.symptomText,
  87. diagnosis: result.diagnosis,
  88. condition: result.condition,
  89. suggestion: result.suggestion
  90. }
  91. const newHistory = [historyItem, ...this.data.diagnosisHistory].slice(0, 10) // 保留最近10条
  92. this.setData({
  93. diagnosisHistory: newHistory
  94. })
  95. // 保存到本地存储
  96. wx.setStorageSync('diagnosisHistory', newHistory)
  97. },
  98. // 加载诊断历史
  99. loadDiagnosisHistory() {
  100. const history = wx.getStorageSync('diagnosisHistory') || []
  101. this.setData({
  102. diagnosisHistory: history
  103. })
  104. },
  105. // 查看历史记录
  106. viewHistory(e) {
  107. const item = e.currentTarget.dataset.item
  108. this.setData({
  109. diagnosisResult: {
  110. diagnosis: item.diagnosis,
  111. symptoms: item.symptoms.split(/[、,,。.\s]+/).filter(s => s.trim()),
  112. suggestion: item.suggestion,
  113. recommendedMedicines: []
  114. }
  115. })
  116. },
  117. // 重置诊断
  118. resetDiagnosis() {
  119. this.setData({
  120. symptomText: '',
  121. diagnosisResult: null
  122. })
  123. },
  124. // 保存诊断记录
  125. saveDiagnosis() {
  126. if (this.data.diagnosisResult) {
  127. wx.showToast({
  128. title: '已保存到诊断历史',
  129. icon: 'success'
  130. })
  131. }
  132. },
  133. // 导航到药品购买页面
  134. navigateToMedicine(e) {
  135. const medicineName = e.currentTarget.dataset.medicine
  136. wx.showModal({
  137. title: '购买药品',
  138. content: `是否要购买"${medicineName}"?`,
  139. success: (res) => {
  140. if (res.confirm) {
  141. // 切换到下单页面并搜索该药品
  142. wx.switchTab({
  143. url: '/pages/order/order'
  144. })
  145. // 这里可以传递参数到下单页面进行自动搜索
  146. setTimeout(() => {
  147. const pages = getCurrentPages()
  148. const orderPage = pages.find(page => page.route === 'pages/order/order')
  149. if (orderPage) {
  150. orderPage.setData({
  151. currentTab: 1,
  152. medicineKeyword: medicineName
  153. })
  154. }
  155. }, 500)
  156. }
  157. }
  158. })
  159. }
  160. })