list.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. const pagination = {
  92. data: [],
  93. current_page: 1,
  94. total: 1,
  95. last_page: 1,
  96. };
  97. // 数据
  98. const state = reactive({
  99. currentTab: 0,
  100. pagination: {
  101. data: [],
  102. current_page: 1,
  103. total: 1,
  104. last_page: 1,
  105. },
  106. loadStatus: '',
  107. type: '',
  108. });
  109. const tabMaps = [
  110. {
  111. name: '领券中心',
  112. value: 'all',
  113. },
  114. {
  115. name: '已领取',
  116. value: 'geted',
  117. },
  118. {
  119. name: '已使用',
  120. value: 'used',
  121. },
  122. {
  123. name: '已失效',
  124. value: 'expired',
  125. },
  126. ];
  127. function onTabsChange(e) {
  128. state.pagination = pagination
  129. state.currentTab = e.index;
  130. state.type = e.value;
  131. if (state.currentTab == 0) {
  132. getData();
  133. } else {
  134. getCoupon();
  135. }
  136. }
  137. async function getData(page = 1, list_rows = 5) {
  138. state.loadStatus = 'loading';
  139. const res = await sheep.$api.coupon.list({ list_rows, page });
  140. if (res.error === 0) {
  141. let couponlist = _.concat(state.pagination.data, res.data.data);
  142. state.pagination = {
  143. ...res.data,
  144. data: couponlist,
  145. };
  146. if (state.pagination.current_page < state.pagination.last_page) {
  147. state.loadStatus = 'more';
  148. } else {
  149. state.loadStatus = 'noMore';
  150. }
  151. }
  152. }
  153. async function getCoupon(page = 1, list_rows = 5) {
  154. state.loadStatus = 'loading';
  155. let res = await sheep.$api.coupon.userCoupon({
  156. type: state.type,
  157. list_rows,
  158. page,
  159. });
  160. if (res.error === 0) {
  161. if (page >= 2) {
  162. let couponlist = _.concat(state.pagination.data, res.data.data);
  163. state.pagination = {
  164. ...res.data,
  165. data: couponlist,
  166. };
  167. } else {
  168. state.pagination = res.data;
  169. }
  170. if (state.pagination.current_page < state.pagination.last_page) {
  171. state.loadStatus = 'more';
  172. } else {
  173. state.loadStatus = 'noMore';
  174. }
  175. }
  176. }
  177. async function getBuy(id) {
  178. const { error, msg } = await sheep.$api.coupon.get(id);
  179. if (error === 0) {
  180. uni.showToast({
  181. title: msg,
  182. });
  183. setTimeout(() => {
  184. state.pagination = pagination
  185. getData();
  186. }, 1000);
  187. }
  188. }
  189. // 加载更多
  190. function loadmore() {
  191. if (state.loadStatus !== 'noMore') {
  192. if (state.currentTab == 0) {
  193. getData(state.pagination.current_page + 1);
  194. } else {
  195. getCoupon(state.pagination.current_page + 1);
  196. }
  197. }
  198. }
  199. onLoad((Option) => {
  200. if (Option.type === 'all' || !Option.type) {
  201. getData();
  202. } else {
  203. state.type = Option.type;
  204. Option.type === 'geted'
  205. ? (state.currentTab = 1)
  206. : Option.type === 'used'
  207. ? (state.currentTab = 2)
  208. : (state.currentTab = 3);
  209. getCoupon();
  210. }
  211. });
  212. onReachBottom(() => {
  213. loadmore();
  214. });
  215. </script>
  216. <style lang="scss" scoped>
  217. .card-btn {
  218. // width: 144rpx;
  219. padding: 0 16rpx;
  220. height: 50rpx;
  221. border-radius: 40rpx;
  222. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  223. color: #ffffff;
  224. font-size: 24rpx;
  225. font-weight: 400;
  226. }
  227. .border-btn {
  228. background: linear-gradient(90deg, var(--ui-BG-Main-opacity-4), var(--ui-BG-Main-light));
  229. color: #fff !important;
  230. }
  231. .disabled-btn {
  232. background: #cccccc;
  233. background-color: #cccccc !important;
  234. color: #fff !important;
  235. }
  236. </style>