useSSO.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // 本来应该是后端做,出于工期考虑转到前端
  2. import QueryString from 'qs';
  3. import { useUserStore } from '/@/store/modules/user';
  4. import { useRoute, useRouter } from 'vue-router';
  5. /** 单点登录功能的Hook,该Hook是为了部署在同一局域网内的多套系统之间能够无缝切换 */
  6. export function useSSO() {
  7. const router = useRouter();
  8. const userStore = useUserStore();
  9. const route = useRoute();
  10. /** 启用单点登录功能来跳转新的页面 */
  11. function open(url: string, redirect?: string, target?: string) {
  12. const qs = QueryString.stringify({
  13. username: userStore.userInfo?.username,
  14. // 毫无意义的伪装,但我就是要装一下
  15. id: userStore.getPassword,
  16. redirect,
  17. });
  18. window.open(`${url}?${qs}`, target);
  19. }
  20. /** 用在跳转到的页面上,执行单点登录的逻辑 */
  21. function doSSO() {
  22. if (!route.query) return;
  23. const { username, id, redirect } = route.query;
  24. if (!username || !id) return;
  25. const realPassword = userStore.decryptPassword(id as string);
  26. const params = {
  27. username: username as string,
  28. password: realPassword,
  29. checkKey: new Date().getTime(),
  30. };
  31. userStore.login(params).then(() => {
  32. if (redirect) {
  33. router.push(redirect as string);
  34. }
  35. });
  36. }
  37. return {
  38. /** 启用单点登录功能来跳转新的页面,参数与window.open一致,但需要注意url需要指向登录页 */
  39. open,
  40. /** 用在跳转到的页面上,执行单点登录的逻辑 */
  41. doSSO,
  42. };
  43. }