与牧同行-小程序用户端
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.

279 lines
6.3 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. import http from '../../../utils/api'
  2. Page({
  3. data: {
  4. // 当前时间
  5. currentTime: '',
  6. // 聊天消息
  7. messages: [],
  8. // 输入框相关
  9. inputValue: '',
  10. autoFocus: false,
  11. // 症状选择
  12. quickSymptoms: [],
  13. wzsearch: {},
  14. selectedSymptoms: [],
  15. showSymptomSelector: false,
  16. // 状态控制
  17. isAIThinking: false,
  18. isLoading: false,
  19. loadingText: '',
  20. showMoreMenu: false
  21. },
  22. onLoad() {
  23. this.initData();
  24. this.getInquiry()
  25. },
  26. // AI问诊快捷字列表
  27. getInquiry() {
  28. http.inquiry({
  29. data: {},
  30. success: res => {
  31. console.log(1111, res);
  32. this.setData({
  33. quickSymptoms: res.rows
  34. })
  35. }
  36. })
  37. },
  38. onShow() {
  39. this.updateCurrentTime();
  40. this.setData({
  41. autoFocus: true
  42. });
  43. },
  44. // 初始化数据
  45. initData() {
  46. // 设置当前时间
  47. this.updateCurrentTime();
  48. // 定时更新当前时间
  49. setInterval(() => {
  50. this.updateCurrentTime();
  51. }, 60000);
  52. },
  53. // 更新当前时间
  54. updateCurrentTime() {
  55. const now = new Date();
  56. const timeString = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
  57. this.setData({
  58. currentTime: timeString
  59. });
  60. },
  61. // 输入框变化
  62. onInput(e) {
  63. this.setData({
  64. inputValue: e.detail.value
  65. });
  66. },
  67. // 发送消息
  68. sendMessage() {
  69. const message = this.data.inputValue.trim();
  70. if (!message) return;
  71. // 添加用户消息
  72. const userMessage = {
  73. id: Date.now(),
  74. type: 'user',
  75. content: message,
  76. time: this.getCurrentTime()
  77. };
  78. this.setData({
  79. messages: [...this.data.messages, userMessage],
  80. inputValue: '',
  81. autoFocus: true
  82. });
  83. console.log(666, this.data.messages)
  84. // 先显示AI思考状态
  85. this.setData({
  86. isAIThinking: true
  87. });
  88. // 模拟AI思考时间(1.5-2.5秒)
  89. setTimeout(() => {
  90. this.generateAIResponse(message);
  91. }, 1500 + Math.random() * 1000);
  92. },
  93. // 获取当前时间
  94. getCurrentTime() {
  95. const now = new Date();
  96. return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
  97. },
  98. // 生成AI响应
  99. generateAIResponse(userMessage) {
  100. console.log(888, userMessage);
  101. http.search({
  102. data: {
  103. keyword: userMessage
  104. },
  105. success: res => {
  106. console.log('查询结果', res);
  107. let aiMessage;
  108. if (res.rows && res.rows.length > 0) {
  109. const wzsearch = res.rows[0];
  110. aiMessage = {
  111. id: Date.now() + 1,
  112. type: 'assistant',
  113. content:'根据您的描述,' + wzsearch.title || '已收到您的症状描述',
  114. diagnosis: wzsearch,
  115. time: this.getCurrentTime()
  116. };
  117. } else {
  118. // 如果没有查询到结果
  119. aiMessage = {
  120. id: Date.now() + 1,
  121. type: 'assistant',
  122. content: '已收到您的症状描述',
  123. diagnosis: {
  124. possibleDiseases: '暂无匹配诊断',
  125. severityLevel: '未知',
  126. suggestions: '建议您提供更详细的症状描述,或直接咨询专业兽医'
  127. },
  128. time: this.getCurrentTime()
  129. };
  130. }
  131. // 添加AI消息到聊天记录并隐藏思考状态
  132. this.setData({
  133. messages: [...this.data.messages, aiMessage],
  134. isAIThinking: false
  135. });
  136. console.log(789, this.data.messages);
  137. },
  138. fail: err => {
  139. console.error('API请求失败:', err);
  140. // API失败时的默认响应
  141. const aiMessage = {
  142. id: Date.now() + 1,
  143. type: 'assistant',
  144. content: '已收到您的症状描述',
  145. diagnosis: {
  146. disease: '网络请求失败',
  147. severity: 'unknown',
  148. severityText: '未知',
  149. suggestion: '请检查网络连接后重试,或直接联系兽医'
  150. },
  151. time: this.getCurrentTime()
  152. };
  153. this.setData({
  154. messages: [...this.data.messages, aiMessage],
  155. isAIThinking: false
  156. });
  157. },
  158. complete: () => {
  159. // 确保无论如何都会隐藏思考状态
  160. if (this.data.isAIThinking) {
  161. this.setData({
  162. isAIThinking: false
  163. });
  164. }
  165. }
  166. });
  167. },
  168. // 选择快捷症状
  169. selectQuickSymptom(e) {
  170. const symptom = e.currentTarget.dataset.symptom.keywords;
  171. this.setData({
  172. inputValue: symptom
  173. });
  174. this.sendMessage();
  175. },
  176. // 返回
  177. goBack() {
  178. wx.navigateBack();
  179. },
  180. // 显示更多菜单
  181. showMoreMenu() {
  182. this.setData({
  183. showMoreMenu: true
  184. });
  185. },
  186. // 关闭更多菜单
  187. closeMoreMenu() {
  188. this.setData({
  189. showMoreMenu: false
  190. });
  191. },
  192. // 阻止事件冒泡
  193. stopPropagation() {},
  194. // 清空记录
  195. clearHistory() {
  196. wx.showModal({
  197. title: '提示',
  198. content: '确定要清空所有聊天记录吗?',
  199. success: (res) => {
  200. if (res.confirm) {
  201. this.setData({
  202. messages: [{
  203. id: 1,
  204. type: 'assistant',
  205. content: '您好!我是AI健康助手,有什么可以帮您?\n\n请描述您或牲畜的健康状况,我会为您提供专业的分析和建议。',
  206. time: this.getCurrentTime()
  207. }],
  208. selectedSymptoms: []
  209. });
  210. this.closeMoreMenu();
  211. }
  212. }
  213. });
  214. },
  215. // 导出记录
  216. exportChat() {
  217. wx.showToast({
  218. title: '记录已保存到本地',
  219. icon: 'success'
  220. });
  221. this.closeMoreMenu();
  222. },
  223. // 联系兽医
  224. contactDoctor() {
  225. wx.showModal({
  226. title: '联系兽医',
  227. content: '确定要拨打兽医热线吗?',
  228. success: (res) => {
  229. if (res.confirm) {
  230. wx.makePhoneCall({
  231. phoneNumber: '400-123-4567'
  232. });
  233. }
  234. }
  235. });
  236. this.closeMoreMenu();
  237. },
  238. // 显示使用说明
  239. showInstructions() {
  240. wx.showModal({
  241. title: '使用说明',
  242. content: '1. 描述您或牲畜的症状2. AI助手会分析并提供建议3. 可使用快捷症状选择4. 诊断结果仅供参考,请及时咨询专业兽医',
  243. showCancel: false
  244. });
  245. this.closeMoreMenu();
  246. }
  247. });