index.vue 3.2 KB

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