-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7677af5
commit 486dec4
Showing
12 changed files
with
207 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useRouter } from 'vue-router'; | ||
import type { RouteLocationRaw } from 'vue-router'; | ||
import type { RouteKey } from '@elegant-router/types'; | ||
import { router as globalRouter } from '@/router'; | ||
|
||
/** | ||
* router push | ||
* @description jump to the specified route, it can replace function router.push | ||
* @param inSetup | ||
*/ | ||
export function useRouterPush(inSetup = true) { | ||
const router = inSetup ? useRouter() : globalRouter; | ||
const route = globalRouter.currentRoute; | ||
|
||
const routerPush = router.push; | ||
|
||
const routerBack = router.back; | ||
|
||
interface RouterPushOptions { | ||
query?: Record<string, string>; | ||
params?: Record<string, string>; | ||
} | ||
|
||
async function routerPushByKey(key: RouteKey, options?: RouterPushOptions) { | ||
const { query, params } = options || {}; | ||
|
||
const routeLocation: RouteLocationRaw = { | ||
name: key | ||
}; | ||
|
||
if (query) { | ||
routeLocation.query = query; | ||
} | ||
|
||
if (params) { | ||
routeLocation.params = params; | ||
} | ||
|
||
return routerPush(routeLocation); | ||
} | ||
|
||
/** | ||
* navigate to login page | ||
* @param loginModule the login module | ||
* @param redirectUrl the redirect url, if not specified, it will be the current route fullPath | ||
*/ | ||
async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) { | ||
const module = loginModule || 'pwd-login'; | ||
|
||
const options: RouterPushOptions = { | ||
params: { | ||
module | ||
} | ||
}; | ||
|
||
const redirect = redirectUrl || route.value.fullPath; | ||
|
||
options.query = { | ||
redirect | ||
}; | ||
|
||
return routerPushByKey('login', options); | ||
} | ||
|
||
return { | ||
route, | ||
routerPush, | ||
routerBack, | ||
routerPushByKey, | ||
toLogin | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ref } from 'vue'; | ||
import { defineStore } from 'pinia'; | ||
import { useLoading } from '@sa/hooks'; | ||
import { SetupStoreId } from '@/enum'; | ||
import { useRouterPush } from '@/hooks/common/router'; | ||
import { localStg } from '@/utils/storage'; | ||
|
||
export const useAuthStore = defineStore(SetupStoreId.Auth, () => { | ||
const { route, toLogin } = useRouterPush(false); | ||
const { loading: loginLoading, startLoading, endLoading } = useLoading(); | ||
|
||
const token = ref(localStg.get('token') || ''); | ||
|
||
async function resetStore() { | ||
const auth = useAuthStore(); | ||
|
||
localStg.remove('token'); | ||
|
||
auth.$reset(); | ||
|
||
if (route.value.meta.requiresAuth) { | ||
toLogin(); | ||
} | ||
} | ||
|
||
async function login() { | ||
startLoading(); | ||
|
||
endLoading(); | ||
} | ||
|
||
return { | ||
token, | ||
loginLoading, | ||
resetStore, | ||
login | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { defineStore } from 'pinia'; | ||
import { SetupStoreId } from '@/enum'; | ||
|
||
export const routeStore = defineStore(SetupStoreId.Route, () => { | ||
export const useRouteStore = defineStore(SetupStoreId.Route, () => { | ||
// | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'vue-router'; | ||
|
||
declare module 'vue-router' { | ||
interface RouteMeta { | ||
/** | ||
* title of the route | ||
* @description it can be used in document title | ||
*/ | ||
title: string; | ||
/** | ||
* i18n key of the route | ||
* @description it's used in i18n, if it is set, the title will be ignored | ||
*/ | ||
i18nKey?: App.I18n.I18nKey; | ||
/** | ||
* whether to require authentication | ||
*/ | ||
requiresAuth?: boolean; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters