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

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