App.vue 3.8 KB

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