图书馆小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
5.4 KiB

2 months ago
1 month ago
2 months ago
1 month ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
  1. <template>
  2. <view class="collection-page">
  3. <view class="content-box">
  4. <view class="empty" v-if="bookCollectList.length === 0 && !isLoading">
  5. <uni-icons style="margin-left: 20px;" custom-prefix="iconfont" type="icon-kongshuju" size="80"></uni-icons>
  6. <text style="margin-top: 20px;">暂无收藏的图书~</text>
  7. </view>
  8. <view class="recommendation-list" v-else>
  9. <book-list-item
  10. class="hot-list-item"
  11. v-for="(item, index) in bookCollectList"
  12. :key="index"
  13. :data="item"
  14. :ranking="index + 1"
  15. @click="onItemClick(item)"
  16. ></book-list-item>
  17. </view>
  18. <uni-load-more status="loading" v-if="isLoading && bookCollectList.length > 0"></uni-load-more>
  19. <view class="no-more" v-if="noMore && bookCollectList.length > 0">
  20. 没有更多数据了
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import BookListItem from "@/components/book-list-item/book-list-item.vue";
  27. import { FetchFindAllBookCollectionByOpenId } from '@/api/book';
  28. import { getOpenId } from '@/utils/storage';
  29. import config from '@/utils/config';
  30. export default {
  31. components: {
  32. BookListItem,
  33. },
  34. data() {
  35. return {
  36. isLoading: false,
  37. noMore: false,
  38. pageNum: 0,
  39. pageSize: 10,
  40. bookCollectList: [],
  41. hasLoadedAll: false,
  42. }
  43. },
  44. onLoad() {
  45. // this.loadData();
  46. },
  47. onShow() {
  48. // 检查是否需要刷新(从图书详情取消收藏回来)
  49. const needRefresh = uni.getStorageSync('needRefreshCollect');
  50. uni.removeStorageSync('needRefreshCollect');
  51. // 如果已经加载到最后,或者有取消收藏操作,需要刷新数据
  52. if (this.hasLoadedAll || needRefresh) {
  53. this.refreshData();
  54. } else if (this.bookCollectList.length === 0) {
  55. // 如果还没加载过数据,也需要加载
  56. this.refreshData();
  57. }
  58. },
  59. onPullDownRefresh() {
  60. this.refreshData();
  61. },
  62. onReachBottom() {
  63. if (this.noMore || this.isLoading) return;
  64. this.loadMore();
  65. },
  66. methods: {
  67. async loadData() {
  68. this.isLoading = true;
  69. try {
  70. const openId = await getOpenId();
  71. if (!openId) {
  72. uni.showToast({ title: '获取用户信息失败', icon: 'none' });
  73. this.isLoading = false;
  74. return;
  75. }
  76. const res = await FetchFindAllBookCollectionByOpenId({
  77. libcode: config.LIB_CODE,
  78. openId: openId,
  79. page: this.pageNum,
  80. size: this.pageSize,
  81. });
  82. if (res.code === 200) {
  83. this.bookCollectList = res.data.content || [];
  84. this.noMore = (res.data.content || []).length < this.pageSize;
  85. this.hasLoadedAll = this.noMore;
  86. } else {
  87. uni.showToast({ title: res.message || '获取收藏列表失败', icon: 'none' });
  88. }
  89. } catch (err) {
  90. console.error('获取收藏列表失败', err);
  91. uni.showToast({ title: '获取收藏列表失败', icon: 'none' });
  92. } finally {
  93. this.isLoading = false;
  94. }
  95. },
  96. async refreshData() {
  97. this.pageNum = 0;
  98. this.noMore = false;
  99. this.bookCollectList = [];
  100. await this.loadData();
  101. // 滚动条回到顶部
  102. uni.pageScrollTo({
  103. scrollTop: 0,
  104. duration: 300
  105. });
  106. uni.stopPullDownRefresh();
  107. },
  108. async loadMore() {
  109. if (this.noMore || this.isLoading) return;
  110. this.pageNum++;
  111. this.isLoading = true;
  112. try {
  113. const openId = await getOpenId();
  114. if (!openId) {
  115. uni.showToast({ title: '获取用户信息失败', icon: 'none' });
  116. this.isLoading = false;
  117. return;
  118. }
  119. const res = await FetchFindAllBookCollectionByOpenId({
  120. libcode: config.LIB_CODE,
  121. openId: openId,
  122. page: this.pageNum,
  123. size: this.pageSize,
  124. });
  125. console.log('res',res)
  126. console.log('res.data',res.data)
  127. console.log('res.data.content',res.data.content)
  128. if (res.code === 200) {
  129. const newData = res.data.content || [];
  130. this.bookCollectList = [...this.bookCollectList, ...newData];
  131. this.noMore = newData.length < this.pageSize;
  132. } else {
  133. this.pageNum--;
  134. uni.showToast({ title: res.message || '加载更多失败', icon: 'none' });
  135. }
  136. } catch (err) {
  137. console.error('加载更多收藏失败', err);
  138. this.pageNum--;
  139. uni.showToast({ title: '加载更多失败', icon: 'none' });
  140. } finally {
  141. this.isLoading = false;
  142. }
  143. },
  144. onItemClick(item) {
  145. console.log('item',item)
  146. uni.navigateTo({
  147. url: "/subpkg/pages/book-detail/book-detail?searchData=" + encodeURIComponent(JSON.stringify(item)) + '&isCollected=true'
  148. })
  149. }
  150. }
  151. }
  152. </script>
  153. <style lang="scss" scoped>
  154. .collection-page {
  155. padding: 10px 0;
  156. background-color: #f7f8fa;
  157. min-height: 100vh;
  158. }
  159. .content-box {
  160. padding: 0 10px;
  161. }
  162. .empty {
  163. padding: 60px 0;
  164. text-align: center;
  165. color: #999;
  166. }
  167. .no-more {
  168. text-align: center;
  169. padding: 20px 0;
  170. color: #999;
  171. font-size: 13px;
  172. }
  173. .recommendation-list {
  174. display: flex;
  175. flex-direction: column;
  176. gap: 12px;
  177. }
  178. .book-item {
  179. display: flex;
  180. background: #fff;
  181. border-radius: 8px;
  182. padding: 12px;
  183. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  184. }
  185. .book-cover {
  186. width: 80px;
  187. height: 100px;
  188. border-radius: 4px;
  189. flex-shrink: 0;
  190. background: #f5f5f5;
  191. }
  192. .book-info {
  193. flex: 1;
  194. display: flex;
  195. flex-direction: column;
  196. justify-content: center;
  197. padding-left: 12px;
  198. overflow: hidden;
  199. }
  200. .book-title {
  201. font-size: 15px;
  202. font-weight: 500;
  203. color: #333;
  204. line-height: 1.4;
  205. display: -webkit-box;
  206. -webkit-box-orient: vertical;
  207. -webkit-line-clamp: 2;
  208. overflow: hidden;
  209. }
  210. .book-author {
  211. font-size: 13px;
  212. color: #999;
  213. margin-top: 6px;
  214. }
  215. .book-publisher {
  216. font-size: 12px;
  217. color: #ccc;
  218. margin-top: 4px;
  219. }
  220. </style>