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

266 lines
6.0 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 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 month ago
  1. // pages/search/index.js
  2. import http from '../../../utils/api'
  3. Page({
  4. data: {
  5. searchValue: '',
  6. searchResults: [],
  7. isLoading: false,
  8. loadingMore: false,
  9. activeCategory: '全部',
  10. categories: [
  11. { name: '全部', value: '全部' },
  12. { name: '疾病防治', value: '疾病防治' },
  13. { name: '饲养管理', value: '饲养管理' }
  14. ],
  15. showTipsModal: false,
  16. searchTimer: null,
  17. // 分页相关
  18. pageNum: 1,
  19. pageSize: 10,
  20. total: 0,
  21. hasMore: true,
  22. // 当前搜索参数
  23. currentSearchKey: '',
  24. currentCategory: '全部'
  25. },
  26. onLoad() {
  27. // 初始加载数据
  28. this.loadData(true);
  29. this.gettipList()
  30. },
  31. // 知识库查询提示
  32. gettipList(){
  33. http.tipList({
  34. data:{},
  35. success:res=>{
  36. console.log(res)
  37. const tip = res.rows[0].tips.split('。')
  38. this.setData({
  39. tip:tip,
  40. showTipsModal:true
  41. })
  42. }
  43. })
  44. },
  45. // 加载数据
  46. loadData(isRefresh = false) {
  47. if (isRefresh) {
  48. this.setData({
  49. pageNum: 1,
  50. hasMore: true,
  51. isLoading: true
  52. });
  53. } else {
  54. if (!this.data.hasMore || this.data.loadingMore) return;
  55. this.setData({ loadingMore: true });
  56. }
  57. const params = {
  58. pageNum: this.data.pageNum,
  59. pageSize: this.data.pageSize
  60. };
  61. // 添加搜索关键词
  62. if (this.data.currentSearchKey) {
  63. params.searchKey = this.data.currentSearchKey;
  64. }
  65. // 添加分类筛选(全部不传值)
  66. if (this.data.currentCategory !== '全部') {
  67. params.categoryType = this.data.currentCategory;
  68. }
  69. console.log('加载参数:', params); // 调试用
  70. http.queryList({
  71. data: params,
  72. success: res => {
  73. const newData = res.rows || [];
  74. const total = res.total || 0;
  75. let searchResults;
  76. if (isRefresh) {
  77. searchResults = newData;
  78. } else {
  79. searchResults = [...this.data.searchResults, ...newData];
  80. }
  81. const hasMore = searchResults.length < total;
  82. this.setData({
  83. searchResults,
  84. total,
  85. hasMore,
  86. isLoading: false,
  87. loadingMore: false
  88. });
  89. // 如果没有搜索结果且有关键词,显示提示
  90. if (isRefresh && searchResults.length === 0 && this.data.currentSearchKey) {
  91. wx.showToast({
  92. title: '未找到相关结果',
  93. icon: 'none',
  94. duration: 2000
  95. });
  96. }
  97. },
  98. fail: () => {
  99. this.setData({
  100. isLoading: false,
  101. loadingMore: false
  102. });
  103. wx.showToast({
  104. title: '加载失败',
  105. icon: 'error',
  106. duration: 2000
  107. });
  108. }
  109. });
  110. },
  111. // 输入搜索关键词(自动搜索)
  112. onInputSearch(e) {
  113. const value = e.detail.value;
  114. this.setData({ searchValue: value });
  115. // 清除之前的定时器
  116. if (this.data.searchTimer) {
  117. clearTimeout(this.data.searchTimer);
  118. }
  119. // 设置新的定时器(防抖处理)
  120. this.data.searchTimer = setTimeout(() => {
  121. const keyword = value.trim();
  122. if (keyword) {
  123. // 保持当前分类,只在当前分类中搜索
  124. this.setData({
  125. currentSearchKey: keyword,
  126. // 不清除 currentCategory,保持当前分类筛选
  127. // 只有搜索框清空时才重置分类
  128. });
  129. this.loadData(true);
  130. } else {
  131. // 搜索框清空时,重置为初始状态
  132. this.setData({
  133. currentSearchKey: '',
  134. // 不清除 currentCategory,让用户保持在当前分类
  135. // 如果需要重置,可以取消下面这行的注释
  136. // currentCategory: '全部'
  137. });
  138. this.loadData(true);
  139. }
  140. }, 500); // 500ms防抖延迟
  141. },
  142. // 手动搜索(点击搜索按钮或按回车)
  143. onSearch() {
  144. const keyword = this.data.searchValue.trim();
  145. if (!keyword) {
  146. // 如果搜索框为空,重置搜索
  147. this.setData({
  148. currentSearchKey: '',
  149. // 保持当前分类,不清除分类筛选
  150. });
  151. } else {
  152. // 保持当前分类,只在当前分类中搜索
  153. this.setData({
  154. currentSearchKey: keyword,
  155. // 保持 currentCategory 不变
  156. });
  157. }
  158. this.loadData(true);
  159. },
  160. // 按分类筛选
  161. onFilterCategory(e) {
  162. const category = e.currentTarget.dataset.category;
  163. this.setData({
  164. activeCategory: category,
  165. currentCategory: category,
  166. // 切换分类时清空搜索关键词
  167. currentSearchKey: '',
  168. searchValue: ''
  169. });
  170. this.loadData(true);
  171. },
  172. // 清空搜索
  173. onClearSearch() {
  174. this.setData({
  175. searchValue: '',
  176. currentSearchKey: '',
  177. // 保持当前分类,不清除分类筛选
  178. });
  179. this.loadData(true);
  180. },
  181. // 上拉加载更多
  182. onScrollToLower() {
  183. if (this.data.hasMore && !this.data.loadingMore) {
  184. this.setData({
  185. pageNum: this.data.pageNum + 1
  186. });
  187. this.loadData(false);
  188. }
  189. },
  190. // 隐藏提示弹框
  191. hideTips() {
  192. this.setData({
  193. showTipsModal: false
  194. });
  195. },
  196. // 阻止事件冒泡
  197. stopPropagation() {
  198. return;
  199. },
  200. // 查看详情
  201. onViewDetail(e) {
  202. const item = e.currentTarget.dataset.value;
  203. wx.showModal({
  204. title: item.title,
  205. content: item.content,
  206. showCancel: false,
  207. confirmText: '知道了'
  208. });
  209. },
  210. // 复制内容到剪贴板
  211. onCopyContent(e) {
  212. const content = e.currentTarget.dataset.content;
  213. wx.setClipboardData({
  214. data: content,
  215. success: () => {
  216. wx.showToast({
  217. title: '复制成功',
  218. icon: 'success'
  219. });
  220. }
  221. });
  222. },
  223. onShareAppMessage() {
  224. return {
  225. title: '动物疾病防治与饲养管理知识库',
  226. path: '/pages/search/index'
  227. };
  228. },
  229. onUnload() {
  230. // 页面卸载时清除定时器
  231. if (this.data.searchTimer) {
  232. clearTimeout(this.data.searchTimer);
  233. }
  234. }
  235. });