index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. const res = await that.$api(`/coupon`, 'GET');
  82. if (res.errcode == '0') {
  83. let list = [...that.list, ...res.data];
  84. that.$set(that, `list`, list)
  85. that.$set(that, `total`, res.total)
  86. } else {
  87. uni.showToast({
  88. title: res.errmsg,
  89. });
  90. }
  91. },
  92. // 分页
  93. toPage(e) {
  94. const that = this;
  95. let list = that.list;
  96. let limit = that.limit;
  97. if (that.total > list.length) {
  98. uni.showLoading({
  99. title: '加载中',
  100. mask: true
  101. })
  102. let page = that.page + 1;
  103. that.$set(that, `page`, page)
  104. let skip = page * limit;
  105. that.$set(that, `skip`, skip)
  106. that.search();
  107. uni.hideLoading();
  108. } else uni.showToast({
  109. title: '没有更多数据了'
  110. });
  111. },
  112. // 清空列表
  113. clearPage() {
  114. const that = this;
  115. that.$set(that, `list`, [])
  116. that.$set(that, `skip`, 0)
  117. that.$set(that, `limit`, 5)
  118. that.$set(that, `page`, 0)
  119. }
  120. }
  121. }
  122. </script>
  123. <style lang="scss">
  124. .main {
  125. background-color: var(--f5Color);
  126. }
  127. </style>