index.vue 2.9 KB

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