App.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <script>
  2. import config from '@/config.js'
  3. import Vue from 'vue'
  4. Vue.prototype.$config = config
  5. export default {
  6. onLaunch: function(options) {
  7. console.log('App Launch', options)
  8. // 冷启动标记,首页根据此标记决定显示 splash overlay
  9. uni.setStorageSync('_showSplashOverlay', '1')
  10. // 处理小程序码场景值(从 wxacode 扫码进入)
  11. this.handleScene(options)
  12. // TabBar 始终显示 5 个底签,不因登录状态隐藏
  13. const token = uni.getStorageSync('token')
  14. if (token) {
  15. this.$store.commit('setToken', token)
  16. } else {
  17. // 无token时尝试自动登录
  18. this.tryAutoLogin()
  19. }
  20. },
  21. onShow: function(options) {
  22. console.log('App Show', options)
  23. // 每次 app 展示时也检查场景值(小程序码可能从冷启动后的 onShow 传入)
  24. this.handleScene(options)
  25. // 同步 storage → Vuex(防止 reLaunch/navigateTo 后 Vuex 未更新的问题)
  26. var token = uni.getStorageSync('token')
  27. if (token && token !== this.$store.state.token) {
  28. this.$store.commit('setToken', token)
  29. }
  30. },
  31. onHide: function() {
  32. console.log('App Hide')
  33. },
  34. methods: {
  35. async tryAutoLogin() {
  36. try {
  37. // 1. 先尝试用缓存的openid自动登录
  38. const cachedOpenid = uni.getStorageSync('openid')
  39. if (cachedOpenid) {
  40. const res = await this.request('/api/auth/auto-login', 'POST', { openid: cachedOpenid })
  41. if (res && res.data) {
  42. this.saveLoginInfo(res.data)
  43. console.log('openid自动登录成功')
  44. // 刷新页面到登录态
  45. uni.switchTab({ url: '/pages/index/index' })
  46. return
  47. }
  48. }
  49. // 2. openid自动登录失败,不自动调用uni.login(避免DevTools环境超时阻塞渲染层)
  50. // 用户会看到登录页,手动选择登录方式
  51. } catch (e) {
  52. console.log('自动登录失败,需要手动登录', e)
  53. }
  54. },
  55. saveLoginInfo(data) {
  56. uni.setStorageSync('token', data.token)
  57. uni.setStorageSync('userId', data.userId)
  58. uni.setStorageSync('role', data.role)
  59. uni.setStorageSync('familyId', data.familyId)
  60. // 自动登录后重置到真实登录角色,避免残留切换状态
  61. uni.setStorageSync('currentRole', data.role)
  62. uni.setStorageSync('isSwitchedChild', false)
  63. uni.setStorageSync('isSwitchedTeacher', false)
  64. if (data.openid) {
  65. uni.setStorageSync('openid', data.openid)
  66. }
  67. uni.setStorageSync('userInfo', {
  68. nickname: data.nickname,
  69. userId: data.userId
  70. })
  71. this.$store.commit('setToken', data.token)
  72. },
  73. request(url, method, data) {
  74. return new Promise((resolve, reject) => {
  75. uni.request({
  76. url: config.api(url),
  77. method: method,
  78. data: data,
  79. timeout: 10000,
  80. header: { 'Content-Type': 'application/json' },
  81. success: (res) => {
  82. if (res.data.code === 200) {
  83. resolve(res.data)
  84. } else {
  85. reject(res.data)
  86. }
  87. },
  88. fail: reject
  89. })
  90. })
  91. },
  92. // 处理小程序码场景值,跳转到邀请页
  93. handleScene(options) {
  94. if (!options || !options.query) return
  95. var scene = options.query.scene
  96. if (!scene) return
  97. var sceneDecoded = decodeURIComponent(scene)
  98. if (sceneDecoded.indexOf('invite=') === 0) {
  99. var token = sceneDecoded.substring(7)
  100. uni.navigateTo({
  101. url: '/pages/invite/join?token=' + token
  102. })
  103. }
  104. }
  105. }
  106. }
  107. </script>
  108. <style>
  109. @import url('./common/uni.css');
  110. page {
  111. background-color: var(--bg, #F5F9FC);
  112. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI',
  113. 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
  114. 'Helvetica Neue', Helvetica, Arial, sans-serif;
  115. color: var(--text, #1E293B);
  116. font-size: 28rpx;
  117. line-height: 1.6;
  118. -webkit-font-smoothing: antialiased;
  119. }
  120. /* 页面容器 */
  121. .page {
  122. padding: 0 32rpx 32rpx;
  123. min-height: 100vh;
  124. }
  125. .page--no-padding {
  126. padding: 0;
  127. }
  128. /* 分段标题 */
  129. .section-header {
  130. font-weight: 700;
  131. font-size: 32rpx;
  132. margin: 32rpx 0 16rpx;
  133. color: var(--text, #1E293B);
  134. display: flex;
  135. align-items: center;
  136. justify-content: space-between;
  137. border-left: 8rpx solid #F08A7A;
  138. padding-left: 20rpx;
  139. }
  140. .section-header__more {
  141. font-size: 24rpx;
  142. color: var(--muted, #94A3B8);
  143. font-weight: 400;
  144. }
  145. /* 安全区底部 */
  146. .safe-area-bottom {
  147. padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
  148. padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
  149. }
  150. </style>