App.vue 3.6 KB

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