perf: 合并用户平台框架修改
This commit is contained in:
@@ -7,7 +7,7 @@ import { localStg } from '@/utils/storage';
|
||||
import { $t } from '@/locales';
|
||||
import { useRouteStore } from '../route';
|
||||
import { clearAuthStorage, emptyInfo, getToken } from './shared';
|
||||
import {sendCaptcha} from "@/service/api/auth";
|
||||
import { doCheckUserRepeat, sendCaptcha } from '@/service/api/auth';
|
||||
|
||||
export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
const routeStore = useRouteStore();
|
||||
@@ -31,13 +31,6 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
}
|
||||
);
|
||||
|
||||
/** is super role in static route */
|
||||
const isStaticSuper = computed(() => {
|
||||
const { VITE_AUTH_ROUTE_MODE, VITE_STATIC_SUPER_ROLE } = import.meta.env;
|
||||
|
||||
return VITE_AUTH_ROUTE_MODE === 'static' && userInfo.roles?.includes(VITE_STATIC_SUPER_ROLE);
|
||||
});
|
||||
|
||||
/** Is login */
|
||||
const isLogin = computed(() => Boolean(token.value));
|
||||
|
||||
@@ -122,6 +115,15 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户信息是否已存在
|
||||
*/
|
||||
async function checkUserRepeat(checkForm: Api.Auth.CheckBody) {
|
||||
const { data, error } = await doCheckUserRepeat(checkForm);
|
||||
return { exists: data, error };
|
||||
}
|
||||
|
||||
/**
|
||||
* Register new user
|
||||
*/
|
||||
@@ -139,22 +141,18 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
endLoading();
|
||||
return !error;
|
||||
}
|
||||
async function captcha(email:string){
|
||||
|
||||
async function captcha(email: string) {
|
||||
if (!email) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await sendCaptcha({ email }); // 这里调用后端接口发送验证码
|
||||
} catch (error) {
|
||||
|
||||
return null;
|
||||
}
|
||||
const { data, error } = await sendCaptcha({ email }); // 这里调用后端接口发送验证码
|
||||
return { data, error };
|
||||
}
|
||||
|
||||
return {
|
||||
token,
|
||||
userInfo,
|
||||
isStaticSuper,
|
||||
isLogin,
|
||||
loginLoading,
|
||||
resetStore,
|
||||
@@ -163,5 +161,6 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
refreshUserInfo,
|
||||
register,
|
||||
captcha,
|
||||
checkUserRepeat
|
||||
};
|
||||
});
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { computed, ref, shallowRef } from 'vue';
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
import { defineStore } from 'pinia';
|
||||
import { useBoolean } from '@sa/hooks';
|
||||
import type { CustomRoute, ElegantConstRoute, LastLevelRouteKey, RouteKey } from '@elegant-router/types';
|
||||
import { SetupStoreId } from '@/enum';
|
||||
import { router } from '@/router';
|
||||
import { createStaticRoutes, getAuthVueRoutes } from '@/router/routes';
|
||||
import { ROOT_ROUTE } from '@/router/routes/builtin';
|
||||
import { getRoutePath } from '@/router/elegant/transform';
|
||||
import { useAppStore } from '../app';
|
||||
import { useAuthStore } from '../auth';
|
||||
import { useTabStore } from '../tab';
|
||||
import {computed, ref, shallowRef} from 'vue';
|
||||
import type {RouteRecordRaw} from 'vue-router';
|
||||
import {defineStore} from 'pinia';
|
||||
import {useBoolean} from '@sa/hooks';
|
||||
import type {CustomRoute, ElegantConstRoute, LastLevelRouteKey, RouteKey} from '@elegant-router/types';
|
||||
import {SetupStoreId} from '@/enum';
|
||||
import {router} from '@/router';
|
||||
import {createStaticRoutes, getAuthVueRoutes} from '@/router/routes';
|
||||
import {ROOT_ROUTE} from '@/router/routes/builtin';
|
||||
import {getRoutePath} from '@/router/elegant/transform';
|
||||
import {useAppStore} from '../app';
|
||||
import {useAuthStore} from '../auth';
|
||||
import {useTabStore} from '../tab';
|
||||
import {
|
||||
filterAuthRoutesByRoles,
|
||||
getBreadcrumbsByRoute,
|
||||
@@ -26,8 +26,8 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
const appStore = useAppStore();
|
||||
const authStore = useAuthStore();
|
||||
const tabStore = useTabStore();
|
||||
const { bool: isInitConstantRoute, setBool: setIsInitConstantRoute } = useBoolean();
|
||||
const { bool: isInitAuthRoute, setBool: setIsInitAuthRoute } = useBoolean();
|
||||
const {bool: isInitConstantRoute, setBool: setIsInitConstantRoute} = useBoolean();
|
||||
const {bool: isInitAuthRoute, setBool: setIsInitAuthRoute} = useBoolean();
|
||||
|
||||
|
||||
/** Home route key */
|
||||
@@ -146,17 +146,9 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
async function initConstantRoute() {
|
||||
if (isInitConstantRoute.value) return;
|
||||
|
||||
// if (authRouteMode.value === 'static') {
|
||||
const { constantRoutes } = createStaticRoutes();
|
||||
const {constantRoutes} = createStaticRoutes();
|
||||
|
||||
addAuthRoutes(constantRoutes);
|
||||
// } else {
|
||||
// const { data, error } = await fetchGetConstantRoutes();
|
||||
|
||||
// if (!error) {
|
||||
// addAuthRoutes(data);
|
||||
// }
|
||||
// }
|
||||
|
||||
handleAuthRoutes();
|
||||
|
||||
@@ -165,43 +157,24 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
|
||||
/** Init auth route */
|
||||
async function initAuthRoute() {
|
||||
await initStaticAuthRoute();
|
||||
await initDynamicAuthRoute();
|
||||
tabStore.initHomeTab();
|
||||
}
|
||||
|
||||
/** Init static auth route */
|
||||
async function initStaticAuthRoute() {
|
||||
const { authRoutes: staticAuthRoutes } = createStaticRoutes();
|
||||
if (authStore.isStaticSuper) {
|
||||
addAuthRoutes(staticAuthRoutes);
|
||||
} else {
|
||||
const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []);
|
||||
|
||||
addAuthRoutes(filteredAuthRoutes);
|
||||
}
|
||||
|
||||
handleAuthRoutes();
|
||||
|
||||
setIsInitAuthRoute(true);
|
||||
}
|
||||
|
||||
/** Init dynamic auth route */
|
||||
async function initDynamicAuthRoute() {
|
||||
const { data: routes, error } = await doGetUserRoutes();
|
||||
const {data: routes, error} = await doGetUserRoutes();
|
||||
if (!error) {
|
||||
addAuthRoutes(routes);
|
||||
const {authRoutes: staticAuthRoutes} = createStaticRoutes();
|
||||
const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []);
|
||||
addAuthRoutes(filteredAuthRoutes.concat(routes));
|
||||
|
||||
handleAuthRoutes();
|
||||
|
||||
setRouteHome('home');
|
||||
|
||||
handleUpdateRootRouteRedirect('manage_role');
|
||||
handleUpdateRootRouteRedirect('home');
|
||||
|
||||
setIsInitAuthRoute(true);
|
||||
} else {
|
||||
await authStore.resetStore();
|
||||
}
|
||||
tabStore.initHomeTab();
|
||||
}
|
||||
|
||||
/** handle auth routes */
|
||||
@@ -249,7 +222,7 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
const redirect = getRoutePath(redirectKey);
|
||||
|
||||
if (redirect) {
|
||||
const rootRoute: CustomRoute = { ...ROOT_ROUTE, redirect };
|
||||
const rootRoute: CustomRoute = {...ROOT_ROUTE, redirect};
|
||||
|
||||
router.removeRoute(rootRoute.name);
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ function sortRouteByOrder(route: ElegantConstRoute) {
|
||||
* @param routes routes
|
||||
*/
|
||||
export function sortRoutesByOrder(routes: ElegantConstRoute[]) {
|
||||
routes.sort((next, prev) => (Number(next.meta?.order) || 0) - (Number(prev.meta?.order) || 0));
|
||||
routes.sort((next, prev) => (Number(next.meta?.order) || 1) - (Number(prev.meta?.order) || 1));
|
||||
routes.forEach(sortRouteByOrder);
|
||||
|
||||
return routes;
|
||||
|
||||
Reference in New Issue
Block a user