Browse Source

获取手机号/封面图base64

master
xuhuajiao 1 month ago
parent
commit
5606f63d8f
  1. 15
      api/user.js
  2. 22
      components/book-list-item/book-list-item.vue
  3. 20
      pages/lendCar/lendCar.vue
  4. 74
      pages/register/register.vue
  5. 14
      pages/search/search.vue
  6. 21
      subpkg/pages/book-detail/book-detail.vue
  7. 25
      subpkg/pages/collect-list/collect-list.vue
  8. 41
      subpkg/pages/myLending/myLending.vue
  9. 12
      subpkg/pages/ranking/ranking.vue
  10. 61
      utils/bookCover.js

15
api/user.js

@ -139,3 +139,18 @@ export function FetchReaderMessage(data) {
data data
}); });
} }
export function FetchSessionKey(data) {
return request({
url: '/api/weixin/getSessionKey',
data
});
}
export function FetchDecryptPhone(data) {
return request({
url: '/api/weixin/decryptPhone',
method: 'POST',
data
});
}

22
components/book-list-item/book-list-item.vue

@ -29,22 +29,17 @@ export default {
}, },
computed: { computed: {
actualCover() { actualCover() {
// base64Cover > cover > imgCover
// 使 cover
if (this.data.cover) {
return this.data.cover;
}
// base64Cover base64 使
// 使 base64Cover
if (this.data.base64Cover) { if (this.data.base64Cover) {
return this.data.base64Cover; return this.data.base64Cover;
} }
// cover imgCover
const cover = this.data.cover || this.data.imgCover;
//
if (cover && this.isValidCoverLink(cover)) {
return cover;
}
//
// 使
return '/static/images/default-book.png'; return '/static/images/default-book.png';
} }
}, },
@ -59,6 +54,11 @@ export default {
return false; return false;
} }
// base64
if (coverlink.indexOf('data:image/') === 0) {
return true;
}
// http // http
if (coverlink.indexOf('http') === -1) { if (coverlink.indexOf('http') === -1) {
return false; return false;

20
pages/lendCar/lendCar.vue

@ -65,6 +65,7 @@ import { FetchInitScreenSetting } from '@/api/user';
import { FetchRdloanlist, FetchRenewbook } from '@/api/book'; import { FetchRdloanlist, FetchRenewbook } from '@/api/book';
import { getCurrentReaderCard } from '@/utils/storage'; import { getCurrentReaderCard } from '@/utils/storage';
import config from '@/utils/config'; import config from '@/utils/config';
import { loadBookCoversBase64 } from '@/utils/bookCover';
export default { export default {
data() { data() {
@ -190,13 +191,15 @@ export default {
const res = await FetchRdloanlist(params); const res = await FetchRdloanlist(params);
const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data; const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
//
//
this.bookList = (result.loanlist || []).map(item => ({ this.bookList = (result.loanlist || []).map(item => ({
...item, ...item,
checked: false,
cover: this.getBookCover(item)
checked: false
})); }));
//
await this.loadCoversForList();
} catch (err) { } catch (err) {
console.error('获取借阅列表失败:', err); console.error('获取借阅列表失败:', err);
uni.showToast({ title: '加载失败', icon: 'none' }); uni.showToast({ title: '加载失败', icon: 'none' });
@ -221,6 +224,17 @@ export default {
return null; return null;
}, },
/**
* 加载封面
*/
async loadCoversForList() {
await loadBookCoversBase64(this.bookList, (index, coverUrl) => {
if (this.bookList[index]) {
this.$set(this.bookList[index], 'cover', coverUrl);
}
});
},
/** /**
* 判断是否逾期 * 判断是否逾期
*/ */

74
pages/register/register.vue

@ -106,10 +106,13 @@
<script> <script>
import config from '@/utils/config'; import config from '@/utils/config';
import { getOpenId } from '@/utils/storage'; import { getOpenId } from '@/utils/storage';
import { FetchSessionKey, FetchDecryptPhone } from '@/api/user.js';
export default { export default {
data() { data() {
return { return {
sessionKey: '',
openid: '',
formData: { formData: {
name: '', name: '',
idCard: '', idCard: '',
@ -123,43 +126,59 @@ export default {
}; };
}, },
onLoad() {},
onLoad() {
this.getSessionKey();
},
methods: { methods: {
async getSessionKey() {
try {
uni.login({
success: async (loginRes) => {
console.log('loginRes',loginRes)
if (loginRes.code) {
const res = await FetchSessionKey({
libcode: config.LIB_CODE,
code: loginRes.code
});
if (res.code === 200) {
this.sessionKey = res.data.session_key;
this.openid = res.data.openid;
}
}
}
});
} catch (error) {
console.error('获取sessionKey失败:', error);
}
},
getPhoneNumber(e) { getPhoneNumber(e) {
if (e.detail.errMsg !== 'getPhoneNumber:ok') { if (e.detail.errMsg !== 'getPhoneNumber:ok') {
uni.showToast({ title: '取消授权', icon: 'none' }); uni.showToast({ title: '取消授权', icon: 'none' });
return; return;
} }
// #ifdef MP-WEIXIN
uni.login({
success: (loginRes) => {
uni.request({
url: config.baseUrl + '/api/wx/decryptPhone',
method: 'POST',
data: {
code: loginRes.code,
if (!this.sessionKey) {
uni.showToast({ title: '请稍后再试', icon: 'none' });
return;
}
FetchDecryptPhone({
sessionKey: this.sessionKey,
encryptedData: e.detail.encryptedData, encryptedData: e.detail.encryptedData,
iv: e.detail.iv iv: e.detail.iv
},
success: (res) => {
if (res.data?.code === 200) {
this.formData.phone = res.data.data.phoneNumber || '';
}).then(res => {
if (res.code === 200) {
this.formData.phone = res.data.phoneNumber || '';
uni.showToast({ title: '获取成功', icon: 'success' });
} else { } else {
uni.showToast({ title: res.data?.message || '获取失败', icon: 'none' });
uni.showToast({ title: res.message || '获取失败', icon: 'none' });
} }
},
fail: () => uni.showToast({ title: '获取手机号失败', icon: 'none' })
}).catch(() => {
uni.showToast({ title: '获取手机号失败', icon: 'none' });
}); });
}, },
fail: () => uni.showToast({ title: '登录失败', icon: 'none' })
});
// #endif
// #ifndef MP-WEIXIN
uni.showToast({ title: '仅支持微信小程序', icon: 'none' });
// #endif
},
togglePwd() { togglePwd() {
this.showPwd = !this.showPwd; this.showPwd = !this.showPwd;
}, },
@ -170,26 +189,20 @@ export default {
// 1518 // 1518
validateIdCard(idCard) { validateIdCard(idCard) {
if (!idCard) return false; if (!idCard) return false;
// //
idCard = idCard.replace(/[\s-]/g, ''); idCard = idCard.replace(/[\s-]/g, '');
// 15 // 15
const idCard15Reg = /^[1-9]\d{5}\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}$/; const idCard15Reg = /^[1-9]\d{5}\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}$/;
// 18 // 18
const idCard18Reg = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/; const idCard18Reg = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/;
// 15 // 15
if (idCard.length === 15) { if (idCard.length === 15) {
return idCard15Reg.test(idCard); return idCard15Reg.test(idCard);
} }
// 18 // 18
if (idCard.length !== 18 || !idCard18Reg.test(idCard)) { if (idCard.length !== 18 || !idCard18Reg.test(idCard)) {
return false; return false;
} }
// //
const year = +idCard.substring(6, 10); const year = +idCard.substring(6, 10);
const month = +idCard.substring(10, 12); const month = +idCard.substring(10, 12);
@ -197,7 +210,6 @@ export default {
const date = new Date(year, month - 1, day); const date = new Date(year, month - 1, day);
return date.getFullYear() === year && date.getMonth() === month - 1 && date.getDate() === day; return date.getFullYear() === year && date.getMonth() === month - 1 && date.getDate() === day;
}, },
// //
validatePhone(phone) { validatePhone(phone) {
return /^1[3-9]\d{9}$/.test(phone); return /^1[3-9]\d{9}$/.test(phone);

14
pages/search/search.vue

@ -81,6 +81,7 @@ import { FetchBookSearch, FetchFindAllBookCollectionByOpenId } from '@/api/book'
import BookListItem from "@/components/book-list-item/book-list-item.vue"; import BookListItem from "@/components/book-list-item/book-list-item.vue";
import config from '@/utils/config'; import config from '@/utils/config';
import { getOpenId } from '@/utils/storage'; import { getOpenId } from '@/utils/storage';
import { loadBookCoversBase64 } from '@/utils/bookCover';
export default { export default {
components: { BookListItem }, components: { BookListItem },
@ -195,7 +196,9 @@ export default {
} }
this.isSearched = true; this.isSearched = true;
console.log('this.listData',this.listData)
//
this.loadCoversForList();
} catch (err) { } catch (err) {
console.error('搜索接口异常', err); console.error('搜索接口异常', err);
this.listData = []; this.listData = [];
@ -218,6 +221,15 @@ export default {
this.getBookList(); this.getBookList();
}, },
//
async loadCoversForList() {
await loadBookCoversBase64(this.listData, (index, coverUrl) => {
if (this.listData[index]) {
this.$set(this.listData[index], 'cover', coverUrl);
}
});
},
// //
async onItemClick(item) { async onItemClick(item) {
console.log('item', item); console.log('item', item);

21
subpkg/pages/book-detail/book-detail.vue

@ -99,6 +99,7 @@ import { FetchInitScreenSetting } from '@/api/user';
import { FetchFindbookByQuery, FetchDictionaryTree,FetchCollectionBook,FetchCancelCollectionBook } from '@/api/book'; import { FetchFindbookByQuery, FetchDictionaryTree,FetchCollectionBook,FetchCancelCollectionBook } from '@/api/book';
import { getOpenId } from '@/utils/storage'; import { getOpenId } from '@/utils/storage';
import config from '@/utils/config'; import config from '@/utils/config';
import { fetchBookCoverBase64 } from '@/utils/bookCover';
export default { export default {
data() { data() {
@ -134,6 +135,8 @@ export default {
const bookData = JSON.parse(decodeURIComponent(options.searchData)); const bookData = JSON.parse(decodeURIComponent(options.searchData));
this.searchListInfo = bookData; this.searchListInfo = bookData;
this.bookrecno = bookData.bookrecno || ""; this.bookrecno = bookData.bookrecno || "";
this.bookInfo = bookData;
} }
// 3. // 3.
@ -165,6 +168,20 @@ export default {
onImgError(e) { onImgError(e) {
e.target.src = "/static/images/default-book.png"; e.target.src = "/static/images/default-book.png";
}, },
//
async loadBookCover() {
console.log('this.bookInfo',this.bookInfo)
if (!this.bookInfo.isbn) return;
try {
const coverUrl = await fetchBookCoverBase64(this.bookInfo.isbn);
if (coverUrl) {
this.$set(this.bookInfo, 'cover', coverUrl);
}
} catch (error) {
console.error('获取封面失败:', error);
}
},
// - // -
async getDictionaryTree() { async getDictionaryTree() {
try { try {
@ -225,6 +242,10 @@ export default {
this.holdingsData = apiData.holdings || []; this.holdingsData = apiData.holdings || [];
// console.log('bookrecno-holdingsData',this.holdingsData); // console.log('bookrecno-holdingsData',this.holdingsData);
//
await this.loadBookCover();
//
this.checkCollectStatus(); this.checkCollectStatus();
} catch (err) { } catch (err) {
console.error(err); console.error(err);

25
subpkg/pages/collect-list/collect-list.vue

@ -28,7 +28,7 @@ import BookListItem from "@/components/book-list-item/book-list-item.vue";
import { FetchFindAllBookCollectionByOpenId } from '@/api/book'; import { FetchFindAllBookCollectionByOpenId } from '@/api/book';
import { getOpenId } from '@/utils/storage'; import { getOpenId } from '@/utils/storage';
import config from '@/utils/config'; import config from '@/utils/config';
import { loadBookCovers } from '@/utils/bookCover';
import { loadBookCoversBase64 } from '@/utils/bookCover';
export default { export default {
components: { components: {
@ -46,7 +46,6 @@ export default {
}, },
onLoad() { onLoad() {
// this.loadData();
}, },
onShow() { onShow() {
// //
@ -69,19 +68,6 @@ export default {
this.loadMore(); this.loadMore();
}, },
methods: { methods: {
// getTest(){
// // 34352,36153
// wx.request({
// url: `http://218.200.95.251:8081/opac/getExistsBookrecnoList/41534`,
// method: 'POST',
// success: (res) => {
// console.log(res.data || []);
// },
// fail: () => {
// resolve([]);
// }
// });
// },
async loadData() { async loadData() {
this.isLoading = true; this.isLoading = true;
try { try {
@ -150,9 +136,6 @@ export default {
page: this.pageNum, page: this.pageNum,
size: this.pageSize, size: this.pageSize,
}); });
console.log('res',res)
console.log('res.data',res.data)
console.log('res.data.content',res.data.content)
if (res.code === 200) { if (res.code === 200) {
const newData = res.data.content || []; const newData = res.data.content || [];
@ -176,17 +159,13 @@ export default {
} }
}, },
async loadCoversForList() { async loadCoversForList() {
await loadBookCovers(this.bookCollectList, (index, coverUrl) => {
console.log('index', index);
console.log('coverUrl', coverUrl);
await loadBookCoversBase64(this.bookCollectList, (index, coverUrl) => {
if (this.bookCollectList[index]) { if (this.bookCollectList[index]) {
// 使 Vue.set
this.$set(this.bookCollectList[index], 'cover', coverUrl); this.$set(this.bookCollectList[index], 'cover', coverUrl);
} }
}); });
}, },
onItemClick(item) { onItemClick(item) {
console.log('item',item)
uni.navigateTo({ uni.navigateTo({
url: "/subpkg/pages/book-detail/book-detail?searchData=" + encodeURIComponent(JSON.stringify(item)) + '&isCollected=true' url: "/subpkg/pages/book-detail/book-detail?searchData=" + encodeURIComponent(JSON.stringify(item)) + '&isCollected=true'
}) })

41
subpkg/pages/myLending/myLending.vue

@ -72,6 +72,7 @@ import myTabs from "@/components/my-tabs/my-tabs.vue";
import lendingListItem from "@/components/lending-list-item/lending-list-item.vue"; import lendingListItem from "@/components/lending-list-item/lending-list-item.vue";
import { getCurrentReaderCard } from '@/utils/storage'; import { getCurrentReaderCard } from '@/utils/storage';
import config from '@/utils/config'; import config from '@/utils/config';
import { loadBookCoversBase64 } from '@/utils/bookCover';
export default { export default {
components: { myTabs, lendingListItem }, components: { myTabs, lendingListItem },
@ -129,25 +130,6 @@ export default {
}, },
methods: { methods: {
//
async getBookListWithCovers(bookList) {
const bookWithCovers = await Promise.all(
bookList.map(async (item) => {
if (!item.isbn) return { ...item, cover: '' };
const isbn = item.isbn.replace(/\-/g, '').trim();
try {
const coverRes = await FetchCoverByISBN({ isbn });
return { ...item, cover: coverRes || '' };
} catch (e) {
console.error(`获取ISBN:${isbn}封面失败`, e);
return { ...item, cover: '' };
}
})
);
return bookWithCovers;
},
async getScreenSetting() { async getScreenSetting() {
try { try {
const res = await FetchInitScreenSetting({ libcode: config.LIB_CODE }); const res = await FetchInitScreenSetting({ libcode: config.LIB_CODE });
@ -191,10 +173,10 @@ export default {
const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data; const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
let list = result.hloanlist || []; let list = result.hloanlist || [];
//
// list = await this.getBookListWithCovers(list);
this.allList = [...this.allList, ...list]; this.allList = [...this.allList, ...list];
//
await this.loadCoversForList(this.allList);
this.hasMoreAll = list.length === 10; this.hasMoreAll = list.length === 10;
this.loadMoreStatusAll = this.hasMoreAll ? '' : 'no-more'; this.loadMoreStatusAll = this.hasMoreAll ? '' : 'no-more';
@ -223,14 +205,14 @@ export default {
const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data; const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
let list = result.loanlist || []; let list = result.loanlist || [];
//
// list = await this.getBookListWithCovers(list);
this.lendingList = list.map(item => ({ this.lendingList = list.map(item => ({
...item, ...item,
loantime: item.loandate || '', loantime: item.loandate || '',
returntime: item.returndate || '', returntime: item.returndate || '',
})); }));
//
await this.loadCoversForList(this.lendingList);
} catch (err) { } catch (err) {
} finally { } finally {
this.loadingLending = false; this.loadingLending = false;
@ -241,6 +223,15 @@ export default {
} }
}, },
//
async loadCoversForList(list) {
await loadBookCoversBase64(list, (index, coverUrl) => {
if (list[index]) {
this.$set(list[index], 'cover', coverUrl);
}
});
},
// //
calcHeight(type) { calcHeight(type) {
const selector = `.list-item-${type}`; const selector = `.list-item-${type}`;

12
subpkg/pages/ranking/ranking.vue

@ -79,6 +79,7 @@
import { FetchInitScreenSetting } from '@/api/user'; import { FetchInitScreenSetting } from '@/api/user';
import { FetchBookRanking } from '@/api/book'; import { FetchBookRanking } from '@/api/book';
import config from '@/utils/config'; import config from '@/utils/config';
import { loadBookCoversBase64 } from '@/utils/bookCover';
export default { export default {
data() { data() {
@ -137,6 +138,8 @@ export default {
if (resultJson.success && resultJson.resultlist.length > 0) { if (resultJson.success && resultJson.resultlist.length > 0) {
// //
this.rankingData = resultJson.resultlist.sort((a, b) => b.TOTALNUM - a.TOTALNUM); this.rankingData = resultJson.resultlist.sort((a, b) => b.TOTALNUM - a.TOTALNUM);
//
this.loadCoversForList();
} else { } else {
this.rankingData = []; this.rankingData = [];
} }
@ -146,6 +149,15 @@ export default {
this.rankingData = []; this.rankingData = [];
this.loading = false; this.loading = false;
}) })
},
//
async loadCoversForList() {
await loadBookCoversBase64(this.rankingData, (index, coverUrl) => {
if (this.rankingData[index]) {
this.$set(this.rankingData[index], 'cover', coverUrl);
}
});
} }
} }
} }

61
utils/bookCover.js

@ -1,3 +1,5 @@
import config from '@/utils/config.js'
/** /**
* 获取已加载的 bookrecno 缓存 * 获取已加载的 bookrecno 缓存
* @returns {Set} - 已加载的 bookrecno 集合 * @returns {Set} - 已加载的 bookrecno 集合
@ -7,6 +9,65 @@ function getLoadedBookrecnos() {
return new Set(loadedList); return new Set(loadedList);
} }
/**
* 通过新接口获取单个图书封面返回 base64
* @param {string} isbn - ISBN 号码
* @returns {Promise<string|null>} - base64 图片字符串或 null
*/
export async function fetchBookCoverBase64(isbn) {
if (!isbn) {
return null;
}
const cleanIsbn = isbn.replace(/-/g, '');
const url = `${config.baseUrl}/api/fileRelevant/getBookCoverByISBN?isbn=${cleanIsbn}`;
return new Promise((resolve) => {
uni.request({
url: url,
method: 'GET',
success: (res) => {
const data = res.data;
if (data && data !== null && data !== '') {
// 返回完整的 data URL
resolve(`${data}`);
} else {
resolve(null);
}
},
fail: () => {
resolve(null);
}
});
});
}
/**
* 批量获取图书封面返回 base64
* @param {Array} bookList - 图书列表
* @param {Function} callback - 回调函数 (index, coverUrl)
* @returns {Promise<void>}
*/
export async function loadBookCoversBase64(bookList, callback) {
if (!bookList || bookList.length === 0 || typeof callback !== 'function') {
return;
}
for (let i = 0; i < bookList.length; i++) {
const book = bookList[i];
if (!book.isbn && !book.ISBN) continue;
try {
const coverUrl = await fetchBookCoverBase64(book.isbn || book.ISBN);
if (coverUrl) {
callback(i, coverUrl);
}
} catch (error) {
console.error('获取封面失败:', error);
}
}
}
/** /**
* 标记 bookrecno 已加载 * 标记 bookrecno 已加载
* @param {Array} bookrecnoList - 已加载的 bookrecno 列表 * @param {Array} bookrecnoList - 已加载的 bookrecno 列表

Loading…
Cancel
Save