useSso.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. const locationUrl = 'http://' + window.location.host + '/';
  11. /**
  12. * 单点登录
  13. */
  14. async function ssoLogin() {
  15. if (openSso == 'true') {
  16. const token = getToken();
  17. const ticket = getUrlParam('ticket');
  18. if (!token) {
  19. if (ticket) {
  20. await validateCasLogin({
  21. ticket: ticket,
  22. service: locationUrl,
  23. })
  24. .then((res) => {
  25. const userStore = useUserStore();
  26. userStore.setToken(res.token);
  27. return userStore.afterLoginAction(true, {});
  28. })
  29. .catch(() => {
  30. window.location.href = globSetting.casBaseUrl + '/login?service=' + encodeURIComponent(locationUrl);
  31. });
  32. } else {
  33. window.location.href = globSetting.casBaseUrl + '/login?service=' + encodeURIComponent(locationUrl);
  34. }
  35. }
  36. }
  37. }
  38. /**
  39. * 退出登录
  40. */
  41. async function ssoLoginOut() {
  42. window.location.href = globSetting.casBaseUrl + '/logout?service=' + encodeURIComponent(locationUrl);
  43. }
  44. return { ssoLogin, ssoLoginOut };
  45. }