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

247 lines
5.7 KiB

  1. import http from '../../../utils/api'
  2. Page({
  3. data: {
  4. currentDate: '2023年11月15日',
  5. searchKeyword: '',
  6. activeFilter: '全部',
  7. activeFilterValue: '',
  8. showPolicyDetail: false,
  9. currentPolicy: {},
  10. filteredPolicies: [],
  11. dictType: [],
  12. // 分页相关
  13. pageNum: 1,
  14. pageSize: 10,
  15. total: 0,
  16. hasMore: true,
  17. isLoading: false,
  18. isLoadingMore: false,
  19. // 滚动区域高度
  20. scrollHeight: 0,
  21. // 搜索定时器
  22. searchTimer: null
  23. },
  24. onLoad: function(options) {
  25. this.getpolicyeZd()
  26. this.getpolicyelucidation()
  27. // 计算滚动区域高度
  28. this.calculateScrollHeight()
  29. },
  30. onReady: function() {
  31. // 确保获取正确的高度
  32. this.calculateScrollHeight()
  33. },
  34. // 计算滚动区域高度
  35. calculateScrollHeight: function() {
  36. const systemInfo = wx.getSystemInfoSync()
  37. const query = wx.createSelectorQuery()
  38. query.select('.search-section').boundingClientRect()
  39. query.select('.bottom-tip').boundingClientRect()
  40. query.exec((res) => {
  41. if (res[0] && res[1]) {
  42. const searchHeight = res[0].height
  43. const bottomHeight = res[1].height
  44. const windowHeight = systemInfo.windowHeight
  45. const scrollHeight = windowHeight - searchHeight - bottomHeight - 40
  46. this.setData({
  47. scrollHeight: Math.max(scrollHeight, 400) // 设置最小高度
  48. })
  49. }
  50. })
  51. },
  52. // 政策解读列表(支持分页和筛选)
  53. getpolicyelucidation: function(isLoadMore = false) {
  54. if (isLoadMore && (!this.data.hasMore || this.data.isLoadingMore)) {
  55. return
  56. }
  57. if (!isLoadMore) {
  58. this.setData({ isLoading: true })
  59. } else {
  60. this.setData({ isLoadingMore: true })
  61. }
  62. const params = {
  63. pageNum: isLoadMore ? this.data.pageNum : 1,
  64. pageSize: this.data.pageSize,
  65. searchKey: this.data.searchKeyword || undefined
  66. }
  67. // 如果选择了分类(不是"全部"),则添加分类参数
  68. if (this.data.activeFilterValue && this.data.activeFilter !== '全部') {
  69. params.policyCategory = this.data.activeFilterValue
  70. }
  71. http.policyelucidation({
  72. data: params,
  73. success: res => {
  74. console.log('政策列表:', res)
  75. const newList = isLoadMore
  76. ? [...this.data.filteredPolicies, ...res.rows]
  77. : res.rows
  78. const total = res.total || 0
  79. const currentTotal = newList.length
  80. const hasMore = currentTotal < total
  81. this.setData({
  82. filteredPolicies: newList,
  83. total: total,
  84. hasMore: hasMore,
  85. pageNum: isLoadMore ? this.data.pageNum + 1 : 2,
  86. isLoading: false,
  87. isLoadingMore: false
  88. })
  89. },
  90. fail: (err) => {
  91. console.error('获取政策列表失败:', err)
  92. this.setData({
  93. isLoading: false,
  94. isLoadingMore: false
  95. })
  96. if (!isLoadMore) {
  97. wx.showToast({
  98. title: '加载失败',
  99. icon: 'none'
  100. })
  101. }
  102. }
  103. })
  104. },
  105. // 类型
  106. getpolicyeZd: function() {
  107. http.policyeZd({
  108. data: {
  109. dictType: 'policy_category'
  110. },
  111. success: res => {
  112. console.log('分类数据:', res)
  113. this.setData({
  114. dictType: res.rows
  115. })
  116. },
  117. fail: (err) => {
  118. console.error('获取分类失败:', err)
  119. wx.showToast({
  120. title: '加载分类失败',
  121. icon: 'none'
  122. })
  123. }
  124. })
  125. },
  126. // 搜索输入处理(防抖)
  127. onSearchInput: function(e) {
  128. const searchKeyword = e.detail.value.trim()
  129. this.setData({
  130. searchKeyword: searchKeyword
  131. })
  132. // 清除之前的定时器
  133. if (this.data.searchTimer) {
  134. clearTimeout(this.data.searchTimer)
  135. }
  136. // 设置新的定时器(500ms防抖)
  137. this.data.searchTimer = setTimeout(() => {
  138. // 重置分页
  139. this.setData({
  140. pageNum: 1,
  141. hasMore: true
  142. }, () => {
  143. this.getpolicyelucidation()
  144. })
  145. }, 500)
  146. },
  147. // 清空搜索
  148. onClearSearch: function() {
  149. this.setData({
  150. searchKeyword: '',
  151. pageNum: 1,
  152. hasMore: true
  153. }, () => {
  154. this.getpolicyelucidation()
  155. })
  156. },
  157. // 搜索按钮点击(键盘搜索键)
  158. onSearch: function() {
  159. this.setData({
  160. pageNum: 1,
  161. hasMore: true
  162. }, () => {
  163. this.getpolicyelucidation()
  164. })
  165. },
  166. // 筛选标签点击
  167. onFilterTap: function(e) {
  168. const filter = e.currentTarget.dataset.filter
  169. const value = e.currentTarget.dataset.value
  170. this.setData({
  171. activeFilter: filter,
  172. activeFilterValue: value,
  173. pageNum: 1,
  174. hasMore: true
  175. }, () => {
  176. this.getpolicyelucidation()
  177. })
  178. },
  179. // 政策卡片点击
  180. onPolicyTap: function(e) {
  181. const policyId = e.currentTarget.dataset.id
  182. http.policyeDetails({
  183. data: {
  184. id: policyId
  185. },
  186. success: res => {
  187. console.log('政策详情:', res)
  188. this.setData({
  189. showPolicyDetail: true,
  190. currentPolicy: res.data
  191. })
  192. },
  193. fail: (err) => {
  194. console.error('获取政策详情失败:', err)
  195. wx.showToast({
  196. title: '加载详情失败',
  197. icon: 'none'
  198. })
  199. }
  200. })
  201. },
  202. // 上拉加载更多
  203. onReachBottom: function() {
  204. if (this.data.hasMore && !this.data.isLoadingMore) {
  205. this.getpolicyelucidation(true)
  206. }
  207. },
  208. // 隐藏政策详情
  209. hidePolicyDetail: function() {
  210. this.setData({
  211. showPolicyDetail: false
  212. })
  213. },
  214. // 阻止事件冒泡
  215. stopPropagation: function() {
  216. // 空函数,仅用于阻止事件冒泡
  217. }
  218. })