App.vue 3.9 KB

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