ssoLogin.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <div class="login-container">
  3. <div class="login-content">
  4. <div class="login-title">正在登录中...</div>
  5. <div class="loading-spinner">
  6. <i class="el-icon-loading"></i>
  7. </div>
  8. <div class="login-status">{{ statusText }}</div>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. import { ssoLoginByCode } from '@/api/user'
  14. import store from '@/store'
  15. import router from '@/router'
  16. export default {
  17. name: 'SsoLogin',
  18. data() {
  19. return {
  20. statusText: '正在解析登录参数...',
  21. }
  22. },
  23. mounted() {
  24. this.handleSSOLogin()
  25. },
  26. methods: {
  27. getUrlParam(key) {
  28. var query = window.location.search.substring(1)
  29. var vars = query.split('&')
  30. for (var i = 0; i < vars.length; i++) {
  31. var pair = vars[i].split('=')
  32. if (pair[0] == key) {
  33. return pair[1]
  34. }
  35. }
  36. return false
  37. },
  38. async handleSSOLogin() {
  39. try {
  40. // 1. 获取URL中的code参数
  41. const code = this.getUrlParam('code')
  42. if (!code) {
  43. this.statusText = '未获取到登录参数,请重试'
  44. setTimeout(() => {
  45. router.replace('/login')
  46. }, 2000)
  47. return
  48. }
  49. this.statusText = '正在验证登录信息...'
  50. // 2. 调用后端接口,将code传给后端
  51. const res = await ssoLoginByCode({
  52. code,
  53. })
  54. if (res.token) {
  55. this.statusText = '登录成功,正在跳转...'
  56. // 使用authentication统一处理用户信息
  57. await store.dispatch('user/authentication', res)
  58. // 5. 登录成功后跳转到首页或重定向页面
  59. const redirect = this.getUrlParam('redirect') || '/'
  60. router.replace(redirect)
  61. } else if (!res.state) {
  62. this.statusText = res.message || '单点登录失败'
  63. setTimeout(() => {
  64. router.replace('/login')
  65. }, 2000)
  66. return
  67. }
  68. } catch (error) {
  69. console.error('单点登录失败:', error)
  70. this.statusText = '登录失败'
  71. // 登录失败后重定向到登录页
  72. setTimeout(() => {
  73. router.replace('/login')
  74. }, 2000)
  75. }
  76. },
  77. },
  78. }
  79. </script>
  80. <style lang="scss" scoped>
  81. .login-container {
  82. min-height: 100vh;
  83. display: flex;
  84. align-items: center;
  85. justify-content: center;
  86. background-color: #f0f2f5;
  87. }
  88. .login-content {
  89. background: #fff;
  90. padding: 40px;
  91. border-radius: 8px;
  92. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  93. text-align: center;
  94. min-width: 360px;
  95. }
  96. .login-title {
  97. font-size: 20px;
  98. font-weight: 500;
  99. color: #303133;
  100. margin-bottom: 30px;
  101. }
  102. .loading-spinner {
  103. margin: 20px 0;
  104. }
  105. .loading-spinner i {
  106. font-size: 48px;
  107. color: #409eff;
  108. animation: rotate 1.5s linear infinite;
  109. }
  110. .login-status {
  111. font-size: 14px;
  112. color: #606266;
  113. margin-top: 20px;
  114. }
  115. @keyframes rotate {
  116. from {
  117. transform: rotate(0deg);
  118. }
  119. to {
  120. transform: rotate(360deg);
  121. }
  122. }
  123. </style>