refactor: Separate store

This commit is contained in:
vben
2024-07-30 21:10:28 +08:00
parent cf0ec053e4
commit 89dcf522f5
40 changed files with 338 additions and 345 deletions

View File

@@ -1,12 +1,11 @@
import type { LoginAndRegisterParams } from '@vben/common-ui';
import type { MenuRecordRaw, UserInfo } from '@vben/types';
import type { RouteRecordRaw } from 'vue-router';
import type { UserInfo } from '@vben/types';
import { computed, ref } from 'vue';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { DEFAULT_HOME_PATH, LOGIN_PATH } from '@vben/constants';
import { resetAllStores, useCoreAccessStore } from '@vben/stores';
import { resetAllStores, useAccessStore, useUserStore } from '@vben/stores';
import { notification } from 'ant-design-vue';
import { defineStore } from 'pinia';
@@ -14,31 +13,12 @@ import { defineStore } from 'pinia';
import { getAccessCodes, getUserInfo, login } from '#/api';
import { $t } from '#/locales';
export const useAccessStore = defineStore('access', () => {
const coreStoreAccess = useCoreAccessStore();
export const useAuthStore = defineStore('auth', () => {
const accessStore = useAccessStore();
const userStore = useUserStore();
const router = useRouter();
const loading = ref(false);
const openLoginExpiredModal = ref(false);
const accessToken = computed(() => coreStoreAccess.accessToken);
const refreshToken = computed(() => coreStoreAccess.refreshToken);
const userRoles = computed(() => coreStoreAccess.userRoles);
const userInfo = computed(() => coreStoreAccess.userInfo);
const accessRoutes = computed(() => coreStoreAccess.accessRoutes);
function setAccessMenus(menus: MenuRecordRaw[]) {
coreStoreAccess.setAccessMenus(menus);
}
function setAccessToken(token: null | string) {
coreStoreAccess.setAccessToken(token);
}
function setAccessRoutes(routes: RouteRecordRaw[]) {
coreStoreAccess.setAccessRoutes(routes);
}
const loginLoading = ref(false);
/**
*
@@ -52,19 +32,16 @@ export const useAccessStore = defineStore('access', () => {
// 异步处理用户登录操作并获取 accessToken
let userInfo: null | UserInfo = null;
try {
loading.value = true;
loginLoading.value = true;
const { accessToken, refreshToken } = await login(params);
// 如果成功获取到 accessToken
// If accessToken is successfully obtained
if (accessToken) {
// 将 accessToken 存储到 accessStore 中
// Store the accessToken in accessStore
coreStoreAccess.setAccessToken(accessToken);
coreStoreAccess.setRefreshToken(refreshToken);
accessStore.setAccessToken(accessToken);
accessStore.setRefreshToken(refreshToken);
// 获取用户信息并存储到 accessStore 中
// Get user information and store it in accessStore
const [fetchUserInfoResult, accessCodes] = await Promise.all([
fetchUserInfo(),
getAccessCodes(),
@@ -72,11 +49,11 @@ export const useAccessStore = defineStore('access', () => {
userInfo = fetchUserInfoResult;
coreStoreAccess.setUserInfo(userInfo);
coreStoreAccess.setAccessCodes(accessCodes);
userStore.setUserInfo(userInfo);
accessStore.setAccessCodes(accessCodes);
if (openLoginExpiredModal.value) {
openLoginExpiredModal.value = false;
if (accessStore.loginExpired) {
accessStore.setLoginExpired(false);
} else {
onSuccess
? await onSuccess?.()
@@ -92,18 +69,17 @@ export const useAccessStore = defineStore('access', () => {
}
}
} finally {
loading.value = false;
loginLoading.value = false;
}
return {
accessToken,
userInfo,
};
}
async function logout() {
resetAllStores();
openLoginExpiredModal.value = false;
accessStore.setLoginExpired(false);
// 回登陆页带上当前路由地址
await router.replace({
@@ -117,29 +93,19 @@ export const useAccessStore = defineStore('access', () => {
async function fetchUserInfo() {
let userInfo: null | UserInfo = null;
userInfo = await getUserInfo();
coreStoreAccess.setUserInfo(userInfo);
userStore.setUserInfo(userInfo);
return userInfo;
}
function $reset() {
loading.value = false;
openLoginExpiredModal.value = false;
loginLoading.value = false;
}
return {
$reset,
accessRoutes,
accessToken,
authLogin,
fetchUserInfo,
loading,
loginLoading,
logout,
openLoginExpiredModal,
refreshToken,
setAccessMenus,
setAccessRoutes,
setAccessToken,
userInfo,
userRoles,
};
});

View File

@@ -1,18 +1 @@
import type { InitStoreOptions } from '@vben/stores';
import type { App } from 'vue';
import { initStore, resetAllStores, storeToRefs } from '@vben/stores';
/**
* @zh_CN 初始化pinia
* @param app vue app 实例
*/
async function setupStore(app: App, options: InitStoreOptions) {
const pinia = await initStore(options);
app.use(pinia);
}
export { resetAllStores, setupStore, storeToRefs };
export { useAccessStore } from './modules/access';
export * from './auth';