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

114 lines
3.8 KiB

3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
5 months ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
  1. <script>
  2. import { FetchLibcodeBycode,FetchInitFondsList } from '@/api/user'
  3. export default {
  4. globalData: {
  5. appid: '',
  6. libcode: '', // 全局libcode
  7. fondsName: '' // 新增:馆藏名称,全局使用
  8. },
  9. onLaunch() {
  10. this.initAppIdAndLibcode()
  11. },
  12. methods: {
  13. // 初始化:拿appid → 请求接口拿libcode,存入globalData
  14. async initAppIdAndLibcode() {
  15. // #ifdef MP-WEIXIN
  16. try {
  17. // 1、全局获取appid
  18. const accountInfo = wx.getAccountInfoSync()
  19. const appid = accountInfo.miniProgram.appId
  20. this.globalData.appid = appid
  21. // 优先读本地缓存libcode,减少接口请求
  22. let cacheLibcode = uni.getStorageSync('GLOBAL_LIBCODE')
  23. if(cacheLibcode){
  24. this.globalData.libcode = cacheLibcode
  25. console.log('使用缓存libcode:', cacheLibcode)
  26. // ✅ 缓存有libcode,直接去拿 fondsName
  27. await this.getFondsName(cacheLibcode)
  28. return
  29. }
  30. // 2、调用接口 FetchLibcodeBycode,传入appid获取libcode
  31. const res = await FetchLibcodeBycode({
  32. code: 'vx_mini_appId',
  33. value: appid
  34. })
  35. if(res.code === 200 && res.data){
  36. const libcode = res.data
  37. this.globalData.libcode = libcode
  38. uni.setStorageSync('GLOBAL_LIBCODE', libcode)
  39. console.log('接口获取全局libcode成功:', libcode)
  40. // ✅拿到libcode之后,调用FetchInitFondsList获取馆藏名称
  41. await this.getFondsName(libcode)
  42. }else{
  43. console.error('FetchLibcodeBycode 获取libcode失败', res)
  44. }
  45. } catch(e){
  46. console.error('初始化appid/libcode异常', e)
  47. }
  48. // #endif
  49. },
  50. /**
  51. * 根据libcode获取馆藏名称 fondsName
  52. */
  53. async getFondsName(libcode) {
  54. try {
  55. // 优先读缓存
  56. let cacheFondsName = uni.getStorageSync('GLOBAL_FONDS_NAME')
  57. if (cacheFondsName) {
  58. this.globalData.fondsName = cacheFondsName
  59. console.log('使用缓存 fondsName:', cacheFondsName)
  60. return
  61. }
  62. // 调用接口,传入libcode
  63. const fondRes = await FetchInitFondsList({
  64. 'fondsNo': libcode
  65. })
  66. // console.log('FetchInitFondsList返回结果', fondRes)
  67. if (fondRes.code === 200 && fondRes.data) {
  68. const fondsName = fondRes.data.fondsName || ''
  69. this.globalData.fondsName = fondsName
  70. uni.setStorageSync('GLOBAL_FONDS_NAME', fondsName)
  71. console.log('获取馆藏名称成功:', fondsName)
  72. } else {
  73. console.error('FetchInitFondsList 获取馆藏失败', fondRes)
  74. }
  75. } catch (err) {
  76. console.error('getFondsName异常', err)
  77. }
  78. },
  79. // 退出登录 待用
  80. // logout(){
  81. // // 1. 清除登录缓存
  82. // uni.removeStorageSync('wx_login_code')
  83. // uni.removeStorageSync('READER_CARD_LIST')
  84. // uni.removeStorageSync('CURRENT_READER_CARD')
  85. // // 2.清除libcode缓存,强制重新拉取
  86. // uni.removeStorageSync('GLOBAL_LIBCODE')
  87. // uni.removeStorageSync('GLOBAL_FONDS_NAME') // 也要清空馆藏缓存
  88. // getApp().globalData.libcode = ''
  89. // getApp().globalData.fondsName = ''
  90. // // 跳转登录页
  91. // uni.reLaunch({url:'/pages/login/login'})
  92. // }
  93. // 刷新libcode 待用
  94. // refreshLibcode(){
  95. // uni.removeStorageSync('GLOBAL_LIBCODE')
  96. // uni.removeStorageSync('GLOBAL_FONDS_NAME') // 清空馆藏
  97. // getApp().globalData.libcode = ''
  98. // getApp().globalData.fondsName = ''
  99. // uni.showToast({title:"已重置libcode,将重新获取",icon:"none"})
  100. // // 重新执行App.vue里面的初始化方法
  101. // getApp().$options.methods.initAppIdAndLibcode()
  102. // }
  103. }
  104. }
  105. </script>