index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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" :total="total"></discount>
  8. <view class="is_bottom" v-if="is_bottom">
  9. <text>{{config.bottom_title}}</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. // 系统设置
  26. config: {},
  27. Style: {
  28. status: true
  29. },
  30. user: {},
  31. list: [],
  32. total: 0,
  33. skip: 0,
  34. limit: 10,
  35. page: 0,
  36. // 数据是否触底
  37. is_bottom: false,
  38. scrollTop: 0,
  39. };
  40. },
  41. onLoad: function(e) {
  42. const that = this;
  43. that.searchConfig();
  44. // 监听用户是否登录
  45. that.watchLogin();
  46. },
  47. onPullDownRefresh: async function() {
  48. const that = this;
  49. that.clearPage();
  50. await that.search();
  51. uni.stopPullDownRefresh();
  52. },
  53. methods: {
  54. // 查询基本设置
  55. searchConfig() {
  56. const that = this;
  57. uni.getStorage({
  58. key: 'config',
  59. success: function(res) {
  60. if (res.data) that.$set(that, `config`, res.data)
  61. },
  62. fail: function(err) {
  63. console.log(err);
  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) {
  75. that.$set(that, `user`, user);
  76. that.search();
  77. }
  78. },
  79. fail: function(err) {
  80. uni.navigateTo({
  81. url: `/pages/login/index`
  82. })
  83. }
  84. });
  85. },
  86. // 查询列表
  87. async search() {
  88. const that = this;
  89. let user = that.user;
  90. let info = {
  91. skip: that.skip,
  92. limit: that.limit,
  93. customer: user._id
  94. }
  95. const res = await that.$api(`/userCoupon`, 'GET', {
  96. ...info
  97. })
  98. if (res.errcode == '0') {
  99. let list = [...that.list, ...res.data];
  100. that.$set(that, `list`, list)
  101. that.$set(that, `total`, res.total)
  102. } else {
  103. uni.showToast({
  104. title: res.errmsg,
  105. });
  106. }
  107. },
  108. // 分页
  109. toPage(e) {
  110. const that = this;
  111. let list = that.list;
  112. let limit = that.limit;
  113. if (that.total > list.length) {
  114. uni.showLoading({
  115. title: '加载中',
  116. mask: true
  117. })
  118. let page = that.page + 1;
  119. that.$set(that, `page`, page)
  120. let skip = page * limit;
  121. that.$set(that, `skip`, skip)
  122. that.search();
  123. uni.hideLoading();
  124. } else that.$set(that, `is_bottom`, true)
  125. },
  126. toScroll(e) {
  127. const that = this;
  128. let up = that.scrollTop;
  129. that.$set(that, `scrollTop`, e.detail.scrollTop);
  130. let num = Math.sign(up - e.detail.scrollTop);
  131. if (num == 1) that.$set(that, `is_bottom`, false);
  132. },
  133. // 清空列表
  134. clearPage() {
  135. const that = this;
  136. that.$set(that, `list`, [])
  137. that.$set(that, `skip`, 0)
  138. that.$set(that, `limit`, 6)
  139. that.$set(that, `page`, 0)
  140. }
  141. }
  142. }
  143. </script>
  144. <style lang="scss">
  145. .main {
  146. background-color: var(--f5Color);
  147. }
  148. .scroll-view {
  149. position: absolute;
  150. top: 0;
  151. left: 0;
  152. right: 0;
  153. bottom: 0;
  154. .list-scroll-view {
  155. display: flex;
  156. flex-direction: column;
  157. }
  158. }
  159. .is_bottom {
  160. text-align: center;
  161. text {
  162. padding: 2vw 0;
  163. display: inline-block;
  164. color: #858585;
  165. font-size: 14px;
  166. }
  167. }
  168. </style>