category.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { getCategoryList, getNovelSearch } 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. gender: "male", // 当前性别选择
  9. currentCategory: "", // 当前选中的分类
  10. categories: [],
  11. bookList: [],
  12. loading: false,
  13. keyword: "",
  14. page: 1,
  15. size: 10,
  16. hasMore: true, // 是否还有更多数据,
  17. isLoggedIn: false, //是否登录
  18. },
  19. onLoad: function (options) {
  20. this.autoLogin();
  21. },
  22. onShow: function () {
  23. if (this.data.isLoggedIn) {
  24. this.syncGenderState();
  25. }
  26. },
  27. onTabItemTap: function (item) {
  28. console.log(item.index);
  29. setTimeout(() => {
  30. this.interstitalPlayFn();
  31. }, 1000);
  32. if (this.data.isLoggedIn) {
  33. this.syncGenderState();
  34. }
  35. },
  36. // 自动登录
  37. async autoLogin() {
  38. try {
  39. // 检查是否已经登录
  40. if (app.globalData.accessToken) {
  41. this.setData({
  42. isLoggedIn: true,
  43. });
  44. // 已经登录
  45. console.log("已经登录");
  46. // 页面首次加载时的处理
  47. this.syncGenderState();
  48. this.interstitalLoad();
  49. return;
  50. }
  51. // 未登录,执行登录
  52. getApp()
  53. .login()
  54. .then((res) => {
  55. this.setData({
  56. isLoggedIn: true,
  57. });
  58. this.syncGenderState();
  59. this.interstitalLoad();
  60. console.log("重新登录", res);
  61. });
  62. } catch (error) {
  63. console.error("登录失败:", error);
  64. }
  65. },
  66. // 插屏广告加载
  67. interstitalLoad() {
  68. let adId = apple.ads.insertAdId;
  69. interstitialAd = wx.createInterstitialAd({
  70. adUnitId: adId,
  71. });
  72. interstitialAd.onLoad(() => {
  73. console.log("分类页插屏广告onLoad");
  74. });
  75. interstitialAd.onError((err) => {
  76. console.log("分类页插屏广告onError", err);
  77. });
  78. interstitialAd.onClose(() => {
  79. console.log("分类页插屏广告onClose");
  80. });
  81. },
  82. // 插屏广告展示
  83. interstitalPlayFn() {
  84. if (interstitialAd) {
  85. interstitialAd.show().catch((err) => {
  86. console.error("分类页插屏显示错误", err);
  87. });
  88. }
  89. },
  90. // 同步性别状态
  91. syncGenderState: async function () {
  92. const gender = wx.getStorageSync("gender") || "male";
  93. if (gender !== this.data.gender) {
  94. this.setData({
  95. gender,
  96. });
  97. await this.loadCategories();
  98. } else {
  99. await this.loadCategories();
  100. }
  101. },
  102. // 切换性别
  103. switchGender: async function (e) {
  104. const { gender } = e.detail;
  105. this.setData({
  106. gender: gender,
  107. currentCategory: "",
  108. bookList: [],
  109. page: 1,
  110. hasMore: true,
  111. });
  112. SendEvent("click_classify_type", {
  113. gender: gender,
  114. uuid: app.globalData.uuid,
  115. });
  116. if (this.data.isLoggedIn) {
  117. await this.loadCategories();
  118. }
  119. },
  120. // 加载分类列表
  121. async loadCategories() {
  122. try {
  123. const channel = this.data.gender === "male" ? "man" : "woman";
  124. const result = await getCategoryList(channel);
  125. if (result && Array.isArray(result)) {
  126. this.setData({
  127. categories: result,
  128. currentCategory: result[0]?.code || "",
  129. page: 1,
  130. hasMore: true,
  131. });
  132. // 加载第一个分类的书籍
  133. if (result[0]?.code) {
  134. this.loadBookList(result[0].code);
  135. }
  136. }
  137. } catch (error) {
  138. console.error("加载分类失败:", error);
  139. wx.showToast({
  140. title: "加载分类失败",
  141. icon: "none",
  142. });
  143. }
  144. },
  145. // 切换分类
  146. switchCategory: function (e) {
  147. const category = e.currentTarget.dataset.category;
  148. this.setData({
  149. currentCategory: category,
  150. bookList: [],
  151. page: 1,
  152. hasMore: true,
  153. });
  154. SendEvent("click_classify_name", {
  155. category: category,
  156. uuid: app.globalData.uuid,
  157. });
  158. this.loadBookList(category);
  159. },
  160. // 加载书籍列表
  161. async loadBookList(category) {
  162. if (this.data.loading) return; // 只检查loading状态,移除hasMore检查
  163. this.setData({
  164. loading: true,
  165. });
  166. try {
  167. const params = {
  168. keyword: this.data.keyword,
  169. page: this.data.page,
  170. size: this.data.size,
  171. category: category,
  172. };
  173. const result = await getNovelSearch(params);
  174. if (result && result.records) {
  175. const newList =
  176. this.data.page === 1
  177. ? result.records
  178. : [...this.data.bookList, ...result.records];
  179. const hasMore = !result.last;
  180. this.setData({
  181. bookList: newList,
  182. loading: false,
  183. hasMore: hasMore,
  184. page: hasMore ? this.data.page + 1 : this.data.page,
  185. });
  186. } else {
  187. this.setData({
  188. loading: false,
  189. hasMore: false,
  190. });
  191. }
  192. } catch (error) {
  193. console.error("加载书籍列表失败:", error);
  194. this.setData({
  195. loading: false,
  196. hasMore: false,
  197. });
  198. wx.showToast({
  199. title: "加载失败,请重试",
  200. icon: "none",
  201. });
  202. }
  203. },
  204. // 监听滚动到底部
  205. onScrollToLower: function () {
  206. if (this.data.currentCategory && this.data.hasMore) {
  207. this.loadBookList(this.data.currentCategory);
  208. }
  209. },
  210. // 跳转到书籍详情
  211. handleBookTap: function (e) {
  212. const { id, wxbookid } = e.currentTarget.dataset;
  213. SendEvent("click_classfy_novel", {
  214. novel_id: id,
  215. wx_book_id: wxbookid,
  216. uuid: app.globalData.uuid,
  217. });
  218. goToBookDetail({
  219. bookId: id,
  220. wxBookId: wxbookid,
  221. });
  222. },
  223. // 跳转到搜索页面
  224. goToSearch: function (e) {
  225. // 如果有输入内容,则带上关键词
  226. const keyword = e.detail?.value || "";
  227. wx.navigateTo({
  228. url: `/pages/search/search?keyword=${encodeURIComponent(keyword)}`,
  229. });
  230. },
  231. });