coupon-list.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <!-- TODO -->
  3. <view class="coupon-find-list">
  4. <tui-skeleton v-if="isRequest" loadingType="2"></tui-skeleton>
  5. <!-- tabs -->
  6. <template v-else>
  7. <tui-tabs
  8. :tabs="tabs"
  9. :currentTab="currentTab"
  10. @change="tabChange"
  11. :sliderWidth="118"
  12. color="#333333"
  13. selectedColor="#FF457B"
  14. sliderBgColor="#FF457B"
  15. ></tui-tabs>
  16. <!-- 优惠券列表 -->
  17. <swiper
  18. :indicator-dots="false"
  19. :autoplay="false"
  20. class="swiper"
  21. :style="{ height: swiperHeight + 'px' }"
  22. :current="currentTab"
  23. @change="swiperChange"
  24. >
  25. <swiper-item class="coupon-list" v-for="(item, type) of couponData" :key="type">
  26. <cm-empty
  27. v-if="item.couponList.length <= 0"
  28. message="暂无任何优惠券~"
  29. :image="baseUrl + 'icon-coupon-empty.png'"
  30. :offset="-12"
  31. ></cm-empty>
  32. <scroll-view :scroll-y="item.couponList.length >= 5" class="coupon-scorll" @scrolltolower="scorllBottom" v-else>
  33. <template v-for="(coupon, index) in item.couponList">
  34. <cm-coupon
  35. :key="index"
  36. :couponData="coupon"
  37. @btnClick="couponClick"
  38. :showStatus="item.showStatus"
  39. ></cm-coupon>
  40. </template>
  41. <template v-if="item.couponList.length >= 6">
  42. <tui-loadmore :index="3" :visible="loadmore"></tui-loadmore>
  43. <tui-nomore :text="loadingText" :visible="!loadmore" backgroundColor="#f7f7f7"></tui-nomore>
  44. </template>
  45. </scroll-view>
  46. </swiper-item>
  47. </swiper>
  48. <!-- 跳转领券中心 -->
  49. <view class="footer">
  50. <view class="btn" @click="$api.navigateTo('/pages/user/activity/coupon-find-list')">领券中心</view>
  51. </view>
  52. </template>
  53. </view>
  54. </template>
  55. <script>
  56. import CmCoupon from '@/components/cm-module/cm-coupon/cm-coupon.vue'
  57. import CmEmpty from '@/components/cm-module/cm-empty/cm-empty.vue'
  58. import { mapGetters, mapActions } from 'vuex'
  59. export default {
  60. components: {
  61. CmCoupon,
  62. CmEmpty
  63. },
  64. data() {
  65. return {
  66. windowHeight: 0,
  67. baseUrl: this.$Static,
  68. isRequest: true,
  69. loadmore: false, // 正在加载更多
  70. currentTab: 0,
  71. pageSize: 10,
  72. timer: null,
  73. couponData: null
  74. }
  75. },
  76. computed: {
  77. ...mapGetters(['isIphoneX', 'userId', 'unusedNum', 'expiredNum', 'usedNum']),
  78. swiperHeight() {
  79. if (this.isIphoneX) {
  80. return this.windowHeight - 144
  81. }
  82. return this.windowHeight - 120
  83. // return this.swiperHeight
  84. },
  85. // 当前选中tab对应的优惠券列表信息
  86. queryInfo() {
  87. if (!this.couponData) return
  88. return this.couponData[this.currentTab]
  89. },
  90. loadingText() {
  91. if (!this.couponData) return '没有更多了'
  92. return this.queryInfo.hasNextPage ? '上拉加载更多' : '没有更多了'
  93. },
  94. // tabs列表
  95. tabs() {
  96. return [
  97. {
  98. name: this.makeTabText(this.unusedNum, '未使用')
  99. },
  100. {
  101. name: this.makeTabText(this.usedNum, '已使用')
  102. },
  103. {
  104. name: this.makeTabText(this.expiredNum, '已失效')
  105. }
  106. ]
  107. }
  108. },
  109. onLoad() {
  110. this.getWindowHeight()
  111. },
  112. onShow() {
  113. this.couponData = this.resetCouponData()
  114. this.initCouponList()
  115. },
  116. methods: {
  117. // tab文字提示
  118. makeTabText(number, text) {
  119. if (number <= 0) return text
  120. return number > 99 ? `${text} (99+)` : `${text} (${number})`
  121. },
  122. // 初始化列表
  123. async initCouponList() {
  124. try {
  125. await this.fetchCouponList(1)
  126. await this.fetchCouponList(2)
  127. await this.fetchCouponList(3)
  128. this.isRequest = false
  129. } catch (e) {
  130. //TODO handle the exception
  131. console.log(e)
  132. }
  133. },
  134. // 获取优惠券
  135. fetchCouponList(status) {
  136. let query = null
  137. if (status) {
  138. query = this.couponData[status - 1]
  139. } else {
  140. query = this.couponData[this.currentTab]
  141. }
  142. // 查询参数
  143. const listQuery = {
  144. userId: this.userId,
  145. pageSize: this.pageSize,
  146. pageNum: query.pageNum,
  147. status: status || query.status
  148. }
  149. this.loadmore = true
  150. return this.CouponService.CouponReceiveList(listQuery).then(res => {
  151. query.hasNextPage = res.data.hasNextPage
  152. query.total = res.data.total
  153. query.couponList = [...query.couponList, ...res.data.list]
  154. query.pageNum++
  155. this.loadmore = false
  156. })
  157. },
  158. // tab切换
  159. tabChange(e) {
  160. this.currentTab = e.index
  161. if (this.queryInfo.pageNum === 1) this.fetchCouponList()
  162. },
  163. // 轮播图切换
  164. swiperChange(e) {
  165. this.currentTab = e.detail.current
  166. if (this.queryInfo.pageNum === 1) this.fetchCouponList()
  167. },
  168. // 获取可用屏幕高度
  169. getWindowHeight() {
  170. this.windowHeight = uni.getSystemInfoSync().windowHeight
  171. console.log(this.windowHeight)
  172. },
  173. // 加载更多
  174. scorllBottom() {
  175. if (!this.queryInfo.hasNextPage) return
  176. clearTimeout(this.timer)
  177. this.timer = setTimeout(() => {
  178. console.log('触底了')
  179. this.fetchCouponList()
  180. }, 200)
  181. },
  182. couponClick() {},
  183. // 初始化优惠券分类查询信息
  184. resetCouponData() {
  185. return {
  186. // 已领取/未使用
  187. 0: {
  188. type: 'received',
  189. pageNum: 1,
  190. status: 1,
  191. hasNextPage: false,
  192. couponList: [],
  193. showStatus: false,
  194. total: 0
  195. },
  196. // 已使用
  197. 1: {
  198. type: 'used',
  199. pageNum: 1,
  200. status: 2,
  201. hasNextPage: false,
  202. couponList: [],
  203. showStatus: true,
  204. total: 0
  205. },
  206. // 已过期
  207. 2: {
  208. type: 'expired',
  209. pageNum: 1,
  210. status: 3,
  211. hasNextPage: false,
  212. couponList: [],
  213. showStatus: true,
  214. total: 0
  215. }
  216. }
  217. }
  218. }
  219. }
  220. </script>
  221. <style lang="scss" scoped>
  222. .coupon-find-list {
  223. width: 100%;
  224. min-height: 100vh;
  225. background: #f7f7f7;
  226. }
  227. .swiper {
  228. background: #f7f7f7;
  229. .coupon-list {
  230. height: 100%;
  231. .coupon-scorll {
  232. height: 100%;
  233. }
  234. }
  235. }
  236. .footer {
  237. display: flex;
  238. justify-content: center;
  239. align-items: center;
  240. background: #f7f7f7;
  241. padding-top: 20rpx;
  242. .btn {
  243. display: flex;
  244. justify-content: center;
  245. align-items: center;
  246. width: 600rpx;
  247. height: 90rpx;
  248. background: linear-gradient(90deg, #fc32b4 0%, #f83c6c 100%);
  249. border-radius: 45rpx;
  250. font-size: 30rpx;
  251. color: #ffffff;
  252. }
  253. }
  254. </style>