list.vue 5.8 KB

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