list.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <!-- 优惠券中心 -->
  2. <template>
  3. <s-layout title="优惠券" :bgStyle="{ color: '#f2f2f2' }">
  4. <su-sticky bgColor="#fff">
  5. <su-tabs
  6. :list="tabMaps"
  7. :scrollable="false"
  8. @change="onTabsChange"
  9. :current="state.currentTab"
  10. />
  11. </su-sticky>
  12. <s-empty
  13. v-if="state.pagination.total === 0"
  14. icon="/static/coupon-empty.png"
  15. text="暂无优惠券"
  16. />
  17. <!-- 情况一:领劵中心 -->
  18. <template v-if="state.currentTab === 0">
  19. <view v-for="item in state.pagination.list" :key="item.id">
  20. <s-coupon-list
  21. :data="item"
  22. @tap="sheep.$router.go('/pages/coupon/detail', { id: item.id })"
  23. >
  24. <template #default>
  25. <button
  26. class="ss-reset-button card-btn ss-flex ss-row-center ss-col-center"
  27. :class="!item.canTake ? 'border-btn' : ''"
  28. @click.stop="getBuy(item.id)"
  29. :disabled="!item.canTake"
  30. >
  31. {{ item.canTake ? '立即领取' : '已领取' }}
  32. </button>
  33. </template>
  34. </s-coupon-list>
  35. </view>
  36. </template>
  37. <!-- 情况二:我的优惠劵 -->
  38. <template v-else>
  39. <view v-for="item in state.pagination.list" :key="item.id">
  40. <s-coupon-list
  41. :data="item"
  42. type="user"
  43. @tap="sheep.$router.go('/pages/coupon/detail', { couponId: item.id })"
  44. >
  45. <template #default>
  46. <button
  47. class="ss-reset-button card-btn ss-flex ss-row-center ss-col-center"
  48. :class="item.status !== 1 ? 'disabled-btn' : ''"
  49. :disabled="item.status !== 1"
  50. @click.stop="sheep.$router.go('/pages/coupon/detail', { couponId: item.id })"
  51. >
  52. {{ item.status === 1 ? '立即使用' : item.status === 2 ? '已使用' : '已过期' }}
  53. </button>
  54. </template>
  55. </s-coupon-list>
  56. </view>
  57. </template>
  58. <uni-load-more
  59. v-if="state.pagination.total > 0"
  60. :status="state.loadStatus"
  61. :content-text="{
  62. contentdown: '上拉加载更多',
  63. }"
  64. @tap="loadMore"
  65. />
  66. </s-layout>
  67. </template>
  68. <script setup>
  69. import sheep from '@/sheep';
  70. import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  71. import { reactive } from 'vue';
  72. import _ from 'lodash-es';
  73. import { resetPagination } from '@/sheep/util';
  74. import CouponApi from '@/sheep/api/promotion/coupon';
  75. // 数据
  76. const state = reactive({
  77. currentTab: 0, // 当前 tab
  78. type: '1',
  79. pagination: {
  80. list: [],
  81. total: 0,
  82. pageNo: 1,
  83. pageSize: 5,
  84. },
  85. loadStatus: '',
  86. });
  87. const tabMaps = [
  88. {
  89. name: '领券中心',
  90. value: 'all',
  91. },
  92. {
  93. name: '已领取',
  94. value: '1',
  95. },
  96. {
  97. name: '已使用',
  98. value: '2',
  99. },
  100. {
  101. name: '已失效',
  102. value: '3',
  103. },
  104. ];
  105. function onTabsChange(e) {
  106. state.currentTab = e.index;
  107. state.type = e.value;
  108. resetPagination(state.pagination);
  109. if (state.currentTab === 0) {
  110. getData();
  111. } else {
  112. getCoupon();
  113. }
  114. }
  115. // 获得优惠劵模版列表
  116. async function getData() {
  117. state.loadStatus = 'loading';
  118. const { data, code } = await CouponApi.getCouponTemplatePage({
  119. pageNo: state.pagination.pageNo,
  120. pageSize: state.pagination.pageSize,
  121. });
  122. if (code !== 0) {
  123. return;
  124. }
  125. state.pagination.list = _.concat(state.pagination.list, data.list);
  126. state.pagination.total = data.total;
  127. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  128. }
  129. // 获得我的优惠劵
  130. async function getCoupon() {
  131. state.loadStatus = 'loading';
  132. const { data, code } = await CouponApi.getCouponPage({
  133. pageNo: state.pagination.pageNo,
  134. pageSize: state.pagination.pageSize,
  135. status: state.type,
  136. });
  137. if (code !== 0) {
  138. return;
  139. }
  140. state.pagination.list = _.concat(state.pagination.list, data.list);
  141. state.pagination.total = data.total;
  142. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  143. }
  144. // 领取优惠劵
  145. async function getBuy(id) {
  146. const { code } = await CouponApi.takeCoupon(id);
  147. if (code !== 0) {
  148. return;
  149. }
  150. uni.showToast({
  151. title: '领取成功',
  152. });
  153. setTimeout(() => {
  154. resetPagination(state.pagination);
  155. getData();
  156. }, 1000);
  157. }
  158. // 加载更多
  159. function loadMore() {
  160. if (state.loadStatus === 'noMore') {
  161. return;
  162. }
  163. state.pagination.pageNo++;
  164. if (state.currentTab === 0) {
  165. getData();
  166. } else {
  167. getCoupon();
  168. }
  169. }
  170. onLoad((Option) => {
  171. // 领劵中心
  172. if (Option.type === 'all' || !Option.type) {
  173. getData();
  174. // 我的优惠劵
  175. } else {
  176. Option.type === 'geted'
  177. ? (state.currentTab = 1)
  178. : Option.type === 'used'
  179. ? (state.currentTab = 2)
  180. : (state.currentTab = 3);
  181. state.type = state.currentTab;
  182. getCoupon();
  183. }
  184. });
  185. onReachBottom(() => {
  186. loadMore();
  187. });
  188. </script>
  189. <style lang="scss" scoped>
  190. .card-btn {
  191. // width: 144rpx;
  192. padding: 0 16rpx;
  193. height: 50rpx;
  194. border-radius: 40rpx;
  195. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  196. color: #ffffff;
  197. font-size: 24rpx;
  198. font-weight: 400;
  199. }
  200. .border-btn {
  201. background: linear-gradient(90deg, var(--ui-BG-Main-opacity-4), var(--ui-BG-Main-light));
  202. color: #fff !important;
  203. }
  204. .disabled-btn {
  205. background: #cccccc;
  206. background-color: #cccccc !important;
  207. color: #fff !important;
  208. }
  209. </style>