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

242 lines
4.9 KiB

  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.simulateAIResponse(message);
  86. },
  87. // 获取当前时间
  88. getCurrentTime() {
  89. const now = new Date();
  90. return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
  91. },
  92. // 模拟AI响应
  93. simulateAIResponse(userMessage) {
  94. this.setData({
  95. isAIThinking: true
  96. });
  97. console.log(888, userMessage);
  98. // 模拟AI思考时间
  99. setTimeout(() => {
  100. const aiMessage = this.generateAIResponse(userMessage);
  101. console.log(333, aiMessage);
  102. this.setData({
  103. messages: [...this.data.messages, aiMessage],
  104. isAIThinking: false
  105. });
  106. console.log(789, this.data.messages);
  107. }, 1500 + Math.random() * 1000);
  108. },
  109. // 生成AI响应
  110. generateAIResponse(userMessage) {
  111. console.log(444, userMessage);
  112. http.search({
  113. data: {
  114. keyword: userMessage
  115. },
  116. success: res => {
  117. console.log('查询结果', res)
  118. const wzsearch = res.rows[0]
  119. return {
  120. id: Date.now() + 1,
  121. type: 'assistant',
  122. content: wzsearch.title,
  123. diagnosis: wzsearch,
  124. time: this.getCurrentTime()
  125. };
  126. }
  127. })
  128. },
  129. // 选择快捷症状
  130. selectQuickSymptom(e) {
  131. const symptom = e.currentTarget.dataset.symptom.keywords;
  132. this.setData({
  133. inputValue: symptom
  134. });
  135. this.sendMessage();
  136. },
  137. // 返回
  138. goBack() {
  139. wx.navigateBack();
  140. },
  141. // 显示更多菜单
  142. showMoreMenu() {
  143. this.setData({
  144. showMoreMenu: true
  145. });
  146. },
  147. // 关闭更多菜单
  148. closeMoreMenu() {
  149. this.setData({
  150. showMoreMenu: false
  151. });
  152. },
  153. // 阻止事件冒泡
  154. stopPropagation() {},
  155. // 清空记录
  156. clearHistory() {
  157. wx.showModal({
  158. title: '提示',
  159. content: '确定要清空所有聊天记录吗?',
  160. success: (res) => {
  161. if (res.confirm) {
  162. this.setData({
  163. messages: [{
  164. id: 1,
  165. type: 'assistant',
  166. content: '您好!我是AI健康助手,有什么可以帮您?\n\n请描述您或牲畜的健康状况,我会为您提供专业的分析和建议。',
  167. time: this.getCurrentTime()
  168. }],
  169. selectedSymptoms: []
  170. });
  171. this.closeMoreMenu();
  172. }
  173. }
  174. });
  175. },
  176. // 导出记录
  177. exportChat() {
  178. wx.showToast({
  179. title: '记录已保存到本地',
  180. icon: 'success'
  181. });
  182. this.closeMoreMenu();
  183. },
  184. // 联系兽医
  185. contactDoctor() {
  186. wx.showModal({
  187. title: '联系兽医',
  188. content: '确定要拨打兽医热线吗?',
  189. success: (res) => {
  190. if (res.confirm) {
  191. wx.makePhoneCall({
  192. phoneNumber: '400-123-4567'
  193. });
  194. }
  195. }
  196. });
  197. this.closeMoreMenu();
  198. },
  199. // 显示使用说明
  200. showInstructions() {
  201. wx.showModal({
  202. title: '使用说明',
  203. content: '1. 描述您或牲畜的症状2. AI助手会分析并提供建议3. 可使用快捷症状选择4. 诊断结果仅供参考,请及时咨询专业兽医',
  204. showCancel: false
  205. });
  206. this.closeMoreMenu();
  207. }
  208. });