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

267 lines
6.0 KiB

// pages/search/index.js
import http from '../../../utils/api'
Page({
data: {
searchValue: '',
searchResults: [],
isLoading: false,
loadingMore: false,
activeCategory: '全部',
categories: [
{ name: '全部', value: '全部' },
{ name: '疾病防治', value: '疾病防治' },
{ name: '饲养管理', value: '饲养管理' }
],
showTipsModal: false,
searchTimer: null,
// 分页相关
pageNum: 1,
pageSize: 10,
total: 0,
hasMore: true,
// 当前搜索参数
currentSearchKey: '',
currentCategory: '全部'
},
onLoad() {
// 初始加载数据
this.loadData(true);
this.gettipList()
},
// 知识库查询提示
gettipList(){
http.tipList({
data:{},
success:res=>{
console.log(res)
const tip = res.rows[0].tips.split('。')
this.setData({
tip:tip,
showTipsModal:true
})
}
})
},
// 加载数据
loadData(isRefresh = false) {
if (isRefresh) {
this.setData({
pageNum: 1,
hasMore: true,
isLoading: true
});
} else {
if (!this.data.hasMore || this.data.loadingMore) return;
this.setData({ loadingMore: true });
}
const params = {
pageNum: this.data.pageNum,
pageSize: this.data.pageSize
};
// 添加搜索关键词
if (this.data.currentSearchKey) {
params.searchKey = this.data.currentSearchKey;
}
// 添加分类筛选(全部不传值)
if (this.data.currentCategory !== '全部') {
params.categoryType = this.data.currentCategory;
}
console.log('加载参数:', params); // 调试用
http.queryList({
data: params,
success: res => {
const newData = res.rows || [];
const total = res.total || 0;
let searchResults;
if (isRefresh) {
searchResults = newData;
} else {
searchResults = [...this.data.searchResults, ...newData];
}
const hasMore = searchResults.length < total;
this.setData({
searchResults,
total,
hasMore,
isLoading: false,
loadingMore: false
});
// 如果没有搜索结果且有关键词,显示提示
if (isRefresh && searchResults.length === 0 && this.data.currentSearchKey) {
wx.showToast({
title: '未找到相关结果',
icon: 'none',
duration: 2000
});
}
},
fail: () => {
this.setData({
isLoading: false,
loadingMore: false
});
wx.showToast({
title: '加载失败',
icon: 'error',
duration: 2000
});
}
});
},
// 输入搜索关键词(自动搜索)
onInputSearch(e) {
const value = e.detail.value;
this.setData({ searchValue: value });
// 清除之前的定时器
if (this.data.searchTimer) {
clearTimeout(this.data.searchTimer);
}
// 设置新的定时器(防抖处理)
this.data.searchTimer = setTimeout(() => {
const keyword = value.trim();
if (keyword) {
// 保持当前分类,只在当前分类中搜索
this.setData({
currentSearchKey: keyword,
// 不清除 currentCategory,保持当前分类筛选
// 只有搜索框清空时才重置分类
});
this.loadData(true);
} else {
// 搜索框清空时,重置为初始状态
this.setData({
currentSearchKey: '',
// 不清除 currentCategory,让用户保持在当前分类
// 如果需要重置,可以取消下面这行的注释
// currentCategory: '全部'
});
this.loadData(true);
}
}, 500); // 500ms防抖延迟
},
// 手动搜索(点击搜索按钮或按回车)
onSearch() {
const keyword = this.data.searchValue.trim();
if (!keyword) {
// 如果搜索框为空,重置搜索
this.setData({
currentSearchKey: '',
// 保持当前分类,不清除分类筛选
});
} else {
// 保持当前分类,只在当前分类中搜索
this.setData({
currentSearchKey: keyword,
// 保持 currentCategory 不变
});
}
this.loadData(true);
},
// 按分类筛选
onFilterCategory(e) {
const category = e.currentTarget.dataset.category;
this.setData({
activeCategory: category,
currentCategory: category,
// 切换分类时清空搜索关键词
currentSearchKey: '',
searchValue: ''
});
this.loadData(true);
},
// 清空搜索
onClearSearch() {
this.setData({
searchValue: '',
currentSearchKey: '',
// 保持当前分类,不清除分类筛选
});
this.loadData(true);
},
// 上拉加载更多
onScrollToLower() {
if (this.data.hasMore && !this.data.loadingMore) {
this.setData({
pageNum: this.data.pageNum + 1
});
this.loadData(false);
}
},
// 隐藏提示弹框
hideTips() {
this.setData({
showTipsModal: false
});
},
// 阻止事件冒泡
stopPropagation() {
return;
},
// 查看详情
onViewDetail(e) {
const item = e.currentTarget.dataset.value;
wx.showModal({
title: item.title,
content: item.content,
showCancel: false,
confirmText: '知道了'
});
},
// 复制内容到剪贴板
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);
}
}
});