bookshelf.js 3.9 KB

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