list.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <!-- 优惠券中心 -->
  2. <template>
  3. <s-layout :title="$t('coupon.list.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="$t('commission.coupon.noCoupon')"
  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: $t('activity.index.uploadMore'),
  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 pagination = {
  93. data: [],
  94. current_page: 1,
  95. total: 1,
  96. last_page: 1,
  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: uni.getLocale() == 'en' ? 'all' : '全部',
  112. value: 'all',
  113. },
  114. {
  115. name: uni.getLocale() == 'en' ? 'received' : '已领取',
  116. value: 'geted',
  117. },
  118. {
  119. name: uni.getLocale() == 'en' ? 'used' : '已使用',
  120. value: 'used',
  121. },
  122. {
  123. name: uni.getLocale() == 'en' ? 'expired' : '已失效',
  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.code === 1) {
  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.code === 1) {
  161. let couponlist = _.concat(state.pagination.data, res.data.data);
  162. state.pagination = {
  163. ...res.data,
  164. data: couponlist,
  165. };
  166. if (state.pagination.current_page < state.pagination.last_page) {
  167. state.loadStatus = 'more';
  168. } else {
  169. state.loadStatus = 'noMore';
  170. }
  171. }
  172. }
  173. async function getBuy(id) {
  174. const { code, msg } = await sheep.$api.coupon.get(id);
  175. if (code === 1) {
  176. uni.showToast({
  177. title: msg,
  178. });
  179. setTimeout(() => {
  180. state.pagination = pagination;
  181. getData();
  182. }, 1000);
  183. }
  184. }
  185. // 加载更多
  186. function loadmore() {
  187. if (state.loadStatus !== 'noMore') {
  188. if (state.currentTab == 0) {
  189. getData(state.pagination.current_page + 1);
  190. } else {
  191. getCoupon(state.pagination.current_page + 1);
  192. }
  193. }
  194. }
  195. onLoad((Option) => {
  196. if (Option.type === 'all' || !Option.type) {
  197. getData();
  198. } else {
  199. state.type = Option.type;
  200. Option.type === 'geted'
  201. ? (state.currentTab = 1)
  202. : Option.type === 'used'
  203. ? (state.currentTab = 2)
  204. : (state.currentTab = 3);
  205. getCoupon();
  206. }
  207. });
  208. onReachBottom(() => {
  209. loadmore();
  210. });
  211. </script>
  212. <style lang="scss" scoped>
  213. .card-btn {
  214. // width: 144rpx;
  215. padding: 0 16rpx;
  216. height: 50rpx;
  217. border-radius: 40rpx;
  218. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  219. color: #ffffff;
  220. font-size: 24rpx;
  221. font-weight: 400;
  222. }
  223. .border-btn {
  224. background: linear-gradient(90deg, var(--ui-BG-Main-opacity-4), var(--ui-BG-Main-light));
  225. color: #fff !important;
  226. }
  227. .disabled-btn {
  228. background: #cccccc;
  229. background-color: #cccccc !important;
  230. color: #fff !important;
  231. }
  232. </style>