list.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 v-if="state.pagination.total > 0" :status="state.loadStatus" :content-text="{
  59. contentdown: '上拉加载更多',
  60. }" @tap="loadMore" />
  61. </s-layout>
  62. </template>
  63. <script setup>
  64. import sheep from '@/sheep';
  65. import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  66. import { reactive } from 'vue';
  67. import _ from 'lodash';
  68. import { resetPagination } from '@/sheep/util';
  69. import CouponApi from '@/sheep/api/promotion/coupon';
  70. // 数据
  71. const state = reactive({
  72. currentTab: 0, // 当前 tab
  73. type: '1',
  74. pagination: {
  75. list: [],
  76. total: 0,
  77. pageNo: 1,
  78. pageSize: 5
  79. },
  80. loadStatus: '',
  81. });
  82. const tabMaps = [
  83. {
  84. name: '领券中心',
  85. value: 'all',
  86. },
  87. {
  88. name: '已领取',
  89. value: '1',
  90. },
  91. {
  92. name: '已使用',
  93. value: '2',
  94. },
  95. {
  96. name: '已失效',
  97. value: '3',
  98. },
  99. ];
  100. // TODO yunai:
  101. function onTabsChange(e) {
  102. state.currentTab = e.index;
  103. state.type = e.value;
  104. resetPagination(state.pagination)
  105. if (state.currentTab === 0) {
  106. getData();
  107. } else {
  108. getCoupon();
  109. }
  110. }
  111. // 获得优惠劵模版列表
  112. async function getData() {
  113. state.loadStatus = 'loading';
  114. const { data, code } = await CouponApi.getCouponTemplatePage({
  115. pageNo: state.pagination.pageNo,
  116. pageSize: state.pagination.pageSize,
  117. });
  118. if (code !== 0) {
  119. return;
  120. }
  121. state.pagination.list = _.concat(state.pagination.list, data.list);
  122. state.pagination.total = data.total;
  123. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  124. }
  125. // 获得我的优惠劵
  126. async function getCoupon() {
  127. state.loadStatus = 'loading';
  128. const { data, code } = await CouponApi.getCouponPage({
  129. pageNo: state.pagination.pageNo,
  130. pageSize: state.pagination.pageSize,
  131. status: state.type
  132. });
  133. if (code !== 0) {
  134. return;
  135. }
  136. state.pagination.list = _.concat(state.pagination.list, data.list);
  137. state.pagination.total = data.total;
  138. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  139. }
  140. // 领取优惠劵
  141. async function getBuy(id) {
  142. const { code } = await CouponApi.takeCoupon(id);
  143. if (code !== 0) {
  144. return;
  145. }
  146. uni.showToast({
  147. title: '领取成功',
  148. });
  149. setTimeout(() => {
  150. resetPagination(state.pagination);
  151. getData();
  152. }, 1000);
  153. }
  154. // 加载更多
  155. function loadMore() {
  156. if (state.loadStatus === 'noMore') {
  157. return;
  158. }
  159. state.pagination.pageNo++;
  160. if (state.currentTab === 0) {
  161. getData();
  162. } else {
  163. getCoupon();
  164. }
  165. }
  166. onLoad((Option) => {
  167. // 领劵中心
  168. if (Option.type === 'all' || !Option.type) {
  169. getData();
  170. // 我的优惠劵
  171. } else {
  172. Option.type === 'geted'
  173. ? (state.currentTab = 1)
  174. : Option.type === 'used'
  175. ? (state.currentTab = 2)
  176. : (state.currentTab = 3);
  177. state.type = state.currentTab;
  178. getCoupon();
  179. }
  180. });
  181. onReachBottom(() => {
  182. loadMore();
  183. });
  184. </script>
  185. <style lang="scss" scoped>
  186. .card-btn {
  187. // width: 144rpx;
  188. padding: 0 16rpx;
  189. height: 50rpx;
  190. border-radius: 40rpx;
  191. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  192. color: #ffffff;
  193. font-size: 24rpx;
  194. font-weight: 400;
  195. }
  196. .border-btn {
  197. background: linear-gradient(90deg, var(--ui-BG-Main-opacity-4), var(--ui-BG-Main-light));
  198. color: #fff !important;
  199. }
  200. .disabled-btn {
  201. background: #cccccc;
  202. background-color: #cccccc !important;
  203. color: #fff !important;
  204. }
  205. </style>