123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- // 本来应该是后端做,出于工期考虑转到前端
- import QueryString from 'qs';
- import { useUserStore } from '/@/store/modules/user';
- import { useRoute, useRouter } from 'vue-router';
- /** 单点登录功能的Hook,该Hook是为了部署在同一局域网内的多套系统之间能够无缝切换 */
- export function useSSO() {
- const router = useRouter();
- const userStore = useUserStore();
- const route = useRoute();
- /** 启用单点登录功能来跳转新的页面 */
- function open(url: string, redirect?: string, target?: string) {
- const qs = QueryString.stringify({
- username: userStore.userInfo?.username,
- // 毫无意义的伪装,但我就是要装一下
- id: userStore.getPassword,
- redirect,
- });
- window.open(`${url}?${qs}`, target);
- }
- /** 用在跳转到的页面上,执行单点登录的逻辑 */
- function doSSO() {
- if (!route.query) return;
- const { username, id, redirect } = route.query;
- if (!username || !id) return;
- const realPassword = userStore.decryptPassword(id as string);
- const params = {
- username: username as string,
- password: realPassword,
- checkKey: new Date().getTime(),
- };
- userStore.login(params).then(() => {
- if (redirect) {
- router.push(redirect as string);
- }
- });
- }
- return {
- /** 启用单点登录功能来跳转新的页面,参数与window.open一致,但需要注意url需要指向登录页 */
- open,
- /** 用在跳转到的页面上,执行单点登录的逻辑 */
- doSSO,
- };
- }
|