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

304 lines
8.1 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
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
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
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. export default {
  67. components: { myTabs, lendingListItem },
  68. data() {
  69. return {
  70. screenConfig: {},
  71. tabData: [
  72. { label: '在借中', status: 'lending' },
  73. { label: '历史借阅', status: 'all' },
  74. ],
  75. currentIndex: 0,
  76. // 全部(历史)独立数据
  77. allList: [],
  78. loadingAll: false,
  79. pageAll: 1,
  80. hasMoreAll: true,
  81. loadMoreStatusAll: '',
  82. // 在借中 独立数据
  83. lendingList: [],
  84. loadingLending: false,
  85. swiperHeightData: {},
  86. currentSwiperHeight: 400,
  87. };
  88. },
  89. onLoad() {
  90. this.getScreenSetting();
  91. },
  92. onShow() {
  93. const tabIndex = uni.getStorageSync('switch_tab_index');
  94. if (tabIndex !== '') {
  95. this.currentIndex = Number(tabIndex);
  96. uni.removeStorageSync('switch_tab_index');
  97. }
  98. },
  99. onPullDownRefresh() {
  100. if (this.currentIndex === 0) {
  101. this.getLendingList();
  102. } else {
  103. this.getAllList(true);
  104. }
  105. },
  106. onReachBottom() {
  107. if (this.currentIndex === 0 && this.hasMoreAll) {
  108. this.getLendingList();
  109. }
  110. },
  111. methods: {
  112. // 获取封面(你要的格式)
  113. async getBookListWithCovers(bookList) {
  114. const bookWithCovers = await Promise.all(
  115. bookList.map(async (item) => {
  116. if (!item.isbn) return { ...item, cover: '' };
  117. const isbn = item.isbn.replace(/\-/g, '').trim();
  118. try {
  119. const coverRes = await FetchCoverByISBN({ isbn });
  120. return { ...item, cover: coverRes || '' };
  121. } catch (e) {
  122. console.error(`获取ISBN:${isbn}封面失败`, e);
  123. return { ...item, cover: '' };
  124. }
  125. })
  126. );
  127. return bookWithCovers;
  128. },
  129. async getScreenSetting() {
  130. try {
  131. const res = await FetchInitScreenSetting({ libcode: '1201' });
  132. const data = res.data;
  133. this.screenConfig = {
  134. thirdUrl: data.open_lib_http?.context || '',
  135. thirdAppid: data.open_lib_appId?.context || '',
  136. thirdSecret: data.open_lib_secret?.context || '',
  137. sm4Key: data.sm4_key?.context || ''
  138. };
  139. // 默认加载第一个 tab
  140. this.getLendingList(true);
  141. } catch (err) {}
  142. },
  143. async getAllList(isRefresh = false) {
  144. if (this.loadingAll) return;
  145. this.loadingAll = true;
  146. if (isRefresh) {
  147. this.pageAll = 1;
  148. this.allList = [];
  149. this.hasMoreAll = true;
  150. this.loadMoreStatusAll = '';
  151. }
  152. try {
  153. // 使用带缓存降级的函数获取读者证
  154. const currentReaderCard = await getCurrentReaderCard();
  155. const params = {
  156. ...this.screenConfig,
  157. rdid: currentReaderCard.bindValue,
  158. logtype: '30002',
  159. page: this.pageAll,
  160. rows: 10,
  161. };
  162. const res = await FetchHistoryloan(params);
  163. const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
  164. let list = result.hloanlist || [];
  165. // 插入封面
  166. // list = await this.getBookListWithCovers(list);
  167. this.allList = [...this.allList, ...list];
  168. this.hasMoreAll = list.length === 10;
  169. this.loadMoreStatusAll = this.hasMoreAll ? '' : 'no-more';
  170. if (this.hasMoreAll) this.pageAll++;
  171. } catch (err) {
  172. } finally {
  173. this.loadingAll = false;
  174. uni.stopPullDownRefresh();
  175. setTimeout(() => {
  176. this.calcHeight('all');
  177. }, 100);
  178. }
  179. },
  180. async getLendingList() {
  181. this.loadingLending = true;
  182. try {
  183. // 使用带缓存降级的函数获取读者证
  184. const currentReaderCard = await getCurrentReaderCard();
  185. const params = {
  186. ...this.screenConfig,
  187. rdid: currentReaderCard.bindValue,
  188. };
  189. const res = await FetchRdloanlist(params);
  190. const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
  191. let list = result.loanlist || [];
  192. // 插入封面
  193. // list = await this.getBookListWithCovers(list);
  194. this.lendingList = list.map(item => ({
  195. ...item,
  196. loantime: item.loandate || '',
  197. returntime: item.returndate || '',
  198. }));
  199. } catch (err) {
  200. } finally {
  201. this.loadingLending = false;
  202. uni.stopPullDownRefresh();
  203. setTimeout(() => {
  204. this.calcHeight('lending');
  205. }, 100);
  206. }
  207. },
  208. // 高度自适应(修复版)
  209. calcHeight(type) {
  210. const selector = `.list-item-${type}`;
  211. const query = uni.createSelectorQuery().in(this);
  212. query.selectAll(selector).boundingClientRect((res) => {
  213. let totalHeight = 200;
  214. if (res && res.length > 0) {
  215. totalHeight = res.reduce((sum, rect) => sum + rect.height, 0);
  216. totalHeight += res.length * 10 + 40;
  217. }
  218. this.swiperHeightData[type] = totalHeight;
  219. this.currentSwiperHeight = totalHeight;
  220. }).exec();
  221. },
  222. tabClick(index) {
  223. this.currentIndex = index;
  224. if (index === 0) {
  225. if (this.lendingList.length === 0) {
  226. this.getLendingList();
  227. } else {
  228. this.currentSwiperHeight = this.swiperHeightData.lending || 600;
  229. }
  230. } else {
  231. if (this.allList.length === 0) {
  232. this.getAllList(true);
  233. } else {
  234. this.currentSwiperHeight = this.swiperHeightData.all || 600;
  235. }
  236. }
  237. },
  238. onSwiperChange(e) {
  239. if (e.detail.source === 'touch') this.currentIndex = e.detail.current;
  240. },
  241. onSwiperEnd() {
  242. this.tabClick(this.currentIndex);
  243. },
  244. }
  245. };
  246. </script>
  247. <style lang="scss" scoped>
  248. .lending-container {
  249. background: #f5f5f5;
  250. min-height: 100vh;
  251. }
  252. .tab-sticky {
  253. position: sticky;
  254. top: 0;
  255. z-index: 99;
  256. background: #fff;
  257. }
  258. .swiper {
  259. width: 100%;
  260. transition: height 0.2s ease;
  261. }
  262. .list-wrapper {
  263. padding: 10px;
  264. box-sizing: border-box;
  265. }
  266. </style>