useSso.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // 单点登录核心类
  2. import { getToken } from '/@/utils/auth';
  3. import { getUrlParam } from '/@/utils';
  4. import { useGlobSetting } from '/@/hooks/setting';
  5. import { validateCasLogin } from '/@/api/sys/user';
  6. import { useUserStore } from '/@/store/modules/user';
  7. const globSetting = useGlobSetting();
  8. const openSso = globSetting.openSso;
  9. export function useSso() {
  10. //update-begin---author:wangshuai---date:2024-01-03---for:【QQYUN-7805】SSO登录强制用http #957---
  11. let locationUrl = document.location.protocol +"//" + window.location.host + '/';
  12. //update-end---author:wangshuai---date:2024-01-03---for:【QQYUN-7805】SSO登录强制用http #957---
  13. /**
  14. * 单点登录
  15. */
  16. async function ssoLogin() {
  17. if (openSso == 'true') {
  18. let token = getToken();
  19. let ticket = getUrlParam('ticket');
  20. if (!token) {
  21. if (ticket) {
  22. await validateCasLogin({
  23. ticket: ticket,
  24. service: locationUrl,
  25. }).then((res) => {
  26. const userStore = useUserStore();
  27. userStore.setToken(res.token);
  28. return userStore.afterLoginAction(true, {});
  29. });
  30. } else {
  31. window.location.href = globSetting.casBaseUrl + '/login?service=' + encodeURIComponent(locationUrl);
  32. }
  33. }
  34. }
  35. }
  36. /**
  37. * 退出登录
  38. */
  39. async function ssoLoginOut() {
  40. window.location.href = globSetting.casBaseUrl + '/logout?service=' + encodeURIComponent(locationUrl);
  41. }
  42. return { ssoLogin, ssoLoginOut };
  43. }