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

155 lines
4.0 KiB
TypeScript
Raw Normal View History

import { unref, nextTick } from 'vue';
import { defineStore } from 'pinia';
import { router } from '@/router';
2022-01-05 01:35:32 +08:00
import { fetchLogin, fetchUserInfo } from '@/service';
2022-07-16 00:13:19 +08:00
import { useRouterPush } from '@/composables';
import { localStg } from '@/utils';
import { $t } from '@/locales';
import { useTabStore } from '../tab';
import { useRouteStore } from '../route';
import { getToken, getUserInfo, clearAuthStorage } from './helpers';
interface AuthState {
/** 用户信息 */
userInfo: Auth.UserInfo;
/** 用户token */
token: string;
2022-01-05 01:35:32 +08:00
/** 登录的加载状态 */
2022-04-13 23:45:15 +08:00
loginLoading: boolean;
}
export const useAuthStore = defineStore('auth-store', {
state: (): AuthState => ({
userInfo: getUserInfo(),
token: getToken(),
2022-04-13 23:45:15 +08:00
loginLoading: false
}),
getters: {
/** 是否登录 */
isLogin(state) {
return Boolean(state.token);
}
},
actions: {
/** 重置auth状态 */
resetAuthStore() {
const { toLogin } = useRouterPush(false);
const { resetTabStore } = useTabStore();
const { resetRouteStore } = useRouteStore();
const route = unref(router.currentRoute);
clearAuthStorage();
this.$reset();
if (route.meta.requiresAuth) {
toLogin();
}
nextTick(() => {
resetTabStore();
resetRouteStore();
});
},
/**
*
* @param backendToken - token
*/
async handleActionAfterLogin(backendToken: ApiAuth.Token) {
const route = useRouteStore();
const { toLoginRedirect } = useRouterPush(false);
const loginSuccess = await this.loginByToken(backendToken);
if (loginSuccess) {
await route.initAuthRoute();
// 跳转登录后的地址
toLoginRedirect();
// 登录成功弹出欢迎提示
if (route.isInitAuthRoute) {
window.$notification?.success({
title: $t('page.login.common.loginSuccess'),
content: $t('page.login.common.welcomeBack', { userName: this.userInfo.userName }),
duration: 3000
});
}
return;
}
// 不成功则重置状态
this.resetAuthStore();
},
/**
* token进行登录
* @param backendToken - token
*/
async loginByToken(backendToken: ApiAuth.Token) {
let successFlag = false;
2022-01-05 01:35:32 +08:00
// 先把token存储到缓存中(后面接口的请求头需要token)
const { token, refreshToken } = backendToken;
localStg.set('token', token);
localStg.set('refreshToken', refreshToken);
2022-01-05 01:35:32 +08:00
// 获取用户信息
const { data } = await fetchUserInfo();
if (data) {
// 成功后把用户信息存储到缓存中
localStg.set('userInfo', data);
2022-01-05 01:35:32 +08:00
// 更新状态
2022-05-09 20:46:27 +08:00
this.userInfo = data;
this.token = token;
2022-01-05 01:35:32 +08:00
successFlag = true;
}
return successFlag;
},
/**
*
* @param userName -
* @param password -
*/
async login(userName: string, password: string) {
2022-04-13 23:45:15 +08:00
this.loginLoading = true;
const { data } = await fetchLogin(userName, password);
if (data) {
await this.handleActionAfterLogin(data);
}
2022-04-13 23:45:15 +08:00
this.loginLoading = false;
},
/**
* ()
* @param userRole
*/
async updateUserRole(userRole: Auth.RoleType) {
const { resetRouteStore, initAuthRoute } = useRouteStore();
const accounts: Record<Auth.RoleType, { userName: string; password: string }> = {
super: {
userName: 'Super',
password: 'super123'
},
admin: {
userName: 'Admin',
password: 'admin123'
},
user: {
userName: 'User01',
password: 'user01123'
}
};
const { userName, password } = accounts[userRole];
const { data } = await fetchLogin(userName, password);
if (data) {
await this.loginByToken(data);
resetRouteStore();
initAuthRoute();
}
}
}
});