list.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. ></su-tabs>
  11. </su-sticky>
  12. <s-empty
  13. v-if="state.pagination.total === 0"
  14. icon="/static/coupon-empty.png"
  15. text="暂无优惠券"
  16. ></s-empty>
  17. <template v-if="state.currentTab == '0'">
  18. <view v-for="item in state.pagination.data" :key="item.id">
  19. <s-coupon-list
  20. :data="item"
  21. @tap="
  22. sheep.$router.go('/pages/coupon/detail', {
  23. id: item.id,
  24. })
  25. "
  26. >
  27. <template #default>
  28. <button
  29. class="ss-reset-button card-btn ss-flex ss-row-center ss-col-center"
  30. :class="item.get_status != 'can_get' ? 'border-btn' : ''"
  31. @click.stop="getBuy(item.id)"
  32. :disabled="item.get_status != 'can_get'"
  33. >
  34. {{ item.get_status_text }}
  35. </button>
  36. </template>
  37. </s-coupon-list>
  38. </view>
  39. </template>
  40. <template v-else>
  41. <view v-for="item in state.pagination.data" :key="item.id">
  42. <s-coupon-list
  43. :data="item"
  44. type="user"
  45. @tap="
  46. sheep.$router.go('/pages/coupon/detail', {
  47. id: item.coupon_id,
  48. user_coupon_id: item.id,
  49. })
  50. "
  51. >
  52. <template #default>
  53. <button
  54. class="ss-reset-button card-btn ss-flex ss-row-center ss-col-center"
  55. :class="
  56. item.status == 'can_get' || item.status == 'can_use'
  57. ? ''
  58. : item.status == 'used' || item.status == 'expired'
  59. ? 'disabled-btn'
  60. : 'border-btn'
  61. "
  62. :disabled="item.status != 'can_get' && item.status != 'can_use'"
  63. @click.stop="
  64. sheep.$router.go('/pages/coupon/detail', {
  65. id: item.coupon_id,
  66. user_coupon_id: item.id,
  67. })
  68. "
  69. >
  70. {{ item.status_text }}
  71. </button>
  72. </template>
  73. </s-coupon-list>
  74. </view>
  75. </template>
  76. <uni-load-more
  77. v-if="state.pagination.total > 0"
  78. :status="state.loadStatus"
  79. :content-text="{
  80. contentdown: '上拉加载更多',
  81. }"
  82. @tap="loadmore"
  83. />
  84. </s-layout>
  85. </template>
  86. <script setup>
  87. import sheep from '@/sheep';
  88. import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  89. import { computed, reactive } from 'vue';
  90. import _ from 'lodash';
  91. // 数据
  92. const state = reactive({
  93. currentTab: 0,
  94. pagination: {
  95. data: [],
  96. current_page: 1,
  97. total: 1,
  98. last_page: 1,
  99. },
  100. loadStatus: '',
  101. type: '',
  102. });
  103. const tabMaps = [
  104. {
  105. name: '领券中心',
  106. value: 'all',
  107. },
  108. {
  109. name: '已领取',
  110. value: 'geted',
  111. },
  112. {
  113. name: '已使用',
  114. value: 'used',
  115. },
  116. {
  117. name: '已失效',
  118. value: 'expired',
  119. },
  120. ];
  121. function onTabsChange(e) {
  122. state.pagination = {
  123. data: [],
  124. current_page: 1,
  125. total: 1,
  126. last_page: 1,
  127. };
  128. state.currentTab = e.index;
  129. state.type = e.value;
  130. if (state.currentTab == 0) {
  131. getData();
  132. } else {
  133. getCoupon();
  134. }
  135. }
  136. async function getData(page = 1, list_rows = 5) {
  137. state.loadStatus = 'loading';
  138. const res = await sheep.$api.coupon.list({ list_rows, page });
  139. if (res.error === 0) {
  140. if (page >= 2) {
  141. let couponlist = _.concat(state.pagination.data, res.data.data);
  142. state.pagination = {
  143. ...res.data,
  144. data: couponlist,
  145. };
  146. } else {
  147. state.pagination = res.data;
  148. }
  149. if (state.pagination.current_page < state.pagination.last_page) {
  150. state.loadStatus = 'more';
  151. } else {
  152. state.loadStatus = 'noMore';
  153. }
  154. }
  155. }
  156. async function getCoupon(page = 1, list_rows = 5) {
  157. state.loadStatus = 'loading';
  158. let res = await sheep.$api.coupon.userCoupon({
  159. type: state.type,
  160. list_rows,
  161. page,
  162. });
  163. if (res.error === 0) {
  164. if (page >= 2) {
  165. let couponlist = _.concat(state.pagination.data, res.data.data);
  166. state.pagination = {
  167. ...res.data,
  168. data: couponlist,
  169. };
  170. } else {
  171. state.pagination = res.data;
  172. }
  173. if (state.pagination.current_page < state.pagination.last_page) {
  174. state.loadStatus = 'more';
  175. } else {
  176. state.loadStatus = 'noMore';
  177. }
  178. }
  179. }
  180. async function getBuy(id) {
  181. const { error, msg } = await sheep.$api.coupon.get(id);
  182. if (error === 0) {
  183. uni.showToast({
  184. title: msg,
  185. });
  186. setTimeout(() => {
  187. getData();
  188. }, 1000);
  189. }
  190. }
  191. // 加载更多
  192. function loadmore() {
  193. if (state.loadStatus !== 'noMore') {
  194. if (state.currentTab == 0) {
  195. getData(state.pagination.current_page + 1);
  196. } else {
  197. getCoupon(state.pagination.current_page + 1);
  198. }
  199. }
  200. }
  201. onLoad((Option) => {
  202. if (Option.type === 'all' || !Option.type) {
  203. getData();
  204. } else {
  205. state.type = Option.type;
  206. Option.type === 'geted'
  207. ? (state.currentTab = 1)
  208. : Option.type === 'used'
  209. ? (state.currentTab = 2)
  210. : (state.currentTab = 3);
  211. getCoupon();
  212. }
  213. });
  214. onReachBottom(() => {
  215. loadmore();
  216. });
  217. </script>
  218. <style lang="scss" scoped>
  219. .card-btn {
  220. // width: 144rpx;
  221. padding: 0 16rpx;
  222. height: 50rpx;
  223. border-radius: 40rpx;
  224. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  225. color: #ffffff;
  226. font-size: 24rpx;
  227. font-weight: 400;
  228. }
  229. .border-btn {
  230. background: linear-gradient(90deg, var(--ui-BG-Main-opacity-4), var(--ui-BG-Main-light));
  231. color: #fff !important;
  232. }
  233. .disabled-btn {
  234. background: #cccccc;
  235. background-color: #cccccc !important;
  236. color: #fff !important;
  237. }
  238. </style>