App.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. // 自动登录后重置到真实登录角色,避免残留切换状态
  67. uni.setStorageSync('currentRole', data.role)
  68. uni.setStorageSync('isSwitchedChild', false)
  69. uni.setStorageSync('isSwitchedTeacher', false)
  70. if (data.openid) {
  71. uni.setStorageSync('openid', data.openid)
  72. }
  73. uni.setStorageSync('userInfo', {
  74. nickname: data.nickname,
  75. userId: data.userId
  76. })
  77. this.$store.commit('setToken', data.token)
  78. },
  79. request(url, method, data) {
  80. return new Promise((resolve, reject) => {
  81. uni.request({
  82. url: config.api(url),
  83. method: method,
  84. data: data,
  85. timeout: 10000,
  86. header: { 'Content-Type': 'application/json' },
  87. success: (res) => {
  88. if (res.data.code === 200) {
  89. resolve(res.data)
  90. } else {
  91. reject(res.data)
  92. }
  93. },
  94. fail: reject
  95. })
  96. })
  97. },
  98. // 处理小程序码场景值,跳转到邀请页
  99. handleScene(options) {
  100. if (!options || !options.query) return
  101. var scene = options.query.scene
  102. if (!scene) return
  103. var sceneDecoded = decodeURIComponent(scene)
  104. if (sceneDecoded.indexOf('invite=') === 0) {
  105. var token = sceneDecoded.substring(7)
  106. uni.navigateTo({
  107. url: '/pages/invite/join?token=' + token
  108. })
  109. } else if (sceneDecoded.indexOf('ref=') === 0) {
  110. var refCode = sceneDecoded.substring(4)
  111. if (!refCode) {
  112. console.warn('推广码场景缺少 refCode', sceneDecoded)
  113. return
  114. }
  115. uni.navigateTo({
  116. url: '/pages/invite/join?refCode=' + encodeURIComponent(refCode)
  117. })
  118. }
  119. }
  120. }
  121. }
  122. </script>
  123. <style>
  124. @import url('./common/uni.css');
  125. page {
  126. background-color: var(--bg, #F5F9FC);
  127. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI',
  128. 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
  129. 'Helvetica Neue', Helvetica, Arial, sans-serif;
  130. color: var(--text, #1E293B);
  131. font-size: 28rpx;
  132. line-height: 1.6;
  133. -webkit-font-smoothing: antialiased;
  134. }
  135. /* 页面容器 */
  136. .page {
  137. padding: 0 32rpx 32rpx;
  138. min-height: 100vh;
  139. }
  140. .page--no-padding {
  141. padding: 0;
  142. }
  143. /* 分段标题 */
  144. .section-header {
  145. font-weight: 700;
  146. font-size: 32rpx;
  147. margin: 32rpx 0 16rpx;
  148. color: var(--text, #1E293B);
  149. display: flex;
  150. align-items: center;
  151. justify-content: space-between;
  152. border-left: 8rpx solid #F08A7A;
  153. padding-left: 20rpx;
  154. }
  155. .section-header__more {
  156. font-size: 24rpx;
  157. color: var(--muted, #94A3B8);
  158. font-weight: 400;
  159. }
  160. /* 安全区底部 */
  161. .safe-area-bottom {
  162. padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
  163. padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
  164. }
  165. </style>