import { queenApi } from "queenjs"; import { AuthModule } from ".."; export const authHelper = AuthModule.helper({ isLoginPage() { if (this.config.singlePage) { const [loginPath] = this.config.loginPath.split("#"); return location.href.indexOf(loginPath) > 0; } else { return queenApi.router.currentRoute.value.name === "login"; } }, isCurrPathNeedAuth() { return queenApi.router.currentRoute.value.meta.needAuth; }, getCurrPath() { return queenApi.router.currentRoute.value.fullPath; }, getRedirectPath() { if (this.config.singlePage) { const params = new URLSearchParams(location.search); return decodeURIComponent(params.get("redirect") || ""); } else { return decodeURIComponent( (queenApi.router.currentRoute.value.query.redirect as string) || "" ); } }, createBgStyle(bgConfig: any = {}) { const bgStyle: any = {}; for (const name in bgConfig) { const value = bgConfig[name]; bgStyle["background-" + name] = name === "image" ? `url(${value})` : value; } return bgStyle; }, linkTo(path: string, options?: { redirect?: boolean; replace?: boolean }) { if (this.config.singlePage) { if (options?.redirect) path = path + "?redirect=" + encodeURIComponent(location.href); if (options?.replace) { location.replace(path); } else { location.href = path; } } else { const route = { path, query: { redirect: options?.redirect ? this.helper.getCurrPath() : undefined, }, }; if (options?.replace) { queenApi.router.replace(route); } else { queenApi.router.push(route); } } }, });