all_books.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // parents//pages/all_books/all_books.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
  3. import { ThemeBorrowedList } from '../../../utils/api.js'
  4. const { navigateTo, redirectTo, navigateBack } = routers()
  5. const { globalData, hasLibraryService, hasVipService } = getApp()
  6. const { baseImgUrl, qrcodeInvalidToastText } = globalData
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. baseImgUrl,
  13. isLoaded: 0,
  14. pageSize: 10,
  15. pageNo: 1,
  16. isAll: 0,
  17. nodataArray: {
  18. text: "暂无阅读计划",
  19. margin: '140rpx auto'
  20. }
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function (options) {
  26. options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`)
  27. // options.vipId = '306330bd-5c8c-41aa-8a00-0f0c296ebb29',
  28. options.libId = getGlobalVal('library').id || ''
  29. this.setData({ options })
  30. console.log(options)
  31. this.getData(res => {
  32. this.setData({ isLoaded: 1 })
  33. })
  34. },
  35. /**
  36. * 生命周期函数--监听页面初次渲染完成
  37. */
  38. onReady: function () {
  39. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  40. for (const i in obj) {
  41. this[i] = obj[i]
  42. }
  43. },
  44. /**
  45. * 生命周期函数--监听页面显示
  46. */
  47. onShow: function () {
  48. },
  49. /**
  50. * 生命周期函数--监听页面隐藏
  51. */
  52. onHide: function () {
  53. },
  54. /**
  55. * 生命周期函数--监听页面卸载
  56. */
  57. onUnload: function () {
  58. },
  59. /**
  60. * 页面相关事件处理函数--监听用户下拉动作
  61. */
  62. onPullDownRefresh: function () {
  63. const { isAll} = this.data
  64. this.setData({ pageNo: 1 })
  65. this.getData(res => {
  66. wx.stopPullDownRefresh()
  67. })
  68. },
  69. /**
  70. * 页面上拉触底事件的处理函数
  71. */
  72. onReachBottom: function () {
  73. const { isAll } = this.data
  74. if (isAll) return
  75. this.getData()
  76. },
  77. /**
  78. * 用户点击右上角分享
  79. */
  80. onShareAppMessage: function () {
  81. if (isFn(sharePage)) return sharePage()
  82. },
  83. /**
  84. * 获取数据
  85. */
  86. getData: function (cb) {
  87. const continuousFn = { fn: this.getData, param: { ...arguments } }
  88. const { pageNo, pageSize, options, list} = this.data
  89. const { vipId, code } = options
  90. ThemeBorrowedList({ data: { pageNo, pageSize, vipId, code }, continuousFn }).then(res => {
  91. const { pages } = res.data
  92. const newList = res.data.list
  93. const listTemp = (res => {
  94. for (let i in res) {
  95. if (res[i].summary.length > 30) {
  96. res[i].summary = res[i].summary.slice(0, 30) + '...'
  97. } else {
  98. res[i].summary = res[i].summary
  99. }
  100. }
  101. return res
  102. })(newList)
  103. let arr = []
  104. if (pageNo == 1) {
  105. arr = [...listTemp]
  106. } else {
  107. arr = [...list, ...listTemp]
  108. }
  109. this.setData({ list: arr, pages, isAll: newList.length < pageSize, pageNo: newList.length == pageSize ? pageNo + 1 : pageNo, isLoaded: true })
  110. cb && cb()
  111. }).catch(res => {
  112. this.setData({ isLoaded: true })
  113. cb && cb()
  114. })
  115. },
  116. })