useSso.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. let redirect = false;
  16. if (openSso == 'true') {
  17. const token = getToken();
  18. const ticket = getUrlParam('ticket');
  19. if (!token) {
  20. if (ticket) {
  21. try {
  22. const res = await validateCasLogin({
  23. ticket: ticket,
  24. service: locationUrl,
  25. });
  26. const userStore = useUserStore();
  27. userStore.setToken(res.token);
  28. redirect = true;
  29. await userStore.afterLoginAction(true, {});
  30. } catch (e) {
  31. redirect = true;
  32. window.location.href = globSetting.casBaseUrl + '/login?service=' + encodeURIComponent(locationUrl);
  33. }
  34. } else {
  35. if (window.location.search == '?type=noCas') {
  36. } else {
  37. window.location.href = globSetting.casBaseUrl + '/login?service=' + encodeURIComponent(locationUrl);
  38. redirect = true;
  39. }
  40. }
  41. }
  42. }
  43. return redirect;
  44. }
  45. /**
  46. * 退出登录
  47. */
  48. async function ssoLoginOut() {
  49. window.location.href = globSetting.casBaseUrl + '/logout?service=' + encodeURIComponent(locationUrl);
  50. }
  51. return { ssoLogin, ssoLoginOut };
  52. }