index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <script lang="ts" setup>
  2. import { computed, ref } from 'vue'
  3. import { useScroll } from '@/hooks/useScroll'
  4. import { safeAreaInsets } from '@/utils'
  5. import { getCouponByType } from '../../api/home'
  6. import DiscountCoupon from '../../components/discountCoupon.vue'
  7. definePage({
  8. style: {
  9. navigationBarTitleText: '',
  10. navigationStyle: 'custom',
  11. // 禁用默认下拉刷新
  12. enablePullDownRefresh: false,
  13. },
  14. })
  15. const params = ref()
  16. // 使用内置的useScroll hooks
  17. const {
  18. list: data, // 响应式的数据列表
  19. loading, // 是否加载中
  20. finished, // 是否已全部加载
  21. error, // 是否加载失败
  22. refresh: onRefresh, // 下拉刷新方法
  23. loadMore: onLoadMore, // 加载更多方法
  24. } = useScroll({
  25. fetchData: async (page, pageSize) => {
  26. const response = await getCouponByType({
  27. pageNo: page,
  28. pageSize,
  29. ...params.value,
  30. })
  31. return response.records || []
  32. },
  33. pageSize: 7,
  34. })
  35. // 计算底部安全区高度
  36. const safeBottomHeight = computed(() => {
  37. return safeAreaInsets?.bottom || 0
  38. })
  39. const topSafeAreaHeight = safeAreaInsets?.top || 0
  40. // 页面加载时获取数据
  41. onLoad((options) => {
  42. if (options.type) {
  43. params.value = options
  44. onRefresh()
  45. }
  46. })
  47. </script>
  48. <template>
  49. <view class="page-container">
  50. <up-navbar title="折扣券" :auto-back="true" />
  51. <up-status-bar />
  52. <scroll-view :style="{ paddingTop: `${topSafeAreaHeight}px` }" class="discount-coupon-list" scroll-y
  53. refresher-enabled :refresher-triggered="loading" @refresherrefresh="onRefresh" @scrolltolower="onLoadMore">
  54. <view class="list-content">
  55. <view v-for="item in data" :key="item.templateId" class="discount-coupon-item">
  56. <DiscountCoupon :coupon="item" />
  57. </view>
  58. <!-- 加载状态提示 -->
  59. <view v-if="loading && data.length > 0" class="loadmore-container">
  60. <up-loadmore status="loading" loading-text="努力加载中..." icon-size="18" />
  61. </view>
  62. <view v-else-if="finished && data.length > 0" class="loadmore-container">
  63. <up-loadmore status="nomore" nomore-text="我们是有底线的" icon-size="18" />
  64. </view>
  65. </view>
  66. </scroll-view>
  67. <!-- 安全区底部占位 -->
  68. <view class="safe-bottom" :style="{ height: `${safeBottomHeight}px` }" />
  69. </view>
  70. </template>
  71. <style lang="scss" scoped>
  72. .page-container {
  73. height: 100vh;
  74. display: flex;
  75. flex-direction: column;
  76. }
  77. .discount-coupon-list {
  78. flex: 1;
  79. overflow-y: auto;
  80. .list-content {
  81. padding-top: 20rpx;
  82. padding-left: 24rpx;
  83. padding-right: 24rpx;
  84. display: flex;
  85. flex-direction: column;
  86. gap: 20rpx;
  87. }
  88. .loadmore-container {
  89. padding: 30rpx 0;
  90. text-align: center;
  91. }
  92. }
  93. .safe-bottom {
  94. width: 100%;
  95. background-color: #fff;
  96. }
  97. </style>