category.js 6.9 KB

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