bookshelf.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {
  2. getBookshelfList,
  3. getBrowsingHistory
  4. } from '../../api/api'
  5. import {
  6. goToBookDetail, SendEvent
  7. } from '../../utils/util'
  8. import {
  9. apple
  10. } from '../../config/config'
  11. let interstitialAd = null
  12. let app = getApp()
  13. Page({
  14. data: {
  15. activeTab: 'history',
  16. bookList: [],
  17. loading: false,
  18. hasMore: true,
  19. page: 1,
  20. size: 10,
  21. isLoggedIn: false
  22. },
  23. onLoad() {
  24. this.autoLogin()
  25. },
  26. onShow() {
  27. // 每次显示页面时刷新列表
  28. this.setData({
  29. page: 1,
  30. bookList: []
  31. })
  32. if (this.data.isLoggedIn) {
  33. this.loadBookList()
  34. }
  35. },
  36. onTabItemTap(item) {
  37. console.log(item.index)
  38. setTimeout(() => {
  39. this.interstitalPlayFn()
  40. }, 1000)
  41. },
  42. // 自动登录
  43. async autoLogin() {
  44. try {
  45. // 检查是否已经登录
  46. const token = wx.getStorageSync('accessToken');
  47. if (token) {
  48. this.setData({
  49. isLoggedIn: true
  50. });
  51. // 已经登录
  52. console.log('已经登录')
  53. // 页面首次加载时的处理
  54. this.loadBookList()
  55. this.interstitalLoad()
  56. return;
  57. }
  58. // 未登录,执行登录
  59. const res = await getApp().login();
  60. this.setData({
  61. isLoggedIn: true
  62. });
  63. console.log('重新登录', res)
  64. // 页面首次加载时的处理
  65. if (res.accessToken) {
  66. this.loadBookList();
  67. }
  68. this.interstitalLoad();
  69. } catch (error) {
  70. console.error('登录失败:', error);
  71. }
  72. },
  73. // 插屏广告加载
  74. interstitalLoad() {
  75. let adId = apple.ads.insertAdId
  76. interstitialAd = wx.createInterstitialAd({
  77. adUnitId: adId
  78. })
  79. interstitialAd.onLoad(() => {
  80. console.log('视频页插屏广告onLoad')
  81. })
  82. interstitialAd.onError((err) => {
  83. console.log('视频页插屏广告onError', err)
  84. })
  85. interstitialAd.onClose(() => {
  86. console.log('视频页插屏广告onClose')
  87. })
  88. },
  89. // 插屏广告展示
  90. interstitalPlayFn() {
  91. if (interstitialAd) {
  92. interstitialAd.show().catch((err) => {
  93. console.error(err)
  94. })
  95. }
  96. },
  97. // 切换标签
  98. switchTab(e) {
  99. const tab = e.currentTarget.dataset.tab
  100. this.setData({
  101. activeTab: tab,
  102. page: 1,
  103. bookList: []
  104. })
  105. this.loadBookList()
  106. },
  107. // 加载书架列表
  108. async loadBookList() {
  109. if (this.data.loading) return
  110. this.setData({
  111. loading: true
  112. })
  113. try {
  114. let result = [];
  115. // 根据当前激活的标签页加载不同的数据
  116. if (this.data.activeTab === 'history') {
  117. // 加载阅读历史记录
  118. result = await getBrowsingHistory();
  119. } else {
  120. // 加载书架列表
  121. result = await getBookshelfList();
  122. }
  123. if (Array.isArray(result)) {
  124. this.setData({
  125. bookList: result,
  126. loading: false,
  127. hasMore: false // 目前接口不支持分页,所以直接设置为false
  128. })
  129. } else {
  130. this.setData({
  131. loading: false,
  132. hasMore: false
  133. })
  134. }
  135. } catch (error) {
  136. console.error('加载数据失败:', error)
  137. this.setData({
  138. loading: false,
  139. hasMore: false
  140. })
  141. wx.showToast({
  142. title: '加载失败,请重试',
  143. icon: 'none'
  144. })
  145. }
  146. },
  147. // 加载更多
  148. loadMore() {
  149. if (this.data.hasMore) {
  150. this.loadBookList()
  151. }
  152. },
  153. // 跳转到书籍详情
  154. goToBookDetail(e) {
  155. const book = e.currentTarget.dataset.book;
  156. console.log(book)
  157. SendEvent('click_shelf_novel', {
  158. "video_id": book.novelId,
  159. "wx_book_id": book.wxBookId,
  160. "chapter_no": book.novelChapterId,
  161. "uuid": app.globalData.uuid
  162. });
  163. // 使用公共的goToBookDetail函数
  164. goToBookDetail({
  165. bookId: book.novelId,
  166. wxBookId: book.wxBookId,
  167. chapterId: book.novelChapterId
  168. });
  169. }
  170. })