index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <view class="startup-page">
  3. <text class="hint">{{ hintText }}</text>
  4. <text v-if="showRetry" class="retry" @click="runStartup">点击重试</text>
  5. </view>
  6. </template>
  7. <script>
  8. import {
  9. getToken,
  10. setToken,
  11. getCurrentUserInfo,
  12. normalizeUserPayload
  13. } from '../../utils/api'
  14. import { connectWebSocket } from '../../composables/useWebSocket'
  15. // #ifdef APP-PLUS
  16. import { setupAppNotifications } from '../../utils/notificationSetup'
  17. // #endif
  18. const USER_KEY = 'current_user'
  19. const ME_OPTIONS = {
  20. skipSessionRedirectOnAuthFailure: true,
  21. skipRequestFailToast: true
  22. }
  23. export default {
  24. data() {
  25. return {
  26. hintText: '软件正在启动中…',
  27. showRetry: false
  28. }
  29. },
  30. onLoad() {
  31. this.runStartup()
  32. },
  33. methods: {
  34. async runStartup() {
  35. const token = getToken()
  36. if (!token) {
  37. uni.reLaunch({ url: '/pages/login/index' })
  38. return
  39. }
  40. this.showRetry = false
  41. this.hintText = '软件正在启动中…'
  42. try {
  43. const me = await getCurrentUserInfo(token, ME_OPTIONS)
  44. const u = normalizeUserPayload(me)
  45. if (u) {
  46. const orgName = u.orgName || u.org_name || ''
  47. try {
  48. const prev = uni.getStorageSync(USER_KEY)
  49. const base = prev && typeof prev === 'object' ? prev : {}
  50. uni.setStorageSync(USER_KEY, {
  51. ...base,
  52. ...u,
  53. orgName,
  54. org_name: orgName
  55. })
  56. } catch (e) {}
  57. }
  58. connectWebSocket()
  59. // #ifdef APP-PLUS
  60. setupAppNotifications()
  61. // #endif
  62. uni.reLaunch({ url: '/pages/index/index' })
  63. } catch (e) {
  64. if (e && e.authFailure) {
  65. setToken('')
  66. uni.showToast({ title: '登录已过期', icon: 'none' })
  67. setTimeout(() => {
  68. uni.reLaunch({ url: '/pages/login/index' })
  69. }, 80)
  70. return
  71. }
  72. this.hintText = '网络异常,请检查网络后重试'
  73. this.showRetry = true
  74. uni.showToast({ title: e.message || '网络异常', icon: 'none' })
  75. }
  76. }
  77. }
  78. }
  79. </script>
  80. <style scoped>
  81. .startup-page {
  82. min-height: 100vh;
  83. display: flex;
  84. flex-direction: column;
  85. align-items: center;
  86. justify-content: center;
  87. background: #fff;
  88. padding: 48rpx;
  89. box-sizing: border-box;
  90. }
  91. .hint {
  92. font-size: 32rpx;
  93. color: #333;
  94. text-align: center;
  95. line-height: 1.5;
  96. }
  97. .retry {
  98. margin-top: 48rpx;
  99. font-size: 30rpx;
  100. color: #259653;
  101. text-decoration: underline;
  102. }
  103. </style>