index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one">
  5. <scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
  6. <view class="list-scroll-view">
  7. <discount :Style="Style" :couponList="list"></discount>
  8. <view class="is_bottom" v-if="is_bottom">
  9. <text>我们也是有底线的!</text>
  10. </view>
  11. </view>
  12. </scroll-view>
  13. </view>
  14. </view>
  15. </mobile-frame>
  16. </template>
  17. <script>
  18. import discount from '@/components/discount/index.vue';
  19. export default {
  20. components: {
  21. discount
  22. },
  23. data() {
  24. return {
  25. Style: {
  26. status: true
  27. },
  28. user: {},
  29. list: [],
  30. total: 0,
  31. skip: 0,
  32. limit: 6,
  33. page: 0,
  34. // 数据是否触底
  35. is_bottom: false,
  36. scrollTop: 0,
  37. };
  38. },
  39. onLoad: function(e) {
  40. const that = this;
  41. // 监听用户是否登录
  42. that.watchLogin();
  43. },
  44. methods: {
  45. // 监听用户是否登录
  46. watchLogin() {
  47. const that = this;
  48. uni.getStorage({
  49. key: 'token',
  50. success: function(res) {
  51. let user = that.$jwt(res.data);
  52. if (user) that.$set(that, `user`, user);
  53. that.search();
  54. },
  55. fail: function(err) {
  56. uni.navigateTo({
  57. url: `/pages/login/index`
  58. })
  59. }
  60. });
  61. },
  62. // 查询列表
  63. async search() {
  64. const that = this;
  65. let user = that.user;
  66. let info = {
  67. skip: that.skip,
  68. limit: that.limit,
  69. customer: user._id
  70. }
  71. const res = await that.$api(`/userCoupon`, 'GET', {
  72. ...info
  73. })
  74. if (res.errcode == '0') {
  75. let list = [...that.list, ...res.data];
  76. that.$set(that, `list`, list)
  77. that.$set(that, `total`, res.total)
  78. } else {
  79. uni.showToast({
  80. title: res.errmsg,
  81. });
  82. }
  83. },
  84. // 分页
  85. toPage(e) {
  86. const that = this;
  87. let list = that.list;
  88. let limit = that.limit;
  89. if (that.total > list.length) {
  90. uni.showLoading({
  91. title: '加载中',
  92. mask: true
  93. })
  94. let page = that.page + 1;
  95. that.$set(that, `page`, page)
  96. let skip = page * limit;
  97. that.$set(that, `skip`, skip)
  98. that.search();
  99. uni.hideLoading();
  100. } else that.$set(that, `is_bottom`, true)
  101. },
  102. toScroll(e) {
  103. const that = this;
  104. let up = that.scrollTop;
  105. that.$set(that, `scrollTop`, e.detail.scrollTop);
  106. let num = Math.sign(up - e.detail.scrollTop);
  107. if (num == 1) that.$set(that, `is_bottom`, false);
  108. },
  109. // 清空列表
  110. clearPage() {
  111. const that = this;
  112. that.$set(that, `list`, [])
  113. that.$set(that, `skip`, 0)
  114. that.$set(that, `limit`, 5)
  115. that.$set(that, `page`, 0)
  116. }
  117. }
  118. }
  119. </script>
  120. <style lang="scss">
  121. .main {
  122. background-color: var(--f5Color);
  123. }
  124. .scroll-view {
  125. position: absolute;
  126. top: 0;
  127. left: 0;
  128. right: 0;
  129. bottom: 0;
  130. .list-scroll-view {
  131. display: flex;
  132. flex-direction: column;
  133. }
  134. }
  135. .is_bottom {
  136. text-align: center;
  137. text {
  138. padding: 2vw 0;
  139. display: inline-block;
  140. }
  141. }
  142. </style>