初始化

This commit is contained in:
2026-02-22 18:56:10 +08:00
commit 26677972a6
3112 changed files with 255972 additions and 0 deletions

View File

@@ -0,0 +1,119 @@
import type { Recordable, UserInfo } from '@easyflow/types';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { LOGIN_PATH } from '@easyflow/constants';
import { preferences } from '@easyflow/preferences';
import { resetAllStores, useAccessStore, useUserStore } from '@easyflow/stores';
import { ElNotification } from 'element-plus';
import { defineStore } from 'pinia';
import { getAccessCodesApi, getUserInfoApi, loginApi, logoutApi } from '#/api';
import { $t } from '#/locales';
export const useAuthStore = defineStore('auth', () => {
const accessStore = useAccessStore();
const userStore = useUserStore();
const router = useRouter();
const loginLoading = ref(false);
/**
* 异步处理登录操作
* Asynchronously handle the login process
* @param params 登录表单数据
*/
async function authLogin(
params: Recordable<any>,
onSuccess?: () => Promise<void> | void,
) {
// 异步处理用户登录操作并获取 accessToken
let userInfo: null | UserInfo = null;
try {
loginLoading.value = true;
const { token: accessToken } = await loginApi(params);
// 如果成功获取到 accessToken
if (accessToken) {
// 将 accessToken 存储到 accessStore 中
accessStore.setAccessToken(accessToken);
// 获取用户信息并存储到 accessStore 中
const [fetchUserInfoResult, accessCodes] = await Promise.all([
fetchUserInfo(),
getAccessCodesApi(),
]);
userInfo = fetchUserInfoResult;
userStore.setUserInfo(userInfo);
accessStore.setAccessCodes(accessCodes);
if (accessStore.loginExpired) {
accessStore.setLoginExpired(false);
} else {
onSuccess
? await onSuccess?.()
: await router.push(
userInfo.homePath || preferences.app.defaultHomePath,
);
}
if (userInfo?.nickname) {
ElNotification({
message: `${$t('authentication.loginSuccessDesc')}:${userInfo?.nickname}`,
title: $t('authentication.loginSuccess'),
type: 'success',
});
}
}
} finally {
loginLoading.value = false;
}
return {
userInfo,
};
}
async function logout(redirect: boolean = true) {
try {
await logoutApi();
} catch {
// 不做任何处理
}
resetAllStores();
accessStore.setLoginExpired(false);
// 回登录页带上当前路由地址
await router.replace({
path: LOGIN_PATH,
query: redirect
? {
redirect: encodeURIComponent(router.currentRoute.value.fullPath),
}
: {},
});
}
async function fetchUserInfo() {
let userInfo: null | UserInfo = null;
userInfo = await getUserInfoApi();
userStore.setUserInfo(userInfo);
return userInfo;
}
function $reset() {
loginLoading.value = false;
}
return {
$reset,
authLogin,
fetchUserInfo,
loginLoading,
logout,
};
});

View File

@@ -0,0 +1,54 @@
import { defineStore } from 'pinia';
import { api } from '#/api/request';
export const useDictStore = defineStore('dictionary', {
state: () => ({
dictCache: new Map(), // 缓存字典数据
}),
getters: {
// 获取特定字典的 Map 对象
getDictByType: (state) => (dictType: string) => {
return state.dictCache.get(dictType) || new Map();
},
},
actions: {
// 获取字典数据
async fetchDictionary(dictType: string) {
// 如果已经有缓存数据,直接返回
if (this.dictCache.has(dictType)) {
return this.dictCache.get(dictType);
}
try {
const requestPromise = api.get(`/api/v1/dict/items/${dictType}`);
const dictData = await requestPromise;
// 转换为 { value: label } 格式便于查找
const dictMap = new Map(
dictData.data.map((item: any) => [item.value, item.label]),
);
// 缓存数据并清理加载状态
this.dictCache.set(dictType, dictMap);
return dictMap;
} catch (error) {
console.error(`get dict ${dictType} error:`, error);
return new Map();
}
},
// 根据字典类型和值获取标签
getDictLabel(dictType: string, value: any) {
const dictMap = this.dictCache.get(dictType);
if (!dictMap) {
return value; // 返回原值作为降级处理
}
const label = dictMap.get(value);
return label === undefined ? value : label;
},
},
});

View File

@@ -0,0 +1,2 @@
export * from './auth';
export * from './dict';