App.vue 4.5 KB

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