activity.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <div class="activity-container">
  3. <!-- 返回按钮 -->
  4. <div class="back-button" @click="goBackToIndex">
  5. <img src="@/assets/indexPage/icon_back.png" alt="" />
  6. </div>
  7. <!-- 标题 -->
  8. <div class="section-title">社教活动</div>
  9. <!-- 活动列表 -->
  10. <div v-if="loading && activeList.length === 0" class="loading-container">
  11. <div class="loading-text">加载中...</div>
  12. </div>
  13. <div v-else class="content-section">
  14. <div class="collection-list">
  15. <div
  16. class="collection-item"
  17. v-for="(item, index) in activeList"
  18. :key="item.id || item.activityId || index"
  19. >
  20. <div class="item-image-container" @click="viewActivity(item)">
  21. <img
  22. :src="getFieldValue(item, 'infoImg')"
  23. :alt="getFieldValue(item, 'title') || '活动图片'"
  24. class="item-image"
  25. />
  26. <div class="view-button">
  27. <span>查看</span
  28. ><el-icon>
  29. <Right />
  30. </el-icon>
  31. </div>
  32. <div class="item-info">
  33. <div class="item-title">{{ getFieldValue(item, 'title') || '暂无标题' }}</div>
  34. <div class="item-description">{{ getFieldValue(item, 'remark') || '暂无描述' }}</div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. <!-- 空状态 -->
  40. <div v-if="activeList.length === 0" class="empty-state">
  41. <div class="empty-text">暂无活动数据</div>
  42. </div>
  43. <!-- 加载更多指示器 -->
  44. <div v-if="activeList.length > 0 && isLoadingMore" class="loading-more">
  45. <div class="loading-more-text">加载中...</div>
  46. </div>
  47. <!-- 没有更多数据提示 -->
  48. <div v-if="activeList.length > 0 && !activeHasMore && !isLoadingMore" class="no-more">
  49. <div class="no-more-text">没有更多数据了</div>
  50. </div>
  51. </div>
  52. </div>
  53. </template>
  54. <script>
  55. export default {
  56. name: 'ActivityPage',
  57. }
  58. </script>
  59. <script setup>
  60. import { onMounted, computed, nextTick, onUnmounted } from 'vue'
  61. import { useRouter, useRoute } from 'vue-router'
  62. import { useIndexPageApi } from '@/setup/useIndexPageApi'
  63. const router = useRouter()
  64. const route = useRoute()
  65. // 检查是否为预览模式
  66. const isPreviewMode = computed(() => {
  67. return route.query.preview === '1'
  68. })
  69. // 获取字段值的通用方法,优先获取带B后缀的字段
  70. const getFieldValue = (item, fieldName) => {
  71. if (isPreviewMode.value) {
  72. return item[fieldName + 'B'] || item[fieldName]
  73. }
  74. return item[fieldName]
  75. }
  76. // 使用组合式函数获取API方法和响应式数据
  77. const { activeList, getActiveList, loading, activeHasMore, loadMoreActivities } = useIndexPageApi()
  78. // 滚动加载相关
  79. const isLoadingMore = computed(() => loading.value && activeList.value.length > 0)
  80. // 滚动事件处理
  81. const handleScroll = async () => {
  82. const container = document.querySelector('.activity-container')
  83. if (!container) return
  84. const { scrollTop, scrollHeight, clientHeight } = container
  85. const threshold = 100 // 距离底部100px时触发加载
  86. if (
  87. scrollTop + clientHeight >= scrollHeight - threshold &&
  88. !isLoadingMore.value &&
  89. activeHasMore.value
  90. ) {
  91. await loadMore()
  92. }
  93. }
  94. // 加载更多数据
  95. const loadMore = async () => {
  96. try {
  97. const params = {}
  98. // 如果是预览模式,设置status为-1
  99. if (isPreviewMode.value) {
  100. params.status = -1
  101. }
  102. await loadMoreActivities(params)
  103. } catch (error) {
  104. console.error('加载更多活动数据失败:', error)
  105. }
  106. }
  107. // 页面初始化时获取活动数据
  108. onMounted(async () => {
  109. try {
  110. const params = { pageNum: 1, pageSize: 10 }
  111. // 如果是预览模式,设置status为-1
  112. if (isPreviewMode.value) {
  113. params.status = -1
  114. }
  115. await getActiveList(params)
  116. // 添加滚动事件监听
  117. await nextTick()
  118. const container = document.querySelector('.activity-container')
  119. if (container) {
  120. container.addEventListener('scroll', handleScroll)
  121. }
  122. } catch (error) {
  123. console.error('获取活动数据失败:', error)
  124. }
  125. })
  126. // 组件卸载时移除滚动事件监听
  127. onUnmounted(() => {
  128. const container = document.querySelector('.activity-container')
  129. if (container) {
  130. container.removeEventListener('scroll', handleScroll)
  131. }
  132. })
  133. // 返回首页函数
  134. const goBackToIndex = () => {
  135. if (isPreviewMode.value) {
  136. router.replace({
  137. path: '/indexPage',
  138. query: { preview: '1' },
  139. })
  140. } else {
  141. router.replace('/indexPage')
  142. }
  143. }
  144. // 查看活动详情
  145. const viewActivity = (item) => {
  146. console.log('查看活动详情:', item)
  147. // 跳转到详情页,传递活动ID和类型参数
  148. router.push({
  149. path: '/allDetailsShow',
  150. query: {
  151. id: item.id || item.activityId,
  152. type: 'activity', // 标识这是活动详情
  153. isFromPage: 'indexPage/activity',
  154. },
  155. })
  156. }
  157. </script>
  158. <style lang="scss" scoped>
  159. .activity-container {
  160. position: relative;
  161. height: 100vh;
  162. min-width: 375px;
  163. padding: 20px;
  164. background: url('@/assets/indexPage/bg.png') no-repeat;
  165. background-size: cover;
  166. overflow-y: auto;
  167. }
  168. .back-button {
  169. position: absolute;
  170. top: 20px;
  171. left: 20px;
  172. width: 40px;
  173. height: 40px;
  174. border-radius: 50%;
  175. img {
  176. width: 40px;
  177. height: 40px;
  178. }
  179. }
  180. .section-title {
  181. font-size: 20px;
  182. font-weight: bold;
  183. color: #584735;
  184. margin: 54px 0 20px 0;
  185. position: relative;
  186. padding-bottom: 10px;
  187. &::after {
  188. content: '';
  189. position: absolute;
  190. bottom: 0;
  191. left: 0;
  192. width: 100%;
  193. height: 2px;
  194. background: linear-gradient(90deg, rgba(91, 71, 46, 0.5) 0%, rgba(91, 71, 46, 0) 100%);
  195. }
  196. }
  197. .collection-list {
  198. display: flex;
  199. flex-direction: column;
  200. gap: 15px;
  201. }
  202. .collection-item {
  203. background-color: #fff;
  204. border-radius: 8px;
  205. overflow: hidden;
  206. box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  207. }
  208. .item-image-container {
  209. position: relative;
  210. height: 218px;
  211. overflow: hidden;
  212. cursor: pointer;
  213. .item-image {
  214. width: 100%;
  215. height: 100%;
  216. object-fit: cover;
  217. }
  218. .view-button {
  219. position: absolute;
  220. display: flex;
  221. align-items: center;
  222. justify-content: center;
  223. width: 80px;
  224. height: 36px;
  225. top: 12px;
  226. right: 12px;
  227. background-color: rgba(0, 0, 0, 0.2);
  228. color: #fff;
  229. padding: 4px 8px;
  230. border-radius: 50px;
  231. font-size: 12px;
  232. border: 1px solid #fff;
  233. span {
  234. font-size: 14px;
  235. margin-right: 6px;
  236. }
  237. }
  238. }
  239. .item-info {
  240. position: absolute;
  241. width: 90%;
  242. bottom: 16px;
  243. left: 24px;
  244. .item-category {
  245. font-size: 14px;
  246. color: #fff;
  247. }
  248. .item-title {
  249. font-size: 20px;
  250. font-weight: bold;
  251. color: #fff;
  252. font-family: 'heavy';
  253. }
  254. .item-description {
  255. font-size: 12px;
  256. color: #fff;
  257. overflow: hidden;
  258. text-overflow: ellipsis;
  259. }
  260. }
  261. // 加载状态样式
  262. .loading-container {
  263. display: flex;
  264. justify-content: center;
  265. align-items: center;
  266. height: 200px;
  267. .loading-text {
  268. font-size: 18px;
  269. color: #584735;
  270. opacity: 0.8;
  271. }
  272. }
  273. // 空状态样式
  274. .empty-state {
  275. display: flex;
  276. justify-content: center;
  277. align-items: center;
  278. height: 200px;
  279. margin-top: 50px;
  280. .empty-text {
  281. font-size: 18px;
  282. color: #584735;
  283. opacity: 0.6;
  284. }
  285. }
  286. // 加载更多样式
  287. .loading-more {
  288. display: flex;
  289. justify-content: center;
  290. align-items: center;
  291. height: 60px;
  292. margin-top: 20px;
  293. .loading-more-text {
  294. font-size: 16px;
  295. color: #584735;
  296. opacity: 0.8;
  297. }
  298. }
  299. // 没有更多数据样式
  300. .no-more {
  301. display: flex;
  302. justify-content: center;
  303. align-items: center;
  304. height: 60px;
  305. margin-top: 20px;
  306. .no-more-text {
  307. font-size: 16px;
  308. color: #584735;
  309. opacity: 0.6;
  310. }
  311. }
  312. </style>