与牧同行-小程序用户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
7.6 KiB

  1. Page({
  2. data: {
  3. // 当前时间
  4. currentTime: '',
  5. // 聊天消息
  6. messages: [],
  7. // 输入框相关
  8. inputValue: '',
  9. autoFocus: false,
  10. // 症状选择
  11. quickSymptoms: [
  12. '发热', '咳嗽', '腹泻', '食欲不振',
  13. '精神萎靡', '呼吸困难', '体重下降', '皮肤问题'
  14. ],
  15. selectedSymptoms: [],
  16. showSymptomSelector: false,
  17. // 症状数据
  18. symptoms: [
  19. { id: 1, name: '发热', category: '全身症状' },
  20. { id: 2, name: '咳嗽', category: '呼吸系统' },
  21. { id: 3, name: '腹泻', category: '消化系统' },
  22. { id: 4, name: '呕吐', category: '消化系统' },
  23. { id: 5, name: '食欲不振', category: '消化系统' },
  24. { id: 6, name: '呼吸困难', category: '呼吸系统' },
  25. { id: 7, name: '精神萎靡', category: '神经系统' },
  26. { id: 8, name: '体重下降', category: '全身症状' },
  27. { id: 9, name: '皮肤问题', category: '皮肤系统' },
  28. { id: 10, name: '跛行', category: '运动系统' },
  29. { id: 11, name: '眼部分泌物', category: '五官' },
  30. { id: 12, name: '流鼻涕', category: '呼吸系统' }
  31. ],
  32. // 状态控制
  33. isAIThinking: false,
  34. isLoading: false,
  35. loadingText: '',
  36. showMoreMenu: false
  37. },
  38. onLoad() {
  39. this.initData();
  40. },
  41. onShow() {
  42. this.updateCurrentTime();
  43. this.setData({ autoFocus: true });
  44. },
  45. // 初始化数据
  46. initData() {
  47. // 设置当前时间
  48. this.updateCurrentTime();
  49. // 定时更新当前时间
  50. setInterval(() => {
  51. this.updateCurrentTime();
  52. }, 60000);
  53. },
  54. // 更新当前时间
  55. updateCurrentTime() {
  56. const now = new Date();
  57. const timeString = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
  58. this.setData({ currentTime: timeString });
  59. },
  60. // 输入框变化
  61. onInput(e) {
  62. this.setData({ inputValue: e.detail.value });
  63. },
  64. // 发送消息
  65. sendMessage() {
  66. const message = this.data.inputValue.trim();
  67. if (!message) return;
  68. // 添加用户消息
  69. const userMessage = {
  70. id: Date.now(),
  71. type: 'user',
  72. content: message,
  73. time: this.getCurrentTime()
  74. };
  75. this.setData({
  76. messages: [...this.data.messages, userMessage],
  77. inputValue: '',
  78. autoFocus: true
  79. });
  80. // 模拟AI思考
  81. this.simulateAIResponse(message);
  82. },
  83. // 获取当前时间
  84. getCurrentTime() {
  85. const now = new Date();
  86. return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
  87. },
  88. // 模拟AI响应
  89. simulateAIResponse(userMessage) {
  90. this.setData({ isAIThinking: true });
  91. // 模拟AI思考时间
  92. setTimeout(() => {
  93. const aiMessage = this.generateAIResponse(userMessage);
  94. console.log(333,aiMessage);
  95. this.setData({
  96. messages: [...this.data.messages, aiMessage],
  97. isAIThinking: false
  98. });
  99. }, 1500 + Math.random() * 1000);
  100. },
  101. // 生成AI响应
  102. generateAIResponse(userMessage) {
  103. console.log(444,userMessage);
  104. const responses = {
  105. '发热': {
  106. content: '根据您的描述,牲畜出现发热症状。',
  107. diagnosis: {
  108. disease: '可能为感染性疾病或中暑',
  109. severity: 'moderate',
  110. severityText: '中度',
  111. suggestion: '建议:1. 隔离观察 2. 监测体温 3. 提供充足饮水 4. 如持续发热,及时联系兽医'
  112. }
  113. },
  114. '咳嗽': {
  115. content: '咳嗽症状提示可能存在呼吸系统问题。',
  116. diagnosis: {
  117. disease: '可能为呼吸道感染或肺炎',
  118. severity: 'low',
  119. severityText: '轻度',
  120. suggestion: '建议:1. 保持圈舍通风 2. 避免粉尘 3. 观察呼吸频率 4. 如有加重及时就医'
  121. }
  122. },
  123. '腹泻': {
  124. content: '腹泻症状需要关注,可能由多种原因引起。',
  125. diagnosis: {
  126. disease: '可能为消化不良或肠胃感染',
  127. severity: 'moderate',
  128. severityText: '中度',
  129. suggestion: '建议:1. 调整饲料 2. 补充电解质 3. 观察粪便性状 4. 如持续腹泻需兽医检查'
  130. }
  131. }
  132. };
  133. // 匹配症状关键词
  134. let response = responses['发热']; // 默认响应
  135. for (const [symptom, data] of Object.entries(responses)) {
  136. if (userMessage.includes(symptom)) {
  137. response = data;
  138. break;
  139. }
  140. }
  141. return {
  142. id: Date.now() + 1,
  143. type: 'assistant',
  144. content: response.content,
  145. diagnosis: response.diagnosis,
  146. time: this.getCurrentTime()
  147. };
  148. },
  149. // 选择快捷症状
  150. selectQuickSymptom(e) {
  151. console.log(1111,e);
  152. const symptom = e.currentTarget.dataset.symptom;
  153. this.setData({ inputValue: symptom });
  154. this.sendMessage();
  155. },
  156. // 切换症状选择器
  157. toggleSymptomSelector() {
  158. this.setData({ showSymptomSelector: !this.data.showSymptomSelector });
  159. },
  160. // 关闭症状选择器
  161. closeSymptomSelector() {
  162. this.setData({ showSymptomSelector: false });
  163. },
  164. // 症状选择
  165. onSymptomSelect(e) {
  166. const { symptom } = e.detail;
  167. const selectedSymptoms = [...this.data.selectedSymptoms];
  168. const index = selectedSymptoms.findIndex(s => s.id === symptom.id);
  169. if (index > -1) {
  170. selectedSymptoms.splice(index, 1);
  171. } else {
  172. selectedSymptoms.push(symptom);
  173. }
  174. this.setData({ selectedSymptoms });
  175. },
  176. // 确认症状选择
  177. confirmSymptoms() {
  178. if (this.data.selectedSymptoms.length === 0) {
  179. wx.showToast({
  180. title: '请选择至少一个症状',
  181. icon: 'none'
  182. });
  183. return;
  184. }
  185. const symptomNames = this.data.selectedSymptoms.map(s => s.name).join('、');
  186. this.setData({
  187. inputValue: `我的牲畜出现以下症状:${symptomNames}`,
  188. showSymptomSelector: false
  189. });
  190. },
  191. // 返回
  192. goBack() {
  193. wx.navigateBack();
  194. },
  195. // 显示更多菜单
  196. showMoreMenu() {
  197. this.setData({ showMoreMenu: true });
  198. },
  199. // 关闭更多菜单
  200. closeMoreMenu() {
  201. this.setData({ showMoreMenu: false });
  202. },
  203. // 阻止事件冒泡
  204. stopPropagation() {},
  205. // 清空记录
  206. clearHistory() {
  207. wx.showModal({
  208. title: '提示',
  209. content: '确定要清空所有聊天记录吗?',
  210. success: (res) => {
  211. if (res.confirm) {
  212. this.setData({
  213. messages: [{
  214. id: 1,
  215. type: 'assistant',
  216. content: '您好!我是AI健康助手,有什么可以帮您?\n\n请描述您或牲畜的健康状况,我会为您提供专业的分析和建议。',
  217. time: this.getCurrentTime()
  218. }],
  219. selectedSymptoms: []
  220. });
  221. this.closeMoreMenu();
  222. }
  223. }
  224. });
  225. },
  226. // 导出记录
  227. exportChat() {
  228. wx.showToast({
  229. title: '记录已保存到本地',
  230. icon: 'success'
  231. });
  232. this.closeMoreMenu();
  233. },
  234. // 联系兽医
  235. contactDoctor() {
  236. wx.showModal({
  237. title: '联系兽医',
  238. content: '确定要拨打兽医热线吗?',
  239. success: (res) => {
  240. if (res.confirm) {
  241. wx.makePhoneCall({
  242. phoneNumber: '400-123-4567'
  243. });
  244. }
  245. }
  246. });
  247. this.closeMoreMenu();
  248. },
  249. // 显示使用说明
  250. showInstructions() {
  251. wx.showModal({
  252. title: '使用说明',
  253. content: '1. 描述您或牲畜的症状\n2. AI助手会分析并提供建议\n3. 可使用快捷症状选择\n4. 诊断结果仅供参考,请及时咨询专业兽医',
  254. showCancel: false
  255. });
  256. this.closeMoreMenu();
  257. }
  258. });