index.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <view class="main">
  3. <uv-navbar :title="config.title" placeholder autoBack></uv-navbar>
  4. <view class="list">
  5. <couponInfo v-for="(item, index) in listData" :key="index" :item="item" :show="checkIndex === index ? true : false" @click="checkCoupon(item, index)" :isCheck="isCheck" :isMyCoupon="isMyCoupon" @grabTickets="grabTickets" @useTickets="useTickets" />
  6. <view>
  7. <noData v-if="listData.length <= 0"></noData>
  8. </view>
  9. </view>
  10. <view v-if="isMyCoupon && loadStatus == 'nomore'" class="go-center">没有更多券了!<text class="light" @click="goCouponCenter()">去领券中心领取></text></view>
  11. <uv-loadmore v-else :status="loadStatus" />
  12. </view>
  13. </template>
  14. <script setup>
  15. import { ref } from "vue";
  16. import { onLoad, onReachBottom, onPullDownRefresh } from "@dcloudio/uni-app";
  17. import {
  18. couponPage_Api,
  19. couponReceiveRecordAdd_Api,
  20. couponReceiveRecordPage_Api,
  21. } from "@/api/index";
  22. const config = ref({
  23. title: "",
  24. });
  25. const pageParams1 = ref({
  26. pageNum: 1,
  27. pageSize: 10,
  28. status: 0, // 0可以使用,1已使用,2,未使用已过期
  29. });
  30. const pageParams2 = ref({
  31. pageNum: 1,
  32. pageSize: 10,
  33. });
  34. const listData = ref([]);
  35. const loadStatus = ref("loadmore"); //loading / nomore
  36. const isMyCoupon = ref(false); // 是否为“我的优惠券”
  37. const isCheck = ref(false); // 是否为“选择可使用的优惠券”
  38. const checkIndex = ref("");
  39. // 获取我的优惠券列表
  40. const getMyList = (isRefresh, needLoading = false) => {
  41. console.log("getMyList");
  42. if (!isRefresh && loadStatus.value == "nomore") {
  43. return false;
  44. }
  45. pageParams1.value.pageNum = isRefresh ? 1 : pageParams1.value.pageNum + 1;
  46. listData.value = isRefresh ? [] : listData.value;
  47. if (needLoading) {
  48. uni.showLoading({
  49. title: "努力加载中...",
  50. mask: true,
  51. });
  52. }
  53. loadStatus.value = "loading";
  54. couponReceiveRecordPage_Api(pageParams1.value)
  55. .then((res) => {
  56. if (res.code == 200) {
  57. let listDatas = listData.value;
  58. listDatas.push(...res.rows);
  59. // console.log(listDatas);
  60. listData.value = listDatas;
  61. loadStatus.value =
  62. listData.value.length >= res.total ? "nomore" : "loadmore";
  63. }
  64. })
  65. .finally(() => {
  66. if (needLoading) {
  67. // uni.hideLoading();
  68. }
  69. });
  70. };
  71. // 获取优惠券列表
  72. const getList = (isRefresh, needLoading = false) => {
  73. if (!isRefresh && loadStatus.value == "nomore") {
  74. return false;
  75. }
  76. pageParams2.value.pageNum = isRefresh ? 1 : pageParams2.value.pageNum + 1;
  77. listData.value = isRefresh ? [] : listData.value;
  78. if (needLoading) {
  79. uni.showLoading({
  80. title: "努力加载中...",
  81. mask: true,
  82. });
  83. }
  84. loadStatus.value = "loading";
  85. couponPage_Api(pageParams2.value)
  86. .then((res) => {
  87. if (res.code == 200) {
  88. let listDatas = listData.value;
  89. listDatas.push(...res.rows);
  90. listData.value = listDatas;
  91. loadStatus.value =
  92. listData.value.length >= res.total ? "nomore" : "loadmore";
  93. }
  94. })
  95. .finally(() => {
  96. if (needLoading) {
  97. // uni.hideLoading();
  98. }
  99. });
  100. };
  101. //领券
  102. const grabTickets = (id) => {
  103. uni.showLoading({
  104. title: "正在提交中...",
  105. mask: true,
  106. });
  107. couponReceiveRecordAdd_Api(id)
  108. .then((res) => {
  109. if (res.code == 200) {
  110. setTimeout(() => {
  111. uni.showToast({
  112. icon: "success",
  113. title: "领券成功",
  114. });
  115. }, 300);
  116. //获取上一个页面
  117. let pages = getCurrentPages();
  118. if (pages.length > 1) {
  119. let beforePage = pages[pages.length - 2];
  120. if (beforePage.$vm.isMyCoupon) {
  121. beforePage.$vm.getMyList(true);
  122. }
  123. }
  124. }
  125. })
  126. .finally(() => {
  127. // uni.hideLoading();
  128. });
  129. };
  130. const goCouponCenter = () => {
  131. uni.navigateTo({
  132. url: "/pages/couponCenter/index",
  133. });
  134. };
  135. const useTickets = () => {
  136. uni.switchTab({
  137. url: "/pages/tabtar/home",
  138. });
  139. };
  140. const checkCoupon = (item, index) => {
  141. if (isCheck.value) {
  142. if (checkIndex.value === index) {
  143. checkIndex.value = "";
  144. } else {
  145. checkIndex.value = index;
  146. }
  147. }
  148. let params = {
  149. couponId: 0,
  150. };
  151. if (checkIndex.value !== "") {
  152. params = {
  153. my_coupon_id: item.id,
  154. };
  155. }
  156. //获取上一个页面
  157. let pages = getCurrentPages();
  158. if (pages.length > 1) {
  159. let beforePage = pages[pages.length - 2];
  160. if (beforePage.$vm.changeCoupon) {
  161. beforePage.$vm.changeCoupon(params);
  162. }
  163. }
  164. };
  165. onLoad((option) => {
  166. // let objs = uni.getStorageSync("userBusinessInfo") || {};
  167. // // console.log(objs)
  168. // if (!objs.id) {
  169. // uni.$uv.toast("未获取到店铺信息");
  170. // uni.navigateBack();
  171. // return;
  172. // }
  173. // pageParams1.value.businessId = objs.businessId;
  174. // pageParams2.value.businessId = objs.businessId;
  175. // console.log(option);
  176. if (option.isMyCoupon) {
  177. isMyCoupon.value = true;
  178. config.value.title = "我的优惠券";
  179. getMyList(true);
  180. } else {
  181. config.value.title = "领券中心";
  182. getList(true);
  183. }
  184. });
  185. onPullDownRefresh(() => {
  186. if (isMyCoupon.value) {
  187. getMyList(true);
  188. } else if (isCheck.value) {
  189. getMyCanUseList();
  190. } else {
  191. getList(true);
  192. }
  193. uni.stopPullDownRefresh();
  194. });
  195. onReachBottom(() => {
  196. if (isMyCoupon.value) {
  197. getMyList();
  198. } else if (isCheck.value) {
  199. } else {
  200. getList();
  201. }
  202. });
  203. </script>
  204. <style lang="scss" scoped>
  205. .main {
  206. min-height: 100vh;
  207. background-color: rgba(253, 253, 253, 1);
  208. padding: 0 0 50rpx 0;
  209. .go-center {
  210. font-size: 26rpx;
  211. font-family: PingFang SC, PingFang SC-Regular;
  212. font-weight: 400;
  213. text-align: center;
  214. color: #808080;
  215. margin-top: 60rpx;
  216. .light {
  217. color: #00a7e6;
  218. }
  219. }
  220. .list {
  221. }
  222. }
  223. </style>