index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one">
  5. <scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage">
  6. <view class="list-scroll-view">
  7. <discount :Style="Style" :couponList="list" @toReceive="toReceive"></discount>
  8. </view>
  9. </scroll-view>
  10. </view>
  11. </view>
  12. </mobile-frame>
  13. </template>
  14. <script>
  15. import discount from '@/components/discount/index.vue';
  16. export default {
  17. components: {
  18. discount
  19. },
  20. data() {
  21. return {
  22. Style: {
  23. receive: true
  24. },
  25. user: {},
  26. list: [],
  27. total: 0,
  28. skip: 0,
  29. limit: 5,
  30. page: 0
  31. };
  32. },
  33. onLoad: function(e) {
  34. const that = this;
  35. // 监听用户是否登录
  36. that.watchLogin();
  37. },
  38. methods: {
  39. // 领取优惠劵
  40. async toReceive(e) {
  41. const that = this;
  42. const arr = await that.$api(`/userCoupon/getCoupon/${e._id}`, 'POST');
  43. if (arr.errcode == '0') {
  44. uni.showToast({
  45. title: `领取成功`,
  46. icon: 'none',
  47. duration: 3000
  48. })
  49. uni.navigateBack({
  50. detail: 1
  51. })
  52. } else {
  53. uni.showToast({
  54. title: arr.errmsg,
  55. icon: 'none',
  56. duration: 2000
  57. });
  58. }
  59. },
  60. // 监听用户是否登录
  61. watchLogin() {
  62. const that = this;
  63. uni.getStorage({
  64. key: 'token',
  65. success: function(res) {
  66. let user = that.$jwt(res.data);
  67. if (user) that.$set(that, `user`, user);
  68. that.search();
  69. },
  70. fail: function(err) {
  71. uni.navigateTo({
  72. url: `/pages/login/index`
  73. })
  74. }
  75. });
  76. },
  77. // 查询列表
  78. async search() {
  79. const that = this;
  80. let user = that.user;
  81. let info = {
  82. skip: that.skip,
  83. limit: that.limit,
  84. }
  85. const res = await that.$api(`/coupon`, 'GET', {
  86. ...info
  87. })
  88. if (res.errcode == '0') {
  89. let list = [...that.list, ...res.data];
  90. that.$set(that, `list`, list)
  91. that.$set(that, `total`, res.total)
  92. } else {
  93. uni.showToast({
  94. title: res.errmsg,
  95. });
  96. }
  97. },
  98. // 分页
  99. toPage(e) {
  100. const that = this;
  101. let list = that.list;
  102. let limit = that.limit;
  103. if (that.total > list.length) {
  104. uni.showLoading({
  105. title: '加载中',
  106. mask: true
  107. })
  108. let page = that.page + 1;
  109. that.$set(that, `page`, page)
  110. let skip = page * limit;
  111. that.$set(that, `skip`, skip)
  112. that.search();
  113. uni.hideLoading();
  114. } else uni.showToast({
  115. title: '没有更多数据了'
  116. });
  117. },
  118. // 清空列表
  119. clearPage() {
  120. const that = this;
  121. that.$set(that, `list`, [])
  122. that.$set(that, `skip`, 0)
  123. that.$set(that, `limit`, 5)
  124. that.$set(that, `page`, 0)
  125. }
  126. }
  127. }
  128. </script>
  129. <style lang="scss">
  130. .main {
  131. background-color: var(--f5Color);
  132. }
  133. </style>