category.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { getCategoryList, getNovelSearch } from '../../api/api'
  2. import { goToBookDetail } from '../../utils/util'
  3. Page({
  4. data: {
  5. gender: 'male', // 当前性别选择
  6. currentCategory: '', // 当前选中的分类
  7. categories: [],
  8. bookList: [],
  9. loading: false,
  10. keyword: '',
  11. page: 1,
  12. size: 10,
  13. hasMore: true // 是否还有更多数据
  14. },
  15. onLoad: function(options) {
  16. // 页面首次加载时的处理
  17. this.syncGenderState();
  18. },
  19. onShow: function() {
  20. // 每次页面显示时同步性别状态
  21. this.syncGenderState();
  22. },
  23. onTabItemTap: function() {
  24. // tabBar 点击时同步性别状态
  25. this.syncGenderState();
  26. },
  27. // 同步性别状态
  28. syncGenderState: async function() {
  29. const gender = wx.getStorageSync('gender') || 'male';
  30. if (gender !== this.data.gender) {
  31. this.setData({ gender });
  32. await this.loadCategories();
  33. }
  34. },
  35. // 切换性别
  36. switchGender: async function(e) {
  37. const { gender } = e.detail;
  38. this.setData({
  39. gender: gender,
  40. currentCategory: '',
  41. bookList: [],
  42. page: 1,
  43. hasMore: true
  44. });
  45. await this.loadCategories();
  46. },
  47. // 加载分类列表
  48. async loadCategories() {
  49. try {
  50. const channel = this.data.gender === 'male' ? "man" : "woman";
  51. const result = await getCategoryList(channel);
  52. if (result && Array.isArray(result)) {
  53. this.setData({
  54. categories: result,
  55. currentCategory: result[0]?.code || '',
  56. page: 1,
  57. hasMore: true
  58. });
  59. // 加载第一个分类的书籍
  60. if (result[0]?.code) {
  61. this.loadBookList(result[0].code);
  62. }
  63. }
  64. } catch (error) {
  65. console.error('加载分类失败:', error);
  66. wx.showToast({
  67. title: '加载分类失败',
  68. icon: 'none'
  69. });
  70. }
  71. },
  72. // 切换分类
  73. switchCategory: function(e) {
  74. const category = e.currentTarget.dataset.category;
  75. this.setData({
  76. currentCategory: category,
  77. bookList: [],
  78. page: 1,
  79. hasMore: true
  80. });
  81. this.loadBookList(category);
  82. },
  83. // 加载书籍列表
  84. async loadBookList(category) {
  85. if (this.data.loading) return; // 只检查loading状态,移除hasMore检查
  86. this.setData({ loading: true });
  87. try {
  88. const params = {
  89. keyword: this.data.keyword,
  90. page: this.data.page,
  91. size: this.data.size,
  92. category: category
  93. };
  94. const result = await getNovelSearch(params);
  95. if (result && result.records) {
  96. const newList = this.data.page === 1 ? result.records : [...this.data.bookList, ...result.records];
  97. const hasMore = !result.last;
  98. this.setData({
  99. bookList: newList,
  100. loading: false,
  101. hasMore: hasMore,
  102. page: hasMore ? this.data.page + 1 : this.data.page
  103. });
  104. } else {
  105. this.setData({
  106. loading: false,
  107. hasMore: false
  108. });
  109. }
  110. } catch (error) {
  111. console.error('加载书籍列表失败:', error);
  112. this.setData({
  113. loading: false,
  114. hasMore: false
  115. });
  116. wx.showToast({
  117. title: '加载失败,请重试',
  118. icon: 'none'
  119. });
  120. }
  121. },
  122. // 监听滚动到底部
  123. onScrollToLower: function() {
  124. if (this.data.currentCategory && this.data.hasMore) {
  125. this.loadBookList(this.data.currentCategory);
  126. }
  127. },
  128. // 跳转到书籍详情
  129. handleBookTap: function(e) {
  130. const { id, wxbookid } = e.currentTarget.dataset;
  131. goToBookDetail({
  132. bookId: id,
  133. wxBookId: wxbookid
  134. });
  135. },
  136. // 跳转到搜索页面
  137. goToSearch: function(e) {
  138. // 如果有输入内容,则带上关键词
  139. const keyword = e.detail?.value || '';
  140. wx.navigateTo({
  141. url: `/pages/search/search?keyword=${encodeURIComponent(keyword)}`
  142. });
  143. }
  144. })