App.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // 开发环境下跳过自动登录,避免无后端时 10s 超时阻塞
  37. try {
  38. var envInfo = uni.getAccountInfoSync()
  39. if (envInfo && envInfo.miniProgram && envInfo.miniProgram.envVersion === 'develop') {
  40. return
  41. }
  42. } catch (e) { /* 非小程序环境 */ }
  43. try {
  44. // 1. 先尝试用缓存的openid自动登录
  45. const cachedOpenid = uni.getStorageSync('openid')
  46. if (cachedOpenid) {
  47. const res = await this.request('/api/auth/auto-login', 'POST', { openid: cachedOpenid })
  48. if (res && res.data) {
  49. this.saveLoginInfo(res.data)
  50. console.log('openid自动登录成功')
  51. // 刷新页面到登录态
  52. uni.switchTab({ url: '/pages/index-home/index' })
  53. return
  54. }
  55. }
  56. // 2. openid自动登录失败,不自动调用uni.login(避免DevTools环境超时阻塞渲染层)
  57. // 用户会看到登录页,手动选择登录方式
  58. } catch (e) {
  59. console.log('自动登录失败,需要手动登录', e)
  60. }
  61. },
  62. saveLoginInfo(data) {
  63. uni.setStorageSync('token', data.token)
  64. uni.setStorageSync('userId', data.userId)
  65. uni.setStorageSync('role', data.role)
  66. uni.setStorageSync('familyId', data.familyId)
  67. // 自动登录后重置到真实登录角色,避免残留切换状态
  68. uni.setStorageSync('currentRole', data.role)
  69. uni.setStorageSync('isSwitchedChild', false)
  70. uni.setStorageSync('isSwitchedTeacher', false)
  71. if (data.openid) {
  72. uni.setStorageSync('openid', data.openid)
  73. }
  74. uni.setStorageSync('userInfo', {
  75. nickname: data.nickname,
  76. userId: data.userId
  77. })
  78. this.$store.commit('setToken', data.token)
  79. },
  80. request(url, method, data) {
  81. return new Promise((resolve, reject) => {
  82. uni.request({
  83. url: config.api(url),
  84. method: method,
  85. data: data,
  86. timeout: 10000,
  87. header: { 'Content-Type': 'application/json' },
  88. success: (res) => {
  89. if (res.data.code === 200) {
  90. resolve(res.data)
  91. } else {
  92. reject(res.data)
  93. }
  94. },
  95. fail: reject
  96. })
  97. })
  98. },
  99. // 处理小程序码场景值,跳转到邀请页
  100. handleScene(options) {
  101. if (!options || !options.query) return
  102. var scene = options.query.scene
  103. if (!scene) return
  104. var sceneDecoded = decodeURIComponent(scene)
  105. if (sceneDecoded.indexOf('invite=') === 0) {
  106. var token = sceneDecoded.substring(7)
  107. uni.navigateTo({
  108. url: '/pages/invite/join?token=' + token
  109. })
  110. } else if (sceneDecoded.indexOf('ref=') === 0) {
  111. var refCode = sceneDecoded.substring(4)
  112. if (!refCode) {
  113. console.warn('推广码场景缺少 refCode', sceneDecoded)
  114. return
  115. }
  116. uni.navigateTo({
  117. url: '/pages/invite/join?refCode=' + encodeURIComponent(refCode)
  118. })
  119. }
  120. }
  121. }
  122. }
  123. </script>
  124. <style>
  125. @import url('./common/uni.css');
  126. page {
  127. background-color: var(--bg, #F5F9FC);
  128. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI',
  129. 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
  130. 'Helvetica Neue', Helvetica, Arial, sans-serif;
  131. color: var(--text, #1E293B);
  132. font-size: 28rpx;
  133. line-height: 1.6;
  134. -webkit-font-smoothing: antialiased;
  135. }
  136. /* 页面容器 */
  137. .page {
  138. padding: 0 32rpx 32rpx;
  139. min-height: 100vh;
  140. }
  141. .page--no-padding {
  142. padding: 0;
  143. }
  144. /* 分段标题 */
  145. .section-header {
  146. font-weight: 700;
  147. font-size: 32rpx;
  148. margin: 32rpx 0 16rpx;
  149. color: var(--text, #1E293B);
  150. display: flex;
  151. align-items: center;
  152. justify-content: space-between;
  153. border-left: 8rpx solid #F08A7A;
  154. padding-left: 20rpx;
  155. }
  156. .section-header__more {
  157. font-size: 24rpx;
  158. color: var(--muted, #94A3B8);
  159. font-weight: 400;
  160. }
  161. /* 安全区底部 */
  162. .safe-area-bottom {
  163. padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
  164. padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
  165. }
  166. </style>