yuang-framework-ui-common 1.0.95 → 1.0.96

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,8 @@ import { isFullUrl } from './urlUtils';
5
5
  import { getLocalStorageItem, setLocalStorageItem, removeLocalStorageItem } from './storageUtils';
6
6
  import { application } from '../config/applicationConfig';
7
7
  import { getIsDebug } from '../utils/htmlUtils';
8
-
8
+ import { http} from '../config/httpConfig';
9
+ import { removeParameter } from '../utils/urlUtils';
9
10
 
10
11
  /**
11
12
  * 获取sso登录地址
@@ -78,25 +79,71 @@ const getSsoLogoutUrl = (ssoRedirectUrl = '') => {
78
79
  }
79
80
  return ssoLogoutUrl;
80
81
  }
82
+ //
83
+ // /**
84
+ // * 获取sso认证路由路径
85
+ // * @param ssoRedirectRoutePath
86
+ // */
87
+ // const getSsoAuthRoutePath = (ssoRedirectRoutePath = '') => {
88
+ // if(getIsDebug()) {
89
+ // debugger;
90
+ // }
91
+ // let ssoAuthRoutePath = '/sso/client/sso-auth/index';
92
+ // if (!ssoRedirectRoutePath) {
93
+ // ssoAuthRoutePath += `?ssoRedirectRoutePath=${encodeURIComponent('/sso/client/sso-auth/auth-success')}`;
94
+ // } else {
95
+ // ssoAuthRoutePath += `?ssoRedirectRoutePath=${encodeURIComponent(ssoRedirectRoutePath)}`;
96
+ // }
97
+ // if(getIsDebug()) {
98
+ // console.log('ssoAuthRoutePath', ssoAuthRoutePath);
99
+ // }
100
+ // return ssoAuthRoutePath;
101
+ // }
81
102
 
82
103
  /**
83
- * 获取sso认证路由路径
84
- * @param ssoRedirectRoutePath
104
+ * 处理路由守卫sso认证
105
+ * @param to
106
+ * @param from
85
107
  */
86
- const getSsoAuthRoutePath = (ssoRedirectRoutePath = '') => {
87
- if(getIsDebug()) {
88
- debugger;
89
- }
90
- let ssoAuthRoutePath = '/sso/client/sso-auth/index';
91
- if (!ssoRedirectRoutePath) {
92
- ssoAuthRoutePath += `?ssoRedirectRoutePath=${encodeURIComponent('/sso/client/sso-auth/auth-success')}`;
93
- } else {
94
- ssoAuthRoutePath += `?ssoRedirectRoutePath=${encodeURIComponent(ssoRedirectRoutePath)}`;
95
- }
96
- if(getIsDebug()) {
97
- console.log('ssoAuthRoutePath', ssoAuthRoutePath);
98
- }
99
- return ssoAuthRoutePath;
108
+ const handleRouterGuardSsoAuth = (to : any, from : any) => {
109
+ from;
110
+ return new Promise((resolve, reject) => {
111
+ // 已登录,直接放行
112
+ if (getSsoAccessToken()) {
113
+ return resolve({result: true});
114
+ }
115
+ // 免登录路由,直接放行
116
+ if (to.meta?.isSsoClientExcluded) {
117
+ return resolve({result: true});
118
+ }
119
+ // 无认证码,跳转到 SSO 授权页(window.location 跳转后 return false 终止路由)
120
+ const ssoAuthCode = to.query.ssoAuthCode;
121
+ if (!ssoAuthCode) {
122
+ window.location.href = getSsoAuthUrl(window.location.href) || '';
123
+ // 这里一定要return,不return还是会继续执行下面的代码
124
+ return resolve({result: false});
125
+ }
126
+ try {
127
+ // 有认证码,请求获取 AccessToken(try/catch 捕获所有异常)
128
+ http.get(`/sso-api/client/sso-auth/getSsoAccessToken`, { params: { ssoAuthCode } }).then(res =>{
129
+ setSsoAccessToken(res.data.data);
130
+ // 处理重定向路径
131
+ let ssoRedirectRoutePath = to.query.ssoRedirectRoutePath as string;
132
+ ssoRedirectRoutePath = (ssoRedirectRoutePath && decodeURIComponent(ssoRedirectRoutePath)) || '/sso/client/sso-login/sso-login-success';
133
+ ssoRedirectRoutePath = removeParameter('ssoAuthCode', ssoRedirectRoutePath);
134
+ // 无权限页重定向为登录成功页,避免切换账号误解
135
+ if (ssoRedirectRoutePath === '/sso/client/sso-login/sso-login-no-privilege') {
136
+ ssoRedirectRoutePath = '/sso/client/sso-login/sso-login-success';
137
+ }
138
+ // 返回重定向路径
139
+ return resolve({result: false, path: ssoRedirectRoutePath});
140
+ });
141
+ } catch (error) {
142
+ console.error('SSO 获取 AccessToken 失败:', error);
143
+ // 失败后跳转到登录页(可根据业务调整为无权限页)
144
+ return reject({result: false, path:'/sso/client/sso-login/index'});
145
+ }
146
+ });
100
147
  }
101
148
 
102
149
  /**
@@ -216,7 +263,8 @@ export {
216
263
  getSsoLoginUrl,
217
264
  getSsoAuthUrl,
218
265
  getSsoLogoutUrl,
219
- getSsoAuthRoutePath,
266
+ // getSsoAuthRoutePath,
267
+ handleRouterGuardSsoAuth,
220
268
 
221
269
  getSsoEncrypt,
222
270
  getSsoEncryptList,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yuang-framework-ui-common",
3
- "version": "1.0.95",
3
+ "version": "1.0.96",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "scripts": {
@@ -1,20 +1,12 @@
1
1
  import {createRouter, createWebHistory} from 'vue-router'
2
2
  import {getExceptionRoutes} from '../../lib/utils/routerUtils'
3
3
 
4
- import { getSsoAccessToken, getSsoAuthRoutePath } from '../../lib/utils/ssoUtils';
4
+ import { getSsoAccessToken, getSsoAuthRoutePath, handleSsoRouterGuardAuth } from '../../lib/utils/ssoUtils';
5
5
 
6
6
 
7
7
  const router = createRouter({
8
8
  history: createWebHistory(import.meta.env.BASE_URL),
9
9
  routes: [
10
- {
11
- path: '/sso/client/sso-auth/index',
12
- component: () => import('@/views/sso/client/sso-auth/index.vue'),
13
- meta: {
14
- title: '认证',
15
- isSsoClientExcluded: true
16
- }
17
- },
18
10
  {
19
11
  path: '/sso/client/sso-auth/auth-success',
20
12
  component: () => import('@/views/sso/client/sso-auth/auth-success.vue'),
@@ -70,21 +62,15 @@ const router = createRouter({
70
62
  path: '/example/table/index',
71
63
  component: () => import('@/views/example/table/index.vue')
72
64
  },
73
-
74
65
  ...getExceptionRoutes()
75
66
  ]
76
67
  })
77
68
 
78
69
 
79
70
  /* 路由守卫 */
80
- router.beforeEach(async (to) => {
81
- if (!getSsoAccessToken()) {
82
- if (!to.meta?.isSsoClientExcluded) {
83
- return getSsoAuthRoutePath(to.fullPath);
84
- }
85
- return;
86
- }
87
- return;
71
+ router.beforeEach(async (to, from) => {
72
+ let ssoAuthResult = await handleRouterGuardSsoAuth(to, from);
73
+ return ssoAuthResult.result;
88
74
  });
89
75
 
90
76
 
@@ -1,35 +0,0 @@
1
- <template>
2
- <div>认证中...</div>
3
- </template>
4
-
5
- <script setup lang="ts">
6
- import { getCurrentInstance, onMounted } from 'vue';
7
- import { useRouter } from 'vue-router';
8
-
9
- import { getSsoAuthUrl, setSsoAccessToken } from '../../../../lib/utils/ssoUtils';
10
- import { removeParameter } from '../../../../lib/utils/urlUtils';
11
- import { http } from '../../../../lib/config/httpConfig';
12
-
13
- const router = useRouter();
14
-
15
- const { proxy } = getCurrentInstance() as any;
16
-
17
- onMounted(() => {
18
- const ssoAuthCode = router.currentRoute.value.query.ssoAuthCode;
19
- if (!ssoAuthCode) {
20
- window.location.href = getSsoAuthUrl(window.location.href) || '';
21
- // 这里一定要return,不return还是会继续执行下面的代码
22
- return;
23
- }
24
-
25
- http.get(`/sso-api/client/sso-auth/getSsoAccessToken`, { params: { ssoAuthCode: ssoAuthCode } }).then((res: any) => {
26
- setSsoAccessToken(res.data.data);
27
- let ssoRedirectRoutePath = router.currentRoute.value.query.ssoRedirectRoutePath as string;
28
- ssoRedirectRoutePath = (ssoRedirectRoutePath && decodeURIComponent(ssoRedirectRoutePath)) || ('/sso/client/sso-auth/auth-success' as any);
29
- ssoRedirectRoutePath = removeParameter('ssoAuthCode', ssoRedirectRoutePath) || '';
30
- router.push(ssoRedirectRoutePath);
31
- });
32
- });
33
- </script>
34
-
35
- <style scoped></style>