Book.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <div>
  3. <TopSwiper :tops='tops'></TopSwiper>
  4. <!-- <p>图书列表</p> -->
  5. <Card v-for="book in books" :key="book.openid" :book='book'></Card>
  6. <p class="text-footer" v-if="!more">
  7. 没有更多数据
  8. </p>
  9. </div>
  10. </template>
  11. <script>
  12. // 35条数据
  13. // 每次加载10条
  14. // 0页 0-10
  15. // 1 10-20
  16. // 2 20-30(5)
  17. // page 当前第几页
  18. // 没有更多数据
  19. // 1. page=0 不能显示这条提醒
  20. // 2. page>0 数据长度<10 停止触底加载
  21. import {get} from '@/util'
  22. import Card from '@/components/Card'
  23. import TopSwiper from '@/components/TopSwiper'
  24. export default {
  25. components: {
  26. Card,
  27. TopSwiper
  28. },
  29. data () {
  30. return {
  31. books: [],
  32. page: 0,
  33. more: true,
  34. tops: []
  35. }
  36. },
  37. methods: {
  38. async getList (init) {
  39. if (init) {
  40. this.page = 0
  41. this.more = true
  42. }
  43. wx.showNavigationBarLoading()
  44. const books = await get('/weapp/booklist', {page: this.page})
  45. if (books.list.length < 10 && this.page > 0) {
  46. this.more = false // 这种情况下 没有更多数据了
  47. // console.log(this.more);
  48. }
  49. if (init) {
  50. this.books = books.list
  51. wx.stopPullDownRefresh() // 手动停止下拉刷新
  52. } else {
  53. // 下拉刷新,不能直接覆盖books 而是累加
  54. this.books = this.books.concat(books.list)
  55. }
  56. wx.hideNavigationBarLoading() // 关闭下拉刷新加载
  57. },
  58. async getTop () {
  59. const tops = await get('/weapp/top')
  60. this.tops = tops.list
  61. }
  62. },
  63. onPullDownRefresh () {
  64. this.getList(true)
  65. // console.log('下拉');
  66. this.getTop()
  67. },
  68. onReachBottom () {
  69. // console.log('没有更多了')
  70. if (!this.more) {
  71. // 没有更多了
  72. return false
  73. }
  74. this.page++
  75. // console.log('上啦加载', this.page)
  76. this.getList()
  77. },
  78. mounted () {
  79. this.getList(true)
  80. this.getTop()
  81. }
  82. }
  83. </script>
  84. <style lang='scss'>
  85. </style>