index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. uni.showModal({
  43. title: '提示',
  44. content: '确定领取该优惠券?',
  45. success: async function(res) {
  46. if (res.confirm) {
  47. const arr = await that.$api(`/userCoupon/getCoupon/${e._id}`, 'POST');
  48. if (arr.errcode == '0') {
  49. uni.showToast({
  50. title: `领取成功`,
  51. icon: 'none',
  52. duration: 3000
  53. })
  54. that.clearPage();
  55. that.search();
  56. } else {
  57. uni.showToast({
  58. title: arr.errmsg,
  59. icon: 'none',
  60. duration: 2000
  61. });
  62. }
  63. }
  64. }
  65. });
  66. },
  67. // 监听用户是否登录
  68. watchLogin() {
  69. const that = this;
  70. uni.getStorage({
  71. key: 'token',
  72. success: function(res) {
  73. let user = that.$jwt(res.data);
  74. if (user) that.$set(that, `user`, user);
  75. that.search();
  76. },
  77. fail: function(err) {
  78. uni.navigateTo({
  79. url: `/pages/login/index`
  80. })
  81. }
  82. });
  83. },
  84. // 查询列表
  85. async search() {
  86. const that = this;
  87. let user = that.user;
  88. let info = {
  89. skip: that.skip,
  90. limit: that.limit,
  91. }
  92. const res = await that.$api(`/coupon/userView`, 'GET', {
  93. ...info
  94. })
  95. if (res.errcode == '0') {
  96. let list = [...that.list, ...res.data];
  97. that.$set(that, `list`, list)
  98. that.$set(that, `total`, res.total)
  99. } else {
  100. uni.showToast({
  101. title: res.errmsg,
  102. });
  103. }
  104. },
  105. // 分页
  106. toPage(e) {
  107. const that = this;
  108. let list = that.list;
  109. let limit = that.limit;
  110. if (that.total > list.length) {
  111. uni.showLoading({
  112. title: '加载中',
  113. mask: true
  114. })
  115. let page = that.page + 1;
  116. that.$set(that, `page`, page)
  117. let skip = page * limit;
  118. that.$set(that, `skip`, skip)
  119. that.search();
  120. uni.hideLoading();
  121. } else uni.showToast({
  122. title: '没有更多数据了'
  123. });
  124. },
  125. // 清空列表
  126. clearPage() {
  127. const that = this;
  128. that.$set(that, `list`, [])
  129. that.$set(that, `skip`, 0)
  130. that.$set(that, `limit`, 5)
  131. that.$set(that, `page`, 0)
  132. }
  133. }
  134. }
  135. </script>
  136. <style lang="scss">
  137. .main {
  138. background-color: var(--f5Color);
  139. }
  140. </style>