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.
|
|
<script>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
// 优先读本地缓存libcode,减少接口请求
let cacheLibcode = uni.getStorageSync('GLOBAL_LIBCODE') if(cacheLibcode){ this.globalData.libcode = cacheLibcode console.log('使用缓存libcode:', cacheLibcode) return } // 2、调用接口 FetchLibcodeBycode,传入appid获取libcode
const res = await FetchLibcodeBycode({ code: 'vx_mini_appId', value: appid }) if(res.code === 200 && res.data){ const libcode = res.data this.globalData.libcode = libcode uni.setStorageSync('GLOBAL_LIBCODE', libcode) console.log('接口获取全局libcode成功:', libcode) }else{ console.error('FetchLibcodeBycode 获取libcode失败', res) } } catch(e){ console.error('初始化appid/libcode异常', e) } // #endif
}, // 退出登录 待用
// logout(){
// // 1. 清除登录缓存
// uni.removeStorageSync('wx_login_code')
// uni.removeStorageSync('READER_CARD_LIST')
// uni.removeStorageSync('CURRENT_READER_CARD')
// // 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()
// }
}}</script>
|