diagnosis.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. const result = this.data.diagnosisResult
  128. // 创建病历记录
  129. const medicalRecord = {
  130. id: Date.now(),
  131. date: new Date().toISOString().split('T')[0],
  132. hospital: '智能诊断系统',
  133. doctor: 'AI医生',
  134. department: '全科',
  135. diagnosis: result.diagnosis,
  136. symptoms: result.symptoms,
  137. medicines: result.recommendedMedicines.map((name, index) => ({
  138. id: index + 1,
  139. name: name,
  140. price: 0,
  141. quantity: 1
  142. })),
  143. notes: result.suggestion,
  144. shared: false
  145. }
  146. // 保存到全局变量
  147. const app = getApp()
  148. app.addMedicalRecord(medicalRecord)
  149. wx.showToast({
  150. title: '已保存到病历记录',
  151. icon: 'success'
  152. })
  153. }
  154. },
  155. // 导航到药品购买页面
  156. navigateToMedicine(e) {
  157. const medicineName = e.currentTarget.dataset.medicine
  158. wx.showModal({
  159. title: '购买药品',
  160. content: `是否要购买"${medicineName}"?`,
  161. success: (res) => {
  162. if (res.confirm) {
  163. // 切换到下单页面并搜索该药品
  164. wx.switchTab({
  165. url: '/pages/order/order'
  166. })
  167. // 这里可以传递参数到下单页面进行自动搜索
  168. setTimeout(() => {
  169. const pages = getCurrentPages()
  170. const orderPage = pages.find(page => page.route === 'pages/order/order')
  171. if (orderPage) {
  172. orderPage.setData({
  173. currentTab: 1,
  174. medicineKeyword: medicineName
  175. })
  176. }
  177. }, 500)
  178. }
  179. }
  180. })
  181. }
  182. })