useSSO.ts 1.4 KB

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