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

257 lines
6.7 KiB

1 month ago
  1. // 模拟知识库数据
  2. const knowledgeBase = [
  3. {
  4. id: 1,
  5. title: "猪瘟防治",
  6. content: "猪瘟是由猪瘟病毒引起的高度接触性传染病。防治措施:1. 定期接种猪瘟疫苗 2. 加强饲养管理 3. 发现病猪立即隔离 4. 猪舍定期消毒",
  7. category: "疾病防治",
  8. tags: ["猪", "传染病", "疫苗"],
  9. date: "2024-01-15"
  10. },
  11. {
  12. id: 2,
  13. title: "鸡新城疫预防",
  14. content: "新城疫主要症状:呼吸困难、下痢、神经紊乱。预防:1. 接种新城疫疫苗 2. 加强鸡舍卫生 3. 严格控制人员进出 4. 定期检测抗体水平",
  15. category: "疾病防治",
  16. tags: ["鸡", "禽类", "病毒病"],
  17. date: "2024-01-10"
  18. },
  19. {
  20. id: 3,
  21. title: "奶牛饲养管理",
  22. content: "科学饲养要点:1. 合理搭配精粗饲料 2. 保证充足饮水 3. 定时定量饲喂 4. 保持牛舍清洁干燥 5. 定期进行健康检查",
  23. category: "饲养管理",
  24. tags: ["奶牛", "饲养", "管理"],
  25. date: "2024-01-05"
  26. },
  27. {
  28. id: 4,
  29. title: "仔猪腹泻治疗",
  30. content: "常见原因:细菌感染、病毒感染、饲养不当。治疗:1. 补充电解质 2. 使用抗生素(需兽医指导)3. 改善饲养环境 4. 加强母猪管理",
  31. category: "疾病防治",
  32. tags: ["猪", "腹泻", "治疗"],
  33. date: "2024-01-08"
  34. },
  35. {
  36. id: 5,
  37. title: "羊的饲料配方",
  38. content: "育肥羊饲料配方:玉米60%、豆粕20%、麦麸15%、预混料5%。注意事项:1. 逐渐换料 2. 保证粗纤维摄入 3. 添加适量食盐",
  39. category: "饲养管理",
  40. tags: ["羊", "饲料", "营养"],
  41. date: "2024-01-12"
  42. },
  43. {
  44. id: 6,
  45. title: "犬细小病毒防治",
  46. content: "症状:呕吐、腹泻、精神萎靡。防治:1. 定期接种疫苗 2. 发病早期使用血清 3. 补液治疗 4. 严格隔离病犬",
  47. category: "疾病防治",
  48. tags: ["犬", "宠物", "病毒"],
  49. date: "2024-01-18"
  50. },
  51. {
  52. id: 7,
  53. title: "水产养殖水质管理",
  54. content: "水质指标控制:1. pH值7.5-8.5 2. 溶解氧>5mg/L 3. 氨氮<0.2mg/L 4. 定期换水 5. 使用微生物制剂",
  55. category: "饲养管理",
  56. tags: ["水产", "水质", "管理"],
  57. date: "2024-01-14"
  58. }
  59. ];
  60. Page({
  61. data: {
  62. searchValue: '',
  63. searchResults: [],
  64. allKnowledge: knowledgeBase,
  65. recentSearches: [],
  66. isLoading: false,
  67. activeCategory: '全部',
  68. categories: ['全部', '疾病防治', '饲养管理'],
  69. showTipsModal: false,
  70. searchTimer: null // 搜索防抖定时器
  71. },
  72. onLoad() {
  73. // 初始显示所有知识
  74. this.setData({
  75. searchResults: this.data.allKnowledge
  76. });
  77. // 页面加载后显示提示弹框
  78. setTimeout(() => {
  79. this.setData({ showTipsModal: true });
  80. }, 500);
  81. },
  82. onShow() {
  83. const hasShownTips = wx.getStorageSync('hasShownTips');
  84. if (!hasShownTips) {
  85. setTimeout(() => {
  86. this.setData({ showTipsModal: true });
  87. wx.setStorageSync('hasShownTips', true);
  88. }, 500);
  89. }
  90. },
  91. // 输入搜索关键词(自动搜索)
  92. onInputSearch(e) {
  93. const value = e.detail.value;
  94. this.setData({
  95. searchValue: value,
  96. isLoading: true
  97. });
  98. // 清除之前的定时器
  99. if (this.data.searchTimer) {
  100. clearTimeout(this.data.searchTimer);
  101. }
  102. // 设置新的定时器(防抖处理)
  103. this.data.searchTimer = setTimeout(() => {
  104. if (!value.trim()) {
  105. // 如果搜索框为空,显示所有知识
  106. this.setData({
  107. searchResults: this.data.allKnowledge,
  108. isLoading: false
  109. });
  110. } else {
  111. // 执行搜索
  112. this.performSearch(value.trim());
  113. }
  114. }, 300); // 300ms防抖延迟
  115. },
  116. // 执行搜索操作
  117. performSearch(keyword) {
  118. const results = this.searchKnowledge(keyword);
  119. this.setData({
  120. searchResults: results,
  121. isLoading: false
  122. });
  123. // 如果没有搜索结果且有关键词,显示提示
  124. if (results.length === 0 && keyword) {
  125. wx.showToast({
  126. title: '未找到相关结果',
  127. icon: 'none',
  128. duration: 2000
  129. });
  130. }
  131. },
  132. // 手动搜索(点击搜索按钮或按回车)
  133. onSearch() {
  134. const keyword = this.data.searchValue.trim();
  135. if (!keyword) {
  136. this.setData({
  137. searchResults: this.data.allKnowledge
  138. });
  139. return;
  140. }
  141. this.setData({ isLoading: true });
  142. // 清除定时器
  143. if (this.data.searchTimer) {
  144. clearTimeout(this.data.searchTimer);
  145. }
  146. // 立即执行搜索
  147. this.performSearch(keyword);
  148. },
  149. // 搜索知识库
  150. searchKnowledge(keyword) {
  151. const lowerKeyword = keyword.toLowerCase();
  152. return this.data.allKnowledge.filter(item => {
  153. return item.title.toLowerCase().includes(lowerKeyword) ||
  154. item.content.toLowerCase().includes(lowerKeyword) ||
  155. item.tags.some(tag => tag.toLowerCase().includes(lowerKeyword)) ||
  156. item.category.toLowerCase().includes(lowerKeyword);
  157. });
  158. },
  159. // 隐藏提示弹框
  160. hideTips() {
  161. this.setData({ showTipsModal: false });
  162. },
  163. // 阻止事件冒泡
  164. stopPropagation() {
  165. return;
  166. },
  167. // 查看详情
  168. onViewDetail(e) {
  169. const id = e.currentTarget.dataset.id;
  170. const item = this.data.allKnowledge.find(item => item.id === id);
  171. if (item) {
  172. wx.showModal({
  173. title: item.title,
  174. content: item.content,
  175. showCancel: false,
  176. confirmText: '知道了'
  177. });
  178. }
  179. },
  180. // 按分类筛选
  181. onFilterCategory(e) {
  182. const category = e.currentTarget.dataset.category;
  183. this.setData({
  184. activeCategory: category,
  185. searchValue: '' // 清空搜索框
  186. });
  187. if (category === '全部') {
  188. this.setData({
  189. searchResults: this.data.allKnowledge
  190. });
  191. } else {
  192. const results = this.data.allKnowledge.filter(item =>
  193. item.category === category
  194. );
  195. this.setData({
  196. searchResults: results
  197. });
  198. }
  199. },
  200. // 清空搜索
  201. onClearSearch() {
  202. this.setData({
  203. searchValue: '',
  204. searchResults: this.data.allKnowledge,
  205. activeCategory: '全部'
  206. });
  207. },
  208. // 复制内容到剪贴板
  209. onCopyContent(e) {
  210. const content = e.currentTarget.dataset.content;
  211. wx.setClipboardData({
  212. data: content,
  213. success: () => {
  214. wx.showToast({
  215. title: '复制成功',
  216. icon: 'success'
  217. });
  218. }
  219. });
  220. },
  221. onShareAppMessage() {
  222. return {
  223. title: '动物疾病防治与饲养管理知识库',
  224. path: '/pages/search/index'
  225. };
  226. },
  227. onUnload() {
  228. // 页面卸载时清除定时器
  229. if (this.data.searchTimer) {
  230. clearTimeout(this.data.searchTimer);
  231. }
  232. }
  233. });