| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- // 单点登录核心类
- import { getToken } from '/@/utils/auth';
- import { getUrlParam } from '/@/utils';
- import { useGlobSetting } from '/@/hooks/setting';
- import { validateCasLogin } from '/@/api/sys/user';
- import { useUserStore } from '/@/store/modules/user';
- const globSetting = useGlobSetting();
- const openSso = globSetting.openSso;
- export function useSso() {
- const locationUrl = 'http://' + window.location.host + '/';
- /**
- * 单点登录,返回是否需要跳转到单点登录页
- */
- async function ssoLogin() {
- let redirect = false;
- if (openSso == 'true') {
- const token = getToken();
- const ticket = getUrlParam('ticket');
- if (!token) {
- if (ticket) {
- try {
- const res = await validateCasLogin({
- ticket: ticket,
- service: locationUrl,
- });
- const userStore = useUserStore();
- userStore.setToken(res.token);
- redirect = true;
- await userStore.afterLoginAction(true, {});
- } catch (e) {
- redirect = true;
- window.location.href = globSetting.casBaseUrl + '/login?service=' + encodeURIComponent(locationUrl);
- }
- } else {
- if (window.location.search == '?type=noCas') {
- } else {
- window.location.href = globSetting.casBaseUrl + '/login?service=' + encodeURIComponent(locationUrl);
- redirect = true;
- }
- }
- }
- }
- return redirect;
- }
- /**
- * 退出登录
- */
- async function ssoLoginOut() {
- window.location.href = globSetting.casBaseUrl + '/logout?service=' + encodeURIComponent(locationUrl);
- }
- return { ssoLogin, ssoLoginOut };
- }
|