App.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <script>
  2. import config from '@/config.js'
  3. import Vue from 'vue'
  4. Vue.prototype.$config = config
  5. export default {
  6. onLaunch: function() {
  7. console.log('App Launch')
  8. // 检查登录状态
  9. const token = uni.getStorageSync('token')
  10. if (token) {
  11. this.$store.commit('setToken', token)
  12. // 已登录:确保原生 TabBar 显示
  13. uni.showTabBar()
  14. } else {
  15. // 未登录:隐藏原生 TabBar
  16. uni.hideTabBar()
  17. // 如果当前不在允许的未登录页面,则跳转发现页
  18. const pages = getCurrentPages()
  19. const currentRoute = pages.length > 0 ? pages[pages.length - 1].route : ''
  20. const allowedRoutes = ['pages/discover/index', 'pages/index/index', 'pages/body/index', 'pages/mind/index', 'pages/shop/index']
  21. if (!allowedRoutes.includes(currentRoute)) {
  22. uni.reLaunch({ url: '/pages/discover/index' })
  23. }
  24. // 无token时尝试自动登录
  25. this.tryAutoLogin()
  26. }
  27. },
  28. onShow: function() {
  29. console.log('App Show')
  30. // 每次显示时检查登录状态,同步 TabBar
  31. const token = uni.getStorageSync('token')
  32. if (token) {
  33. uni.showTabBar()
  34. } else {
  35. uni.hideTabBar()
  36. }
  37. },
  38. onHide: function() {
  39. console.log('App Hide')
  40. },
  41. methods: {
  42. async tryAutoLogin() {
  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. return
  52. }
  53. }
  54. // 2. openid自动登录失败,不自动调用uni.login(避免DevTools环境超时阻塞渲染层)
  55. // 用户会看到登录页,手动选择登录方式
  56. } catch (e) {
  57. console.log('自动登录失败,需要手动登录', e)
  58. }
  59. },
  60. saveLoginInfo(data) {
  61. uni.setStorageSync('token', data.token)
  62. uni.setStorageSync('userId', data.userId)
  63. uni.setStorageSync('role', data.role)
  64. uni.setStorageSync('familyId', data.familyId)
  65. // 自动登录后重置到真实登录角色,避免残留切换状态
  66. uni.setStorageSync('currentRole', data.role)
  67. uni.setStorageSync('isSwitchedChild', false)
  68. uni.setStorageSync('isSwitchedTeacher', false)
  69. if (data.openid) {
  70. uni.setStorageSync('openid', data.openid)
  71. }
  72. uni.setStorageSync('userInfo', {
  73. nickname: data.nickname,
  74. userId: data.userId
  75. })
  76. this.$store.commit('setToken', data.token)
  77. },
  78. request(url, method, data) {
  79. return new Promise((resolve, reject) => {
  80. uni.request({
  81. url: config.api(url),
  82. method: method,
  83. data: data,
  84. timeout: 10000,
  85. header: { 'Content-Type': 'application/json' },
  86. success: (res) => {
  87. if (res.data.code === 200) {
  88. resolve(res.data)
  89. } else {
  90. reject(res.data)
  91. }
  92. },
  93. fail: reject
  94. })
  95. })
  96. }
  97. }
  98. }
  99. </script>
  100. <style>
  101. @import url('./common/uni.css');
  102. page {
  103. background-color: var(--bg, #F5F9FC);
  104. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI',
  105. 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
  106. 'Helvetica Neue', Helvetica, Arial, sans-serif;
  107. color: var(--text, #1E293B);
  108. font-size: 28rpx;
  109. line-height: 1.6;
  110. -webkit-font-smoothing: antialiased;
  111. }
  112. /* 页面容器 */
  113. .page {
  114. padding: 0 32rpx 32rpx;
  115. min-height: 100vh;
  116. }
  117. .page--no-padding {
  118. padding: 0;
  119. }
  120. /* 分段标题 */
  121. .section-header {
  122. font-weight: 700;
  123. font-size: 32rpx;
  124. margin: 32rpx 0 16rpx;
  125. color: var(--text, #1E293B);
  126. display: flex;
  127. align-items: center;
  128. justify-content: space-between;
  129. border-left: 8rpx solid #F08A7A;
  130. padding-left: 20rpx;
  131. }
  132. .section-header__more {
  133. font-size: 24rpx;
  134. color: var(--muted, #94A3B8);
  135. font-weight: 400;
  136. }
  137. /* 安全区底部 */
  138. .safe-area-bottom {
  139. padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
  140. padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
  141. }
  142. </style>