diff --git a/App.vue b/App.vue index ee288b1..853d000 100644 --- a/App.vue +++ b/App.vue @@ -1,32 +1,71 @@ +import { FetchLibcodeBycode } from '@/api/user' + +export default { + globalData: { + appid: '', + libcode: '', // 全局libcode,所有页面 getApp().globalData.libcode 访问 + }, + onLaunch() { + this.initAppIdAndLibcode() + }, + methods: { + // 初始化:拿appid → 请求接口拿libcode,存入globalData + async initAppIdAndLibcode() { + // #ifdef MP-WEIXIN + try { + // 1、全局获取appid + const accountInfo = wx.getAccountInfoSync() + const appid = accountInfo.miniProgram.appId + this.globalData.appid = appid - + // // 2.清除libcode缓存,强制重新拉取 + // uni.removeStorageSync('GLOBAL_LIBCODE') + // getApp().globalData.libcode = '' + + // // 跳转登录页 + // uni.reLaunch({url:'/pages/login/login'}) + // } + // 刷新libcode 待用 + // refreshLibcode(){ + // uni.removeStorageSync('GLOBAL_LIBCODE') + // getApp().globalData.libcode = '' + // uni.showToast({title:"已重置libcode,将重新获取",icon:"none"}) + // // 重新执行App.vue里面的初始化方法 + // getApp().$options.methods.initAppIdAndLibcode() + // } + } +} + diff --git a/api/user.js b/api/user.js index 70413e8..adbd0eb 100644 --- a/api/user.js +++ b/api/user.js @@ -182,4 +182,12 @@ export function FetchMsgSecCheck(data) { method: 'POST', data }); -} \ No newline at end of file +} + +// 根据微信code 获取馆代码 +export function FetchLibcodeBycode(data) { + return request({ + url: '/api/screenSetting/getLibcodeBycode', + data + }); +} diff --git a/pages/home/home.vue b/pages/home/home.vue index 455a963..4ab22d5 100644 --- a/pages/home/home.vue +++ b/pages/home/home.vue @@ -237,6 +237,7 @@ import { FetchInitScreenSetting, FetchOpenId, FetchFindAllReaderBindByOpenId} fr import { FetchInitScreenBookRecommend } from '@/api/book'; import config from '@/utils/config'; import { STORAGE_KEYS } from '@/utils/storage'; +import { waitLibcodeReady } from '@/utils/global' export default { data() { @@ -255,16 +256,25 @@ export default { }; }, onLoad() { - this.getRecommendBooks(); - this.getScreenSetting() + // this.getRecommendBooks(); + // this.getScreenSetting() + this.initPage() }, onShow() { this.initUserAndCheckBind(); }, methods:{ - async getScreenSetting() { - const res = await FetchInitScreenSetting({ libcode: config.LIB_CODE }); - + async initPage(){ + const libcode = await waitLibcodeReady() + if(!libcode){ + uni.showToast({title:'获取图书馆编码失败',icon:'none'}) + return + } + this.getRecommendBooks(libcode); + this.getScreenSetting(libcode) + }, + async getScreenSetting(libcode) { + const res = await FetchInitScreenSetting({ libcode }); // 处理微信二维码 if (res.data.wechat_qr_code && res.data.wechat_qr_code.context) { @@ -310,17 +320,11 @@ export default { } this.$refs.erweimapopup.open() }, - - - // 格式化读者证号 formatCardNo(cardNo) { if (!cardNo) return ''; const str = String(cardNo); const len = str.length; - - - if (len > 8) { // 前2位 + 中间全部* + 后2位 const front = str.substring(0, 2); @@ -338,6 +342,8 @@ export default { // 初始化:登录 → 获取openid → 查是否绑定 async initUserAndCheckBind() { try { + const libcode = await waitLibcodeReady() + if(!libcode) return // 1. 先看缓存有没有 openId let openId = uni.getStorageSync('wx_login_code'); @@ -350,43 +356,28 @@ export default { }); }); - - if (!loginRes.code) { uni.showToast({ title: '授权失败', icon: 'none' }); return; } - - // 3. 获取 openId const openRes = await FetchOpenId({ - libcode: config.LIB_CODE, + libcode, code: loginRes.code }); - - - if (openRes.code !== 200 || !openRes.data) { return; } - - - openId = openRes.data; uni.setStorageSync('wx_login_code', openId); } - - - // 4. 有了 openId → 查绑定状态 const res = await FetchFindAllReaderBindByOpenId({ - libcode: config.LIB_CODE, + libcode, openId: openId }); - - if (res.code === 200 && res.data.length > 0) { this.isBindLibraryCard = true; // 默认证排第一位 @@ -404,8 +395,6 @@ export default { uni.setStorageSync(STORAGE_KEYS.CURRENT_READER_CARD, null); } - - } catch (err) { console.error('初始化失败:', err); const list = uni.getStorageSync(STORAGE_KEYS.READER_CARD_LIST) || []; @@ -413,20 +402,15 @@ export default { const defaultCard = list.find(item => item.bindDefault === true); const otherCards = list.filter(item => !item.bindDefault); this.readerCardList = defaultCard ? [defaultCard, ...otherCards] : list; - - - this.isBindLibraryCard = list.length > 0; } }, // 获取图书推荐 + 封面转 base64 - async getRecommendBooks() { + async getRecommendBooks(libcode) { try { - const res = await FetchInitScreenBookRecommend({ libcode: config.LIB_CODE }); + const res = await FetchInitScreenBookRecommend({ libcode }); let books = res.data || []; - - // 只处理并展示前3条,避免setData数据过大 const limit = 3; const displayBooks = books.slice(0, limit); @@ -448,9 +432,6 @@ export default { item.base64Cover = ''; } } - - - // 只将展示所需的前3条数据放入响应式,减少setData传输量 this.recommendedBooks = displayBooks; this.isLoading = false; diff --git a/pages/lendCar/lendCar.vue b/pages/lendCar/lendCar.vue index d49cbc2..af90021 100644 --- a/pages/lendCar/lendCar.vue +++ b/pages/lendCar/lendCar.vue @@ -140,7 +140,7 @@ export default { * 获取屏幕配置 */ async getScreenSetting() { - const res = await FetchInitScreenSetting({ libcode: config.LIB_CODE }); + const res = await FetchInitScreenSetting({ libcode: getApp().globalData.libcode }); this.screenConfig = { thirdUrl: res.data.open_lib_http?.context || '', thirdAppid: res.data.open_lib_appId?.context || '', diff --git a/pages/login/login.vue b/pages/login/login.vue index 05fa3a0..d12fec5 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -134,7 +134,7 @@ export default { async getScreenSetting() { try { - const res = await FetchInitScreenSetting({ libcode: config.LIB_CODE }); + const res = await FetchInitScreenSetting({ libcode: getApp().globalData.libcode }); const data = res.data; this.screenConfig = { thirdUrl: data.open_lib_http?.context || '', @@ -211,7 +211,7 @@ export default { openid: openId, bindValue: this.queryvalue, bindType: 'rdid', - libcode: config.LIB_CODE, + libcode: getApp().globalData.libcode, } // 调用绑定接口 const bindRes = await FetchBindReadCard(bindParams); @@ -225,7 +225,7 @@ export default { icon: 'success' }); const data = { - libcode: config.LIB_CODE, + libcode: getApp().globalData.libcode, openId: openId } FetchFindAllReaderBindByOpenId(data).then(res => { diff --git a/pages/search/search.vue b/pages/search/search.vue index 26332b1..80d3f89 100644 --- a/pages/search/search.vue +++ b/pages/search/search.vue @@ -139,7 +139,7 @@ export default { // 获取opacUrl async getOpacUrl() { try { - const res = await FetchInitScreenSetting({ libcode: config.LIB_CODE }); + const res = await FetchInitScreenSetting({ libcode: getApp().globalData.libcode }); this.opacUrl = res.data.opac_url?.context || ''; } catch (err) { console.error('获取配置失败', err); @@ -240,7 +240,7 @@ export default { const openId = await getOpenId(); if (openId) { const res = await FetchFindAllBookCollectionByOpenId({ - libcode: config.LIB_CODE, + libcode: getApp().globalData.libcode, openId: openId, page: 0, size: 100 diff --git a/pages/user/user.vue b/pages/user/user.vue index 3f93319..4b352fc 100644 --- a/pages/user/user.vue +++ b/pages/user/user.vue @@ -100,7 +100,7 @@ export default { if (openId) { const res = await FetchFindAllReaderByOpenId({ - libcode: config.LIB_CODE, + libcode: getApp().globalData.libcode, openId: openId }); diff --git a/subpkg/pages/activity-detail/activity-detail.vue b/subpkg/pages/activity-detail/activity-detail.vue index 2eda40e..e0b7cdb 100644 --- a/subpkg/pages/activity-detail/activity-detail.vue +++ b/subpkg/pages/activity-detail/activity-detail.vue @@ -85,7 +85,7 @@