ruoyi-plus-soybean/src/store/modules/auth/index.ts

140 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-12-14 21:45:29 +08:00
import { computed, reactive, ref } from 'vue';
2024-04-16 00:03:00 +08:00
import { useRoute } from 'vue-router';
import { defineStore } from 'pinia';
2023-11-17 08:45:00 +08:00
import { useLoading } from '@sa/hooks';
import { SetupStoreId } from '@/enum';
import { useRouterPush } from '@/hooks/common/router';
2023-12-14 21:45:29 +08:00
import { fetchGetUserInfo, fetchLogin } from '@/service/api';
2023-11-17 08:45:00 +08:00
import { localStg } from '@/utils/storage';
import { $t } from '@/locales';
2023-12-14 21:45:29 +08:00
import { useRouteStore } from '../route';
2024-06-15 00:49:24 +08:00
import { useTabStore } from '../tab';
import { clearAuthStorage, getToken } from './shared';
2023-11-17 08:45:00 +08:00
export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
2024-04-16 00:03:00 +08:00
const route = useRoute();
2023-11-17 08:45:00 +08:00
const routeStore = useRouteStore();
const tabStore = useTabStore();
const { toLogin, redirectFromLogin } = useRouterPush(false);
2023-11-17 08:45:00 +08:00
const { loading: loginLoading, startLoading, endLoading } = useLoading();
const token = ref(getToken());
const userInfo: Api.Auth.UserInfo = reactive({
userId: '',
userName: '',
roles: [],
buttons: []
});
2023-11-17 08:45:00 +08:00
2024-03-24 15:39:41 +08:00
/** 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);
});
2023-12-14 21:45:29 +08:00
/** Is login */
2023-11-17 08:45:00 +08:00
const isLogin = computed(() => Boolean(token.value));
2023-12-14 21:45:29 +08:00
/** Reset auth store */
2023-11-17 08:45:00 +08:00
async function resetStore() {
const authStore = useAuthStore();
clearAuthStorage();
authStore.$reset();
if (!route.meta.constant) {
2023-11-17 08:45:00 +08:00
await toLogin();
}
tabStore.cacheTabs();
2023-11-17 08:45:00 +08:00
routeStore.resetStore();
}
2023-11-17 08:45:00 +08:00
/**
2023-12-14 21:45:29 +08:00
* Login
*
* @param userName User name
* @param password Password
2024-03-24 15:39:41 +08:00
* @param [redirect=true] Whether to redirect after login. Default is `true`
2023-11-17 08:45:00 +08:00
*/
2024-03-24 15:39:41 +08:00
async function login(userName: string, password: string, redirect = true) {
2023-11-17 08:45:00 +08:00
startLoading();
const { data: loginToken, error } = await fetchLogin(userName, password);
2023-11-17 08:45:00 +08:00
if (!error) {
const pass = await loginByToken(loginToken);
2023-11-17 08:45:00 +08:00
if (pass) {
2024-08-23 10:50:03 +08:00
await redirectFromLogin(redirect);
2023-11-17 08:45:00 +08:00
window.$notification?.success({
title: $t('page.login.common.loginSuccess'),
content: $t('page.login.common.welcomeBack', { userName: userInfo.userName }),
duration: 4500
});
}
} else {
2023-11-17 08:45:00 +08:00
resetStore();
}
endLoading();
}
2023-11-17 08:45:00 +08:00
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);
// 2. get user info
const pass = await getUserInfo();
if (pass) {
token.value = loginToken.token;
return true;
}
return false;
}
async function getUserInfo() {
const { data: info, error } = await fetchGetUserInfo();
if (!error) {
// update store
Object.assign(userInfo, info);
return true;
}
2023-11-17 08:45:00 +08:00
return false;
2023-11-17 08:45:00 +08:00
}
async function initUserInfo() {
const hasToken = getToken();
if (hasToken) {
const pass = await getUserInfo();
if (!pass) {
resetStore();
}
}
}
2023-11-17 08:45:00 +08:00
return {
token,
userInfo,
2024-03-24 15:39:41 +08:00
isStaticSuper,
2023-11-17 08:45:00 +08:00
isLogin,
loginLoading,
resetStore,
login,
initUserInfo
2023-11-17 08:45:00 +08:00
};
});