helper.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { queenApi } from "queenjs";
  2. import { AuthModule } from "..";
  3. export const authHelper = AuthModule.helper({
  4. isLoginPage() {
  5. if (this.config.singlePage) {
  6. const [loginPath] = this.config.loginPath.split("#");
  7. return location.href.indexOf(loginPath) > 0;
  8. } else {
  9. return queenApi.router.currentRoute.value.name === "login";
  10. }
  11. },
  12. isCurrPathNeedAuth() {
  13. return queenApi.router.currentRoute.value.meta.needAuth;
  14. },
  15. getCurrPath() {
  16. return queenApi.router.currentRoute.value.fullPath;
  17. },
  18. getRedirectPath() {
  19. if (this.config.singlePage) {
  20. const params = new URLSearchParams(location.search);
  21. return decodeURIComponent(params.get("redirect") || "");
  22. } else {
  23. return decodeURIComponent(
  24. (queenApi.router.currentRoute.value.query.redirect as string) || ""
  25. );
  26. }
  27. },
  28. createBgStyle(bgConfig: any = {}) {
  29. const bgStyle: any = {};
  30. for (const name in bgConfig) {
  31. const value = bgConfig[name];
  32. bgStyle["background-" + name] =
  33. name === "image" ? `url(${value})` : value;
  34. }
  35. return bgStyle;
  36. },
  37. linkTo(path: string, options?: { redirect?: boolean; replace?: boolean }) {
  38. if (this.config.singlePage) {
  39. if (options?.redirect)
  40. path = path + "?redirect=" + encodeURIComponent(location.href);
  41. if (options?.replace) {
  42. location.replace(path);
  43. } else {
  44. location.href = path;
  45. }
  46. } else {
  47. const route = {
  48. path,
  49. query: {
  50. redirect: options?.redirect ? this.helper.getCurrPath() : undefined,
  51. },
  52. };
  53. if (options?.replace) {
  54. queenApi.router.replace(route);
  55. } else {
  56. queenApi.router.push(route);
  57. }
  58. }
  59. },
  60. });