index-commu.nvue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <div class="index-commu" :style="{ height: $store.state.device.screenHeight - 49 + 'px' }">
  3. <div class="navbar">
  4. <text class="navbar-text" :class="{ bangs: bangs }">社区</text>
  5. </div>
  6. <div v-if="logged" class="content" :style="{ width: $store.state.device.screenWidth + 'px' }">
  7. <div class="swiper-nav">
  8. <div class="swiper-nav-items">
  9. <div class="swiper-nav-item" v-for="(item, index) in typeList" :key="index" @tap="switchSwiper(index)">
  10. <text class="swiper-nav-item-text" :class="{ 'swiper-nav-item-text-act': swiperActiveIndex === index }">{{ item }}</text>
  11. </div>
  12. </div>
  13. <div class="swiper-nav-bar">
  14. <div class="swiper-nav-bar-drog" :style="{ left: moveBarLeft + 'px' }">
  15. <text class="swiper-nav-bar-drog-item"></text>
  16. </div>
  17. </div>
  18. </div>
  19. <swiper class="swiper" @scroll="scrollHandler" :duration="300" :current="swiperNowIndex" @change="onswiperchange" @animationfinish="animationfinish">
  20. <swiper-item v-for="(item, typeIndex) in typeList" :key="typeIndex">
  21. <list class="swiper-list" :loadmoreoffset="20" @loadmore="loadmore">
  22. <refresh style="height: 60px; justify-content: center; align-items: center;"
  23. @refresh="onrefresh" @pullingdown="onpullingdown" :display="refreshing ? 'show' : 'hide'">
  24. <text style="font-size: 14px; color: #666666;">{{ pulldownRefreshText }}</text>
  25. </refresh>
  26. <cell class="swiper-list-item" v-for="(item, index) in lists[typeIndex]" :key="index">
  27. <list-item :item="item" :typeIndex="typeIndex" :listIndex="index" />
  28. </cell>
  29. <cell class="swiper-list-item-loading" :class="{ 'swiper-list-item-loading-center': !lists[typeIndex].length }">
  30. <text v-if="page[typeIndex] > 0" class="swiper-list-item-loading-text">...加载中...</text>
  31. <text v-else-if="page[typeIndex] === -1" class="swiper-list-item-loading-text">...网络崩溃了,下拉刷新试试...</text>
  32. <text v-else class="swiper-list-item-loading-text">没有更多了</text>
  33. </cell>
  34. </list>
  35. </swiper-item>
  36. </swiper>
  37. </div>
  38. <div v-else class="login-btn">
  39. <text class="btn" :style="{ bottom: screenHeight / 2 - 88 + 'px' }">登录/注册</text>
  40. </div>
  41. </div>
  42. </template>
  43. <script>
  44. import listItem from './components/list-item.nvue'
  45. import { commuRecommend, commuNewest, commuChanglai, commuCollect } from '@/apis/commu.js'
  46. export default {
  47. components: { listItem },
  48. data() {
  49. return {
  50. isLoading: true,
  51. refreshing: false,
  52. moveBarLeft: 0,
  53. swiperNowIndex: 0,
  54. swiperFinishIndex: 0,
  55. swiperActiveIndex: 0,
  56. page: [1, 1, 1, 1],
  57. pulldownRefreshText: '... 继续拉下刷新列表 ...',
  58. lists: { 0: [], 1: [], 2: [], 3: [] }, // 数据
  59. typeList: ['热门推荐', '最新更新', '常来微聊', '我的收藏'],
  60. apis: [commuRecommend, commuNewest, commuChanglai, commuCollect]
  61. }
  62. },
  63. computed: {
  64. bangs() { return this.$store.state.device.bangs },
  65. screenHeight() { return this.$store.state.device.screenHeight },
  66. logged() { return this.$store.state.app.token.length === 0 ? false : true }
  67. },
  68. onShow() {
  69. if (uni.getStorageSync('_INDEXARTICLETAP')) {
  70. this.switchSwiper(2)
  71. uni.removeStorageSync('_INDEXARTICLETAP')
  72. }
  73. },
  74. onLoad() {
  75. if (this.logged) {
  76. this.request()
  77. }
  78. },
  79. onBackPress() { // 返回时收起分享
  80. if (uni._SHARE && uni._SHARE.isVisible()) {
  81. uni._MASK.hide()
  82. uni._SHARE.hide()
  83. return true
  84. }
  85. },
  86. watch: {
  87. lists: {
  88. handler(n) {
  89. this.$store.commit('article/UPDATA', n)
  90. },
  91. deep: true,
  92. immediate: true
  93. }
  94. },
  95. methods: {
  96. // onswiperscroll(e) {
  97. // this.moveBarLeft = this.swiperFinishIndex * (this.$store.state.device.screenWidth / this.typeList.length) + e.detail.dx / this.typeList.length
  98. // },
  99. onswiperchange({ detail: { current } }) {
  100. this.swiperActiveIndex = current
  101. this.moveBarLeft = this.swiperActiveIndex * (this.$store.state.device.screenWidth / this.typeList.length)
  102. },
  103. animationfinish({ detail: { current } }) {
  104. this.swiperFinishIndex = this.swiperNowIndex = current
  105. if (!this.lists[this.swiperNowIndex].length && this.page[this.swiperNowIndex] !== 0) { // 当当前类型数量为 0 且有不是 没有更多时请求列表
  106. this.request()
  107. }
  108. },
  109. switchSwiper(index) {
  110. this.swiperActiveIndex = this.swiperNowIndex = index
  111. this.moveBarLeft = this.swiperActiveIndex * (this.$store.state.device.screenWidth / this.typeList.length)
  112. },
  113. request(action) {
  114. return new Promise((resolve, reject) => {
  115. plus.nativeUI.showWaiting()
  116. this.apis[this.swiperNowIndex]({ page: this.page[this.swiperNowIndex] }).then(res => {
  117. resolve()
  118. action ? this.lists[this.swiperNowIndex] = [] : ''
  119. this.lists[this.swiperNowIndex] = [...this.lists[this.swiperNowIndex], ...res.data.list]
  120. res.data.list.length < res.data.size ? this.page[this.swiperNowIndex] = 0 : this.page[this.swiperNowIndex]++ // 如果返回列表数量小于 10 表示没有更多了
  121. }).catch(() => { // 网络请求失败 进入失败状态
  122. resolve()
  123. this.lists[this.swiperNowIndex] = []
  124. this.page[this.swiperNowIndex] = -1
  125. })
  126. })
  127. },
  128. onrefresh(e) {
  129. this.refreshing = true
  130. this.pulldownRefreshText = '... 刷新中 ...'
  131. this.page[this.swiperNowIndex] = 1
  132. this.request('refresh').then(() => {
  133. this.refreshing = false
  134. this.pulldownRefreshText = '... 继续拉下刷新列表 ...'
  135. })
  136. },
  137. onpullingdown(e) {
  138. if (e.pullingDistance >= e.viewHeight) {
  139. this.pulldownRefreshText = '... 松开手指刷新列表 ...'
  140. } else {
  141. this.pulldownRefreshText = '... 继续拉下刷新列表 ...'
  142. }
  143. },
  144. loadmore() {
  145. this.request()
  146. },
  147. scrollHandler() {
  148. console.log(456)
  149. }
  150. }
  151. }
  152. </script>
  153. <style>
  154. .index-commu { background-color: #B2B2B2; }
  155. .navbar { justify-content: flex-end; align-items: center; background-color: #FFFFFF; height: 130rpx; border-bottom-color: #B2B2B2; border-bottom-width: 2rpx; }
  156. .navbar-text { font-size: 40rpx; margin-bottom: 24rpx; }
  157. .bangs { margin-bottom: 16rpx; }
  158. .content { flex: 1; background-color: #F2F2F2; }
  159. .swiper-nav { height: 84rpx; width: 750rpx; background-color: #FFFFFF; flex-direction: column; }
  160. .swiper-nav-items { flex: 1; width: 750rpx; flex-direction: row; }
  161. .swiper-nav-bar { height: 6rpx; width: 750rpx; position: relative; }
  162. .swiper-nav-bar-drog { position: absolute; left: 0px; top: 0px; width: 187.5rpx; height: 6rpx; align-items: center; }
  163. .swiper-nav-bar-drog-item { height: 6rpx; background-color: #F76454; width: 100rpx; }
  164. .swiper-nav-item { flex: 1; justify-content: center; align-items: center; height: 88rpx; }
  165. .swiper-nav-item-text { font-size: 32rpx; color: #666666; }
  166. .swiper-nav-item-text-act { color: #F76454; }
  167. .swiper { flex: 1; width: 750rpx; background-color: #FFFFFF; }
  168. .swiper-list { background-color: #E5E5E5; }
  169. .swiper-list-item { border-top-color: #F2F2F2; border-top-width: 10rpx; background-color: #FFFFFF; padding-left: 30rpx; padding-right: 30rpx; }
  170. .swiper-list-item-loading { justify-content: center; align-items: center; height: 60rpx; }
  171. .swiper-list-item-loading-center { flex: 1; }
  172. .swiper-list-item-loading-text { color: #666666; font-size: 26rpx; }
  173. .login-btn { flex: 1; background-color: #FFFFFF; width: 750rpx; align-items: center; justify-content: center; }
  174. .btn { position: fixed; left: 176rpx; width: 400rpx; height: 88rpx; color: #FFFFFF; font-size: 33.3rpx; border-radius: 10rpx; background-color: #F76454; line-height: 88rpx; text-align: center; }
  175. </style>