useSso.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. }).then((res) => {
  24. const userStore = useUserStore();
  25. userStore.setToken(res.token);
  26. return userStore.afterLoginAction(true, {});
  27. });
  28. } else {
  29. window.location.href = globSetting.casBaseUrl + '/login?service=' + encodeURIComponent(locationUrl);
  30. }
  31. }
  32. }
  33. }
  34. /**
  35. * 退出登录
  36. */
  37. async function ssoLoginOut() {
  38. window.location.href = globSetting.casBaseUrl + '/logout?service=' + encodeURIComponent(locationUrl);
  39. }
  40. return { ssoLogin, ssoLoginOut };
  41. }