list.vue 6.4 KB

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