mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
feat: 整合登录
This commit is contained in:
@ -6,7 +6,7 @@ import { SetupStoreId } from '@/enum';
|
||||
import { useRouterPush } from '@/hooks/common/router';
|
||||
import { fetchGetUserInfo, fetchLogin } from '@/service/api';
|
||||
import { localStg } from '@/utils/storage';
|
||||
import { $t } from '@/locales';
|
||||
// import { $t } from '@/locales';
|
||||
import { useRouteStore } from '../route';
|
||||
import { useTabStore } from '../tab';
|
||||
import { clearAuthStorage, getToken } from './shared';
|
||||
@ -21,10 +21,9 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
const token = ref(getToken());
|
||||
|
||||
const userInfo: Api.Auth.UserInfo = reactive({
|
||||
userId: '',
|
||||
userName: '',
|
||||
user: undefined,
|
||||
roles: [],
|
||||
buttons: []
|
||||
permissions: []
|
||||
});
|
||||
|
||||
/** is super role in static route */
|
||||
@ -56,14 +55,20 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
/**
|
||||
* Login
|
||||
*
|
||||
* @param userName User name
|
||||
* @param password Password
|
||||
* @param [redirect=true] Whether to redirect after login. Default is `true`
|
||||
*/
|
||||
async function login(userName: string, password: string, redirect = true) {
|
||||
async function login(loginForm: Api.Auth.LoginForm, redirect = true) {
|
||||
startLoading();
|
||||
|
||||
const { data: loginToken, error } = await fetchLogin(userName, password);
|
||||
const { VITE_APP_CLIENT_ID } = import.meta.env;
|
||||
|
||||
const loginData: Api.Auth.LoginData = {
|
||||
...loginForm,
|
||||
clientId: VITE_APP_CLIENT_ID!,
|
||||
grantType: 'password'
|
||||
};
|
||||
|
||||
const { data: loginToken, error } = await fetchLogin(loginData);
|
||||
|
||||
if (!error) {
|
||||
const pass = await loginByToken(loginToken);
|
||||
@ -76,11 +81,11 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
}
|
||||
|
||||
if (routeStore.isInitAuthRoute) {
|
||||
window.$notification?.success({
|
||||
title: $t('page.login.common.loginSuccess'),
|
||||
content: $t('page.login.common.welcomeBack', { userName: userInfo.userName }),
|
||||
duration: 4500
|
||||
});
|
||||
// window.$notification?.success({
|
||||
// title: $t('page.login.common.loginSuccess'),
|
||||
// content: $t('page.login.common.welcomeBack', { userName: userInfo.userName }),
|
||||
// duration: 4500
|
||||
// });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -88,18 +93,20 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
}
|
||||
|
||||
endLoading();
|
||||
|
||||
return error ? Promise.reject(error) : Promise.resolve();
|
||||
}
|
||||
|
||||
async function loginByToken(loginToken: Api.Auth.LoginToken) {
|
||||
// 1. stored in the localStorage, the later requests need it in headers
|
||||
localStg.set('token', loginToken.token);
|
||||
localStg.set('refreshToken', loginToken.refreshToken);
|
||||
localStg.set('token', loginToken.access_token);
|
||||
localStg.set('refreshToken', loginToken.refresh_token);
|
||||
|
||||
// 2. get user info
|
||||
const pass = await getUserInfo();
|
||||
|
||||
if (pass) {
|
||||
token.value = loginToken.token;
|
||||
token.value = loginToken.access_token;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
43
src/store/modules/notice/index.ts
Normal file
43
src/store/modules/notice/index.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { reactive } from 'vue';
|
||||
|
||||
interface NoticeItem {
|
||||
title?: string;
|
||||
read: boolean;
|
||||
message: any;
|
||||
time: string;
|
||||
}
|
||||
|
||||
export const useNoticeStore = defineStore('notice', () => {
|
||||
const state = reactive({
|
||||
notices: [] as NoticeItem[]
|
||||
});
|
||||
|
||||
const addNotice = (notice: NoticeItem) => {
|
||||
state.notices.push(notice);
|
||||
};
|
||||
|
||||
const removeNotice = (notice: NoticeItem) => {
|
||||
state.notices.splice(state.notices.indexOf(notice), 1);
|
||||
};
|
||||
|
||||
// 实现全部已读
|
||||
const readAll = () => {
|
||||
state.notices.forEach((item: any) => {
|
||||
item.read = true;
|
||||
});
|
||||
};
|
||||
|
||||
const clearNotice = () => {
|
||||
state.notices = [];
|
||||
};
|
||||
return {
|
||||
state,
|
||||
addNotice,
|
||||
removeNotice,
|
||||
readAll,
|
||||
clearNotice
|
||||
};
|
||||
});
|
||||
|
||||
export default useNoticeStore;
|
@ -5,10 +5,10 @@ import { useBoolean } from '@sa/hooks';
|
||||
import type { CustomRoute, ElegantConstRoute, LastLevelRouteKey, RouteKey, RouteMap } from '@elegant-router/types';
|
||||
import { SetupStoreId } from '@/enum';
|
||||
import { router } from '@/router';
|
||||
import { createStaticRoutes, getAuthVueRoutes } from '@/router/routes';
|
||||
import { createDynamicRoutes, createStaticRoutes, getAuthVueRoutes } from '@/router/routes';
|
||||
import { ROOT_ROUTE } from '@/router/routes/builtin';
|
||||
import { getRouteName, getRoutePath } from '@/router/elegant/transform';
|
||||
import { fetchGetConstantRoutes, fetchGetUserRoutes, fetchIsRouteExist } from '@/service/api';
|
||||
import { fetchGetRoutes } from '@/service/api';
|
||||
import { useAppStore } from '../app';
|
||||
import { useAuthStore } from '../auth';
|
||||
import { useTabStore } from '../tab';
|
||||
@ -75,6 +75,17 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
authRoutesMap.set(route.name, route);
|
||||
});
|
||||
|
||||
const dynamicRoutes = createDynamicRoutes();
|
||||
|
||||
dynamicRoutes.authRoutes.forEach(route => {
|
||||
const parent = authRoutesMap.get(route.name);
|
||||
if (parent && route.children) {
|
||||
parent.children?.push(...route.children);
|
||||
return;
|
||||
}
|
||||
authRoutesMap.set(route.name, route);
|
||||
});
|
||||
|
||||
authRoutes.value = Array.from(authRoutesMap.values());
|
||||
}
|
||||
|
||||
@ -201,14 +212,7 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
if (authRouteMode.value === 'static') {
|
||||
addConstantRoutes(staticRoute.constantRoutes);
|
||||
} else {
|
||||
const { data, error } = await fetchGetConstantRoutes();
|
||||
|
||||
if (!error) {
|
||||
addConstantRoutes(data);
|
||||
} else {
|
||||
// if fetch constant routes failed, use static constant routes
|
||||
addConstantRoutes(staticRoute.constantRoutes);
|
||||
}
|
||||
addConstantRoutes(staticRoute.constantRoutes);
|
||||
}
|
||||
|
||||
handleConstantAndAuthRoutes();
|
||||
@ -246,18 +250,16 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
|
||||
/** Init dynamic auth route */
|
||||
async function initDynamicAuthRoute() {
|
||||
const { data, error } = await fetchGetUserRoutes();
|
||||
const { data, error } = await fetchGetRoutes();
|
||||
|
||||
if (!error) {
|
||||
const { routes, home } = data;
|
||||
|
||||
addAuthRoutes(routes);
|
||||
addAuthRoutes(data);
|
||||
|
||||
handleConstantAndAuthRoutes();
|
||||
|
||||
setRouteHome(home);
|
||||
setRouteHome('home');
|
||||
|
||||
handleUpdateRootRouteRedirect(home);
|
||||
handleUpdateRootRouteRedirect('home');
|
||||
|
||||
setIsInitAuthRoute(true);
|
||||
} else {
|
||||
@ -335,14 +337,8 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (authRouteMode.value === 'static') {
|
||||
const { authRoutes: staticAuthRoutes } = createStaticRoutes();
|
||||
return isRouteExistByRouteName(routeName, staticAuthRoutes);
|
||||
}
|
||||
|
||||
const { data } = await fetchIsRouteExist(routeName);
|
||||
|
||||
return data;
|
||||
const { authRoutes: staticAuthRoutes } = createStaticRoutes();
|
||||
return isRouteExistByRouteName(routeName, staticAuthRoutes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user