auth.ts 821 B

1234567891011121314151617181920212223242526272829303132
  1. const TAB_BAR_PAGES = ["/pages/map/map", "/pages/user/user"];
  2. export function isAuthenticated(): boolean {
  3. return !!getApp<any>().globalData.token;
  4. }
  5. export function isTabBarPage(path: string): boolean {
  6. if (!path) return false;
  7. return TAB_BAR_PAGES.some((p) => path.indexOf(p) >= 0);
  8. }
  9. export function requireAuth(redirectUrl?: string): boolean {
  10. if (isAuthenticated()) {
  11. return true;
  12. }
  13. const pages = getCurrentPages();
  14. const currentPage = pages[pages.length - 1];
  15. if (currentPage && currentPage.route === "pages/login/login") {
  16. return false;
  17. }
  18. const redirect = redirectUrl
  19. ? `/${redirectUrl}`.replace(/^\/+/, "/")
  20. : `/${currentPage?.route || "pages/map/map"}`;
  21. uni.navigateTo({
  22. url: `/pages/login/login?redirect=${encodeURIComponent(redirect)}`,
  23. });
  24. return false;
  25. }