|
@@ -7,22 +7,35 @@ import { AxiosError } from 'axios';
|
|
|
|
|
|
/** 自动登录功能的Hook,该Hook是为了部署在同一局域网内的多套系统之间能够无缝切换 */
|
|
|
export function useAutoLogin() {
|
|
|
- /** 启用自动登录功能来跳转新的页面 */
|
|
|
- function open(url: string, target?: string) {
|
|
|
+ /** 获取自动登录的url */
|
|
|
+ function getUrl(baseUrl: string, extraQuery: Record<string, string> = {}) {
|
|
|
const userStore = useUserStore();
|
|
|
const qs = QueryString.stringify({
|
|
|
[AUTO_LOGIN_URL_QUERY.key]: AUTO_LOGIN_URL_QUERY.val,
|
|
|
realname: userStore.getUserInfo.realname,
|
|
|
workNo: userStore.getUserInfo.workNo,
|
|
|
+ ...extraQuery,
|
|
|
});
|
|
|
- window.open(`${url}?${qs}`, target);
|
|
|
+ return `${baseUrl}?${qs}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 启用自动登录功能来跳转新的页面 */
|
|
|
+ function open(url: string, target?: string) {
|
|
|
+ window.open(getUrl(url), target);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 判断当前路由是否需要自动登录,本方法是同步方法,可以用于事先判断 */
|
|
|
+ function validateRoute(route: RouteLocationNormalized) {
|
|
|
+ if (route.query && route.query[AUTO_LOGIN_URL_QUERY.key] === AUTO_LOGIN_URL_QUERY.val) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
/** 用在路由守卫里,执行自动登录的逻辑,如果存在符合自动登录标准的query则执行自动登录,返回是否自动登录 */
|
|
|
async function doAutoLogin(route: RouteLocationNormalized): Promise<void> {
|
|
|
- if (!route.query) return;
|
|
|
- // 这部分在外部的路由守卫里写过了
|
|
|
- // if (route.query[AUTO_LOGIN_URL_QUERY.key] !== AUTO_LOGIN_URL_QUERY.val) return;
|
|
|
+ if (!validateRoute(route)) return;
|
|
|
|
|
|
const { realname, workNo } = route.query;
|
|
|
if (!realname || !workNo) return;
|
|
@@ -38,6 +51,9 @@ export function useAutoLogin() {
|
|
|
...params,
|
|
|
goHome: false,
|
|
|
});
|
|
|
+ delete route.query[AUTO_LOGIN_URL_QUERY.key];
|
|
|
+ delete route.query['username'];
|
|
|
+ delete route.query['workNo'];
|
|
|
return;
|
|
|
} catch (e) {
|
|
|
const message = useMessage().createMessage;
|
|
@@ -47,9 +63,13 @@ export function useAutoLogin() {
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
- /** 启用单点登录功能来跳转新的页面,参数与window.open一致,但需要注意url需要指向登录页 */
|
|
|
+ /** 启用单点登录功能来跳转新的页面,参数与window.open一致 */
|
|
|
open,
|
|
|
/** 用在跳转到的页面上,执行单点登录的逻辑 */
|
|
|
doAutoLogin,
|
|
|
+ /** 获取自动登录的url */
|
|
|
+ getUrl,
|
|
|
+ /** 判断当前路由是否需要自动登录,本方法是同步方法,可以用于事先判断 */
|
|
|
+ validateRoute,
|
|
|
};
|
|
|
}
|