list.vue 6.3 KB

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