App.vue 3.3 KB

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