WXL (wul)
昨天 4c44ccbdcbe25de7dc0725bf53f1290fba10edf7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<template>
  <div class="sso-redirect">
    <div class="loading-container">
      <el-alert
        v-if="errorMessage"
        :title="errorMessage"
        type="error"
        :closable="false"
        show-icon
      ></el-alert>
      <div v-else class="loading-content">
        <i class="el-icon-loading"></i>
        <p>单点登录中,请稍候...</p>
      </div>
    </div>
  </div>
</template>
 
<script>
import { setToken } from '@/utils/auth'
 
export default {
  name: 'SSORedirect',
  data() {
    return {
      errorMessage: '',
      loading: true
    }
  },
  created() {
    this.handleSSORedirect()
  },
  methods: {
    async handleSSORedirect() {
      try {
        // 从URL参数中获取token和机构信息
        const { token, orgid, orgname, ZuHuID, deptCode, redirect } = this.$route.query
 
        // 验证必要参数
        if (!token) {
          throw new Error('缺少认证令牌(token)')
        }
        setToken(token)
        this.$store.commit('SET_TOKEN', token)
 
        // 存储机构信息到localStorage
        if (orgid) {
          localStorage.setItem('orgid', orgid)
          this.$store.dispatch('UpdateOrgId', orgid)
        }
        if (orgname) localStorage.setItem('orgname', orgname)
        if (ZuHuID) localStorage.setItem('ZuHuID', ZuHuID)
        if (deptCode) localStorage.setItem('deptCode', deptCode)
 
        // 获取用户信息
        await this.$store.dispatch('GetInfo')
 
        // 确定重定向路径
        let redirectPath = '/index'
        if (redirect) {
          redirectPath = decodeURIComponent(redirect)
        } else {
          // 根据用户角色决定默认跳转页面
          const roles = this.$store.state.user.roles
          const username = this.$store.state.user.name
 
          if (roles.includes('admin') || username === 'admin') {
            redirectPath = '/index'
          } else {
            redirectPath = '/followvisit/discharge'
          }
        }
 
        // 跳转到目标页面
        this.$router.replace({ path: redirectPath })
 
      } catch (error) {
        console.error('SSO登录失败:', error)
        this.errorMessage = `单点登录失败: ${error.message || '未知错误'}`
        this.loading = false
 
        // 5秒后跳转到普通登录页
        setTimeout(() => {
          this.$router.replace('/login')
        }, 5000)
      }
    }
  }
}
</script>
 
<style scoped>
.sso-redirect {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}
 
.loading-container {
  text-align: center;
  padding: 20px;
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
 
.loading-content {
  padding: 20px;
}
 
.el-icon-loading {
  font-size: 40px;
  color: #409EFF;
  margin-bottom: 10px;
}
 
p {
  margin: 0;
  color: #606266;
}
</style>