index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
  5. <view class="list-scroll-view">
  6. <discount :Style="Style" :couponList="list" :total="total" @toReceive="toReceive"></discount>
  7. <view class="is_bottom" v-if="is_bottom">
  8. <text>{{config.bottom_title}}</text>
  9. </view>
  10. </view>
  11. </scroll-view>
  12. </view>
  13. </mobile-frame>
  14. </template>
  15. <script>
  16. import discount from '@/components/discount/index.vue';
  17. export default {
  18. components: {
  19. discount
  20. },
  21. data() {
  22. return {
  23. config: {},
  24. user: {},
  25. Style: {
  26. receive: true
  27. },
  28. list: [],
  29. total: 0,
  30. skip: 0,
  31. limit: 10,
  32. page: 0,
  33. // 数据是否触底
  34. is_bottom: false,
  35. scrollTop: 0
  36. };
  37. },
  38. onLoad: function(e) {
  39. const that = this;
  40. // 监听用户是否登录
  41. that.watchLogin();
  42. // 下拉刷新
  43. },
  44. onShow: function() {
  45. const that = this;
  46. that.searchConfig();
  47. },
  48. onPullDownRefresh: async function() {
  49. const that = this;
  50. that.clearPage();
  51. await that.search();
  52. uni.stopPullDownRefresh();
  53. },
  54. methods: {
  55. // 查询基本设置
  56. searchConfig() {
  57. const that = this;
  58. uni.getStorage({
  59. key: 'config',
  60. success: function(res) {
  61. if (res.data) that.$set(that, `config`, res.data)
  62. },
  63. })
  64. },
  65. watchLogin() {
  66. const that = this;
  67. uni.getStorage({
  68. key: 'token',
  69. success: function(res) {
  70. let user = that.$jwt(res.data);
  71. if (user) {
  72. that.$set(that, `user`, user);
  73. that.search();
  74. }
  75. },
  76. fail: function(err) {
  77. console.log(err);
  78. }
  79. })
  80. },
  81. // 查询列表
  82. async search() {
  83. const that = this;
  84. let info = {
  85. skip: that.skip,
  86. limit: that.limit,
  87. }
  88. const res = await that.$api(`/coupon/userView`, 'GET', {
  89. ...info
  90. })
  91. if (res.errcode == '0') {
  92. let list = [...that.list, ...res.data];
  93. that.$set(that, `list`, list)
  94. that.$set(that, `total`, res.total)
  95. } else {
  96. uni.showToast({
  97. title: res.errmsg,
  98. });
  99. }
  100. },
  101. // 分页
  102. toPage() {
  103. const that = this;
  104. let list = that.list;
  105. let limit = that.limit;
  106. if (that.total > list.length) {
  107. uni.showLoading({
  108. title: '加载中',
  109. mask: true
  110. })
  111. let page = that.page + 1;
  112. that.$set(that, `page`, page)
  113. let skip = page * limit;
  114. that.$set(that, `skip`, skip)
  115. that.search();
  116. uni.hideLoading();
  117. } else that.$set(that, `is_bottom`, true)
  118. },
  119. toScroll(e) {
  120. const that = this;
  121. let up = that.scrollTop;
  122. that.$set(that, `scrollTop`, e.detail.scrollTop);
  123. let num = Math.sign(up - e.detail.scrollTop);
  124. if (num == 1) that.$set(that, `is_bottom`, false);
  125. },
  126. async toReceive(e) {
  127. const that = this;
  128. uni.showModal({
  129. title: '提示',
  130. content: '确定领取该优惠券?',
  131. success: async function(res) {
  132. if (res.confirm) {
  133. const arr = await that.$api(`/userCoupon/getCoupon/${e._id}`, 'POST');
  134. if (arr.errcode == '0') {
  135. if (arr.data) {
  136. uni.showToast({
  137. title: arr.data.msg,
  138. icon: 'none'
  139. });
  140. } else {
  141. uni.showToast({
  142. title: `领取成功`,
  143. icon: 'none'
  144. })
  145. that.clearPage();
  146. that.search();
  147. }
  148. } else {
  149. uni.showToast({
  150. title: arr.errmsg,
  151. icon: 'none',
  152. });
  153. }
  154. }
  155. }
  156. });
  157. },
  158. // 清空列表
  159. clearPage() {
  160. const that = this;
  161. that.$set(that, `list`, [])
  162. that.$set(that, `skip`, 0)
  163. that.$set(that, `limit`, 10)
  164. that.$set(that, `page`, 0)
  165. }
  166. },
  167. }
  168. </script>
  169. <style lang="scss">
  170. .main {
  171. background-color: var(--f5Color);
  172. }
  173. .scroll-view {
  174. position: absolute;
  175. top: 0;
  176. left: 0;
  177. right: 0;
  178. bottom: 0;
  179. .list-scroll-view {
  180. display: flex;
  181. flex-direction: column;
  182. }
  183. }
  184. .is_bottom {
  185. text-align: center;
  186. text {
  187. padding: 2vw 0;
  188. display: inline-block;
  189. color: #858585;
  190. font-size: 14px;
  191. }
  192. }
  193. </style>