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

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