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

278 lines
6.5 KiB

  1. import http from '../../../utils/api'
  2. Page({
  3. data: {
  4. // 分类数据
  5. categories: [],
  6. currentCategory: '全部', // 当前选中分类
  7. currentType: '', // 当前选中的分类值
  8. searchKeyword: '', // 搜索关键字
  9. noticeList: [], // 渲染列表数据
  10. pageIndex: 1, // 页码
  11. pageSize: 8, // 每页条数
  12. hasMore: true, // 是否有更多
  13. loading: false, // 加载中
  14. refreshing: false, // 下拉刷新状态
  15. total: 0, // 总条数
  16. searchTimer: null, // 搜索防抖定时器
  17. isSearching: false, // 是否正在搜索
  18. },
  19. onLoad() {
  20. this.getwarningType()
  21. this.fetchNotices(true)
  22. },
  23. onUnload() {
  24. // 页面卸载时清除定时器
  25. if (this.data.searchTimer) {
  26. clearTimeout(this.data.searchTimer)
  27. }
  28. },
  29. // 获取分类数据
  30. getwarningType() {
  31. http.warningType({
  32. data: {
  33. dictType: 'warning_type'
  34. },
  35. success: res => {
  36. console.log('分类数据:', res)
  37. this.setData({
  38. categories: res.rows || []
  39. })
  40. },
  41. fail: err => {
  42. console.error('获取分类失败:', err)
  43. }
  44. })
  45. },
  46. // 获取通知公告列表
  47. fetchNotices(isRefresh = false) {
  48. const { pageIndex, pageSize, searchKeyword, currentType, loading, hasMore } = this.data
  49. // 如果是刷新或者没有更多数据且不是刷新,则返回
  50. if (!isRefresh && !hasMore) return
  51. if (loading) return
  52. this.setData({ loading: true })
  53. // 构建请求参数
  54. const params = {
  55. pageNum: isRefresh ? 1 : pageIndex,
  56. pageSize: pageSize,
  57. }
  58. // 添加搜索关键字(如果有关键字)
  59. if (searchKeyword && searchKeyword.trim() !== '') {
  60. params.keyword = searchKeyword.trim()
  61. }
  62. // 添加分类筛选(如果有分类且不是"全部")
  63. if (currentType) {
  64. params.warningType = currentType
  65. }
  66. console.log('请求参数:', params, '当前分类:', this.data.currentCategory, '当前类型:', currentType)
  67. http.disaster({
  68. data: params,
  69. success: res => {
  70. console.log('公告数据:', res)
  71. const newList = res.rows || []
  72. const total = res.total || 0
  73. this.setData({
  74. noticeList: isRefresh ? newList : [...this.data.noticeList, ...newList],
  75. total: total,
  76. hasMore: newList.length >= pageSize,
  77. pageIndex: isRefresh ? 2 : pageIndex + 1,
  78. loading: false,
  79. refreshing: false,
  80. isSearching: false
  81. })
  82. },
  83. fail: err => {
  84. console.error('获取公告失败:', err)
  85. this.setData({
  86. loading: false,
  87. refreshing: false,
  88. isSearching: false
  89. })
  90. wx.showToast({
  91. title: '加载失败',
  92. icon: 'none'
  93. })
  94. }
  95. })
  96. },
  97. // 切换分类
  98. switchCategory(e) {
  99. const category = e.currentTarget.dataset.category
  100. const type = e.currentTarget.dataset.type || ''
  101. if (category === this.data.currentCategory) return
  102. console.log('切换分类:', category, type)
  103. this.setData({
  104. currentCategory: category,
  105. currentType: type,
  106. pageIndex: 1,
  107. hasMore: true,
  108. noticeList: []
  109. }, () => {
  110. // 切换分类后立即加载数据(保留当前搜索关键词)
  111. this.fetchNotices(true)
  112. })
  113. },
  114. // 搜索输入 - 实时搜索
  115. onSearchInput(e) {
  116. const keyword = e.detail.value
  117. this.setData({
  118. searchKeyword: keyword,
  119. isSearching: true
  120. })
  121. // 清除之前的定时器
  122. if (this.data.searchTimer) {
  123. clearTimeout(this.data.searchTimer)
  124. }
  125. // 设置新的定时器,300ms后执行搜索
  126. const timer = setTimeout(() => {
  127. this.performSearch()
  128. }, 300)
  129. this.setData({
  130. searchTimer: timer
  131. })
  132. },
  133. // 执行搜索
  134. performSearch() {
  135. const { searchKeyword } = this.data
  136. console.log('执行搜索:', searchKeyword)
  137. // 重置列表并加载新数据
  138. this.setData({
  139. pageIndex: 1,
  140. hasMore: true,
  141. noticeList: []
  142. }, () => {
  143. this.fetchNotices(true)
  144. })
  145. },
  146. // 手动搜索(点击确认时)
  147. handleSearch() {
  148. // 清除防抖定时器
  149. if (this.data.searchTimer) {
  150. clearTimeout(this.data.searchTimer)
  151. }
  152. // 立即执行搜索
  153. this.setData({
  154. pageIndex: 1,
  155. hasMore: true,
  156. noticeList: []
  157. }, () => {
  158. this.fetchNotices(true)
  159. })
  160. },
  161. // 清空搜索框
  162. clearSearch() {
  163. // 清除防抖定时器
  164. if (this.data.searchTimer) {
  165. clearTimeout(this.data.searchTimer)
  166. }
  167. this.setData({
  168. searchKeyword: '',
  169. pageIndex: 1,
  170. hasMore: true,
  171. noticeList: []
  172. }, () => {
  173. // 清空搜索后重新加载数据(保持当前分类)
  174. this.fetchNotices(true)
  175. })
  176. },
  177. // 上拉加载更多
  178. loadMore() {
  179. const { hasMore, loading, refreshing, isSearching } = this.data
  180. if (!hasMore || loading || refreshing || isSearching) return
  181. this.fetchNotices(false)
  182. },
  183. // 下拉刷新
  184. onRefresh() {
  185. // 清除防抖定时器
  186. if (this.data.searchTimer) {
  187. clearTimeout(this.data.searchTimer)
  188. }
  189. this.setData({
  190. refreshing: true,
  191. pageIndex: 1,
  192. hasMore: true
  193. }, () => {
  194. this.fetchNotices(true)
  195. })
  196. },
  197. // 查看详情
  198. viewDetail(e) {
  199. const id = e.currentTarget.dataset.id
  200. //跳转详情页
  201. wx.navigateTo({
  202. url: `/pagesA/pages/noticeListDetails/noticeListDetails?id=${id}`,
  203. })
  204. },
  205. // 根据重要性获取颜色
  206. getImportanceColor(level) {
  207. const colorMap = {
  208. '紧急': '#f43f5e',
  209. '重要': '#f97316',
  210. '一般': '#3b82f6'
  211. }
  212. return colorMap[level] || '#3b82f6'
  213. },
  214. // 获取标签背景色
  215. getTagBg(level) {
  216. const bgMap = {
  217. '紧急': '#fee2e2',
  218. '重要': '#fff3e0',
  219. '一般': '#eef2ff'
  220. }
  221. return bgMap[level] || '#eef2ff'
  222. },
  223. // 获取级别文字颜色
  224. getLevelColor(level) {
  225. const colorMap = {
  226. '紧急': '#f43f5e',
  227. '重要': '#f97316',
  228. '一般': '#3b82f6'
  229. }
  230. return colorMap[level] || '#3b82f6'
  231. },
  232. // 获取级别背景色
  233. getLevelBg(level) {
  234. const bgMap = {
  235. '紧急': 'rgba(244, 63, 94, 0.1)',
  236. '重要': 'rgba(249, 115, 22, 0.1)',
  237. '一般': 'rgba(59, 130, 246, 0.1)'
  238. }
  239. return bgMap[level] || 'rgba(59, 130, 246, 0.1)'
  240. }
  241. })