图书馆小程序
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.

298 lines
7.9 KiB

2 months ago
1 month ago
2 months ago
1 month ago
1 month ago
2 months ago
1 month ago
1 month ago
1 month ago
2 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month 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
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
1 month ago
1 month ago
2 months ago
1 month ago
1 month ago
1 month ago
1 month ago
2 months ago
1 month ago
1 month 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
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
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
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
1 month ago
2 months ago
  1. <template>
  2. <view class="lending-container">
  3. <!-- 固定只有两个 tab -->
  4. <view class="tab-sticky">
  5. <my-tabs
  6. :tabData="tabData"
  7. :defaultIndex="currentIndex"
  8. :config="{ textColor: '#333' }"
  9. @tabClick="tabClick"
  10. />
  11. </view>
  12. <swiper
  13. class="swiper"
  14. :current="currentIndex"
  15. :style="{ height: currentSwiperHeight + 'px' }"
  16. @animationfinish="onSwiperEnd"
  17. @change="onSwiperChange"
  18. >
  19. <!-- 在借中 -->
  20. <swiper-item>
  21. <view class="list-wrapper">
  22. <uni-load-more status="loading" v-if="loadingLending" />
  23. <view class="empty" v-else-if="lendingList.length === 0">
  24. <uni-icons style="margin-left: 20px;" custom-prefix="iconfont" type="icon-kongshuju" size="80"></uni-icons>
  25. <text style="margin-top: 20px;">暂无在借记录</text>
  26. </view>
  27. <lending-list-item
  28. class="list-item-lending"
  29. v-for="(item, index) in lendingList"
  30. :key="index"
  31. :data="item"
  32. :is-lending="true"
  33. />
  34. </view>
  35. </swiper-item>
  36. <!-- 全部历史借阅 -->
  37. <swiper-item>
  38. <view class="list-wrapper">
  39. <uni-load-more status="loading" v-if="loadingAll" />
  40. <view class="empty" v-else-if="allList.length === 0">
  41. <uni-icons style="margin-left: 20px;" custom-prefix="iconfont" type="icon-kongshuju" size="80"></uni-icons>
  42. <text style="margin-top: 20px;">暂无历史借阅记录</text>
  43. </view>
  44. <lending-list-item
  45. class="list-item-all"
  46. v-for="(item, index) in allList"
  47. :key="index"
  48. :data="item"
  49. :is-lending="false"
  50. />
  51. <uni-load-more
  52. v-if="allList.length > 0 && hasMoreAll"
  53. :status="loadMoreStatusAll"
  54. />
  55. </view>
  56. </swiper-item>
  57. </swiper>
  58. </view>
  59. </template>
  60. <script>
  61. import { FetchInitScreenSetting } from '@/api/user';
  62. import { FetchHistoryloan, FetchRdloanlist, FetchCoverByISBN } from '@/api/book';
  63. import myTabs from "@/components/my-tabs/my-tabs.vue";
  64. import lendingListItem from "@/components/lending-list-item/lending-list-item.vue";
  65. import { getCurrentReaderCard } from '@/utils/storage';
  66. import config from '@/utils/config';
  67. import { loadBookCoversBase64 } from '@/utils/bookCover';
  68. export default {
  69. components: { myTabs, lendingListItem },
  70. data() {
  71. return {
  72. screenConfig: {},
  73. tabData: [
  74. { label: '在借中', status: 'lending' },
  75. { label: '历史借阅', status: 'all' },
  76. ],
  77. currentIndex: 0,
  78. // 全部(历史)独立数据
  79. allList: [],
  80. loadingAll: false,
  81. pageAll: 1,
  82. hasMoreAll: true,
  83. loadMoreStatusAll: '',
  84. // 在借中 独立数据
  85. lendingList: [],
  86. loadingLending: false,
  87. swiperHeightData: {},
  88. currentSwiperHeight: 400,
  89. };
  90. },
  91. onLoad() {
  92. this.getScreenSetting();
  93. },
  94. onShow() {
  95. const tabIndex = uni.getStorageSync('switch_tab_index');
  96. if (tabIndex !== '') {
  97. this.currentIndex = Number(tabIndex);
  98. uni.removeStorageSync('switch_tab_index');
  99. }
  100. },
  101. onPullDownRefresh() {
  102. if (this.currentIndex === 0) {
  103. this.getLendingList();
  104. } else {
  105. this.getAllList(true);
  106. }
  107. },
  108. onReachBottom() {
  109. if (this.currentIndex === 0) {
  110. this.getLendingList();
  111. } else if (this.currentIndex === 1 && this.hasMoreAll) {
  112. this.getAllList();
  113. }
  114. },
  115. methods: {
  116. async getScreenSetting() {
  117. try {
  118. const res = await FetchInitScreenSetting({ libcode: config.LIB_CODE });
  119. const data = res.data;
  120. this.screenConfig = {
  121. thirdUrl: data.open_lib_http?.context || '',
  122. thirdAppid: data.open_lib_appId?.context || '',
  123. thirdSecret: data.open_lib_secret?.context || '',
  124. sm4Key: data.sm4_key?.context || ''
  125. };
  126. // 默认加载第一个 tab
  127. this.getLendingList(true);
  128. } catch (err) {}
  129. },
  130. async getAllList(isRefresh = false) {
  131. if (this.loadingAll) return;
  132. this.loadingAll = true;
  133. if (isRefresh) {
  134. this.pageAll = 1;
  135. this.allList = [];
  136. this.hasMoreAll = true;
  137. this.loadMoreStatusAll = '';
  138. }
  139. try {
  140. // 使用带缓存降级的函数获取读者证
  141. const currentReaderCard = await getCurrentReaderCard();
  142. const params = {
  143. ...this.screenConfig,
  144. rdid: currentReaderCard.bindValue,
  145. logtype: '30002',
  146. page: this.pageAll,
  147. rows: 10,
  148. };
  149. const res = await FetchHistoryloan(params);
  150. const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
  151. let list = result.hloanlist || [];
  152. this.allList = [...this.allList, ...list];
  153. // 加载封面
  154. await this.loadCoversForList(this.allList);
  155. this.hasMoreAll = list.length === 10;
  156. this.loadMoreStatusAll = this.hasMoreAll ? '' : 'no-more';
  157. if (this.hasMoreAll) this.pageAll++;
  158. } catch (err) {
  159. } finally {
  160. this.loadingAll = false;
  161. uni.stopPullDownRefresh();
  162. setTimeout(() => {
  163. this.calcHeight('all');
  164. }, 100);
  165. }
  166. },
  167. async getLendingList() {
  168. this.loadingLending = true;
  169. try {
  170. // 使用带缓存降级的函数获取读者证
  171. const currentReaderCard = await getCurrentReaderCard();
  172. const params = {
  173. ...this.screenConfig,
  174. rdid: currentReaderCard.bindValue,
  175. };
  176. const res = await FetchRdloanlist(params);
  177. const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
  178. let list = result.loanlist || [];
  179. this.lendingList = list.map(item => ({
  180. ...item,
  181. loantime: item.loandate || '',
  182. returntime: item.returndate || '',
  183. }));
  184. // 加载封面
  185. await this.loadCoversForList(this.lendingList);
  186. } catch (err) {
  187. } finally {
  188. this.loadingLending = false;
  189. uni.stopPullDownRefresh();
  190. setTimeout(() => {
  191. this.calcHeight('lending');
  192. }, 100);
  193. }
  194. },
  195. // 加载封面
  196. async loadCoversForList(list) {
  197. await loadBookCoversBase64(list, (index, coverUrl) => {
  198. if (list[index]) {
  199. this.$set(list[index], 'cover', coverUrl);
  200. }
  201. });
  202. },
  203. // 高度自适应(修复版)
  204. calcHeight(type) {
  205. const selector = `.list-item-${type}`;
  206. const query = uni.createSelectorQuery().in(this);
  207. query.selectAll(selector).boundingClientRect((res) => {
  208. let totalHeight = 200;
  209. if (res && res.length > 0) {
  210. totalHeight = res.reduce((sum, rect) => sum + rect.height, 0);
  211. totalHeight += res.length * 10 + 40;
  212. }
  213. this.swiperHeightData[type] = totalHeight;
  214. this.currentSwiperHeight = totalHeight + 20;
  215. }).exec();
  216. },
  217. tabClick(index) {
  218. this.currentIndex = index;
  219. if (index === 0) {
  220. if (this.lendingList.length === 0) {
  221. this.getLendingList();
  222. } else {
  223. this.currentSwiperHeight = this.swiperHeightData.lending || 600;
  224. }
  225. } else {
  226. if (this.allList.length === 0) {
  227. this.getAllList(true);
  228. } else {
  229. this.currentSwiperHeight = this.swiperHeightData.all || 600;
  230. }
  231. }
  232. },
  233. onSwiperChange(e) {
  234. if (e.detail.source === 'touch') this.currentIndex = e.detail.current;
  235. },
  236. onSwiperEnd() {
  237. this.tabClick(this.currentIndex);
  238. },
  239. }
  240. };
  241. </script>
  242. <style lang="scss" scoped>
  243. .lending-container {
  244. background: #f5f5f5;
  245. min-height: 100vh;
  246. }
  247. .tab-sticky {
  248. position: sticky;
  249. top: 0;
  250. z-index: 99;
  251. background: #fff;
  252. }
  253. .swiper {
  254. width: 100%;
  255. transition: height 0.2s ease;
  256. }
  257. .list-wrapper {
  258. padding: 10px;
  259. box-sizing: border-box;
  260. }
  261. </style>