// 模拟知识库数据 const knowledgeBase = [ { id: 1, title: "猪瘟防治", content: "猪瘟是由猪瘟病毒引起的高度接触性传染病。防治措施:1. 定期接种猪瘟疫苗 2. 加强饲养管理 3. 发现病猪立即隔离 4. 猪舍定期消毒", category: "疾病防治", tags: ["猪", "传染病", "疫苗"], date: "2024-01-15" }, { id: 2, title: "鸡新城疫预防", content: "新城疫主要症状:呼吸困难、下痢、神经紊乱。预防:1. 接种新城疫疫苗 2. 加强鸡舍卫生 3. 严格控制人员进出 4. 定期检测抗体水平", category: "疾病防治", tags: ["鸡", "禽类", "病毒病"], date: "2024-01-10" }, { id: 3, title: "奶牛饲养管理", content: "科学饲养要点:1. 合理搭配精粗饲料 2. 保证充足饮水 3. 定时定量饲喂 4. 保持牛舍清洁干燥 5. 定期进行健康检查", category: "饲养管理", tags: ["奶牛", "饲养", "管理"], date: "2024-01-05" }, { id: 4, title: "仔猪腹泻治疗", content: "常见原因:细菌感染、病毒感染、饲养不当。治疗:1. 补充电解质 2. 使用抗生素(需兽医指导)3. 改善饲养环境 4. 加强母猪管理", category: "疾病防治", tags: ["猪", "腹泻", "治疗"], date: "2024-01-08" }, { id: 5, title: "羊的饲料配方", content: "育肥羊饲料配方:玉米60%、豆粕20%、麦麸15%、预混料5%。注意事项:1. 逐渐换料 2. 保证粗纤维摄入 3. 添加适量食盐", category: "饲养管理", tags: ["羊", "饲料", "营养"], date: "2024-01-12" }, { id: 6, title: "犬细小病毒防治", content: "症状:呕吐、腹泻、精神萎靡。防治:1. 定期接种疫苗 2. 发病早期使用血清 3. 补液治疗 4. 严格隔离病犬", category: "疾病防治", tags: ["犬", "宠物", "病毒"], date: "2024-01-18" }, { id: 7, title: "水产养殖水质管理", content: "水质指标控制:1. pH值7.5-8.5 2. 溶解氧>5mg/L 3. 氨氮<0.2mg/L 4. 定期换水 5. 使用微生物制剂", category: "饲养管理", tags: ["水产", "水质", "管理"], date: "2024-01-14" } ]; Page({ data: { searchValue: '', searchResults: [], allKnowledge: knowledgeBase, recentSearches: [], isLoading: false, activeCategory: '全部', categories: ['全部', '疾病防治', '饲养管理'], showTipsModal: false, searchTimer: null // 搜索防抖定时器 }, onLoad() { // 初始显示所有知识 this.setData({ searchResults: this.data.allKnowledge }); // 页面加载后显示提示弹框 setTimeout(() => { this.setData({ showTipsModal: true }); }, 500); }, onShow() { const hasShownTips = wx.getStorageSync('hasShownTips'); if (!hasShownTips) { setTimeout(() => { this.setData({ showTipsModal: true }); wx.setStorageSync('hasShownTips', true); }, 500); } }, // 输入搜索关键词(自动搜索) onInputSearch(e) { const value = e.detail.value; this.setData({ searchValue: value, isLoading: true }); // 清除之前的定时器 if (this.data.searchTimer) { clearTimeout(this.data.searchTimer); } // 设置新的定时器(防抖处理) this.data.searchTimer = setTimeout(() => { if (!value.trim()) { // 如果搜索框为空,显示所有知识 this.setData({ searchResults: this.data.allKnowledge, isLoading: false }); } else { // 执行搜索 this.performSearch(value.trim()); } }, 300); // 300ms防抖延迟 }, // 执行搜索操作 performSearch(keyword) { const results = this.searchKnowledge(keyword); this.setData({ searchResults: results, isLoading: false }); // 如果没有搜索结果且有关键词,显示提示 if (results.length === 0 && keyword) { wx.showToast({ title: '未找到相关结果', icon: 'none', duration: 2000 }); } }, // 手动搜索(点击搜索按钮或按回车) onSearch() { const keyword = this.data.searchValue.trim(); if (!keyword) { this.setData({ searchResults: this.data.allKnowledge }); return; } this.setData({ isLoading: true }); // 清除定时器 if (this.data.searchTimer) { clearTimeout(this.data.searchTimer); } // 立即执行搜索 this.performSearch(keyword); }, // 搜索知识库 searchKnowledge(keyword) { const lowerKeyword = keyword.toLowerCase(); return this.data.allKnowledge.filter(item => { return item.title.toLowerCase().includes(lowerKeyword) || item.content.toLowerCase().includes(lowerKeyword) || item.tags.some(tag => tag.toLowerCase().includes(lowerKeyword)) || item.category.toLowerCase().includes(lowerKeyword); }); }, // 隐藏提示弹框 hideTips() { this.setData({ showTipsModal: false }); }, // 阻止事件冒泡 stopPropagation() { return; }, // 查看详情 onViewDetail(e) { const id = e.currentTarget.dataset.id; const item = this.data.allKnowledge.find(item => item.id === id); if (item) { wx.showModal({ title: item.title, content: item.content, showCancel: false, confirmText: '知道了' }); } }, // 按分类筛选 onFilterCategory(e) { const category = e.currentTarget.dataset.category; this.setData({ activeCategory: category, searchValue: '' // 清空搜索框 }); if (category === '全部') { this.setData({ searchResults: this.data.allKnowledge }); } else { const results = this.data.allKnowledge.filter(item => item.category === category ); this.setData({ searchResults: results }); } }, // 清空搜索 onClearSearch() { this.setData({ searchValue: '', searchResults: this.data.allKnowledge, activeCategory: '全部' }); }, // 复制内容到剪贴板 onCopyContent(e) { const content = e.currentTarget.dataset.content; wx.setClipboardData({ data: content, success: () => { wx.showToast({ title: '复制成功', icon: 'success' }); } }); }, onShareAppMessage() { return { title: '动物疾病防治与饲养管理知识库', path: '/pages/search/index' }; }, onUnload() { // 页面卸载时清除定时器 if (this.data.searchTimer) { clearTimeout(this.data.searchTimer); } } });