feat(环境配置): 添加环境配置文件标识并优化权限检查代码

在环境配置文件中添加 VITE_APP_CONFIG_FILE 变量标识当前环境配置文件
同时在权限检查模块中优化代码格式和逻辑结构
This commit is contained in:
2025-11-15 23:15:29 +08:00
parent feb53ffad8
commit 55a048725b
6 changed files with 32 additions and 23 deletions

View File

@@ -1,2 +1,3 @@
VITE_APP_CONFIG_FILE=.env.dev
VITE_APP_ENV=开发环境 VITE_APP_ENV=开发环境
VITE_API_URL=http://localhost:8080 VITE_API_URL=http://localhost:8080

View File

@@ -1,2 +1,3 @@
VITE_APP_CONFIG_FILE=.env.prod
VITE_APP_ENV=生产环境 VITE_APP_ENV=生产环境
VITE_API_URL=http://localhost:8080 VITE_API_URL=http://localhost:8080

View File

@@ -1,2 +1,3 @@
VITE_APP_CONFIG_FILE=.env.test
VITE_APP_ENV=测试环境 VITE_APP_ENV=测试环境
VITE_API_URL=http://localhost:8080 VITE_API_URL=http://localhost:8080

View File

@@ -13,28 +13,30 @@ const userStore = useUserStore();
* @param loginUser 当前登录用户 * @param loginUser 当前登录用户
* @returns 如果需要权限访问且权限不足,返回重定向路径;否则返回空字符串 * @returns 如果需要权限访问且权限不足,返回重定向路径;否则返回空字符串
*/ */
const redirectWithAccess = (to: RouteLocationNormalizedGeneric,loginUser: LoginUesr):string =>{ const redirectWithAccess = (
// 获取要访问的路由的权限 to: RouteLocationNormalizedGeneric,
const needAccess: string = to.meta?.access ?? ACCESS_ENUM.NOT_LOGIN; //?? 运算符, 如果 to.meta?.access 为 undefined 或 null loginUser: LoginUesr
// 则使用 ACCESS_ENUM.NOT_LOGIN 作为默认值 ): string => {
// 必须要登录才能访问的页面 // 获取要访问的路由的权限
if (needAccess !== ACCESS_ENUM.NOT_LOGIN) { const needAccess: string = to.meta?.access ?? ACCESS_ENUM.NOT_LOGIN; //?? 运算符, 如果 to.meta?.access 为 undefined 或 null
// 如果说当前是未登录状态 那直接给他跳登录页面去 // 则使用 ACCESS_ENUM.NOT_LOGIN 作为默认值
if ( // 必须要登录才能访问的页面
!loginUser || if (needAccess !== ACCESS_ENUM.NOT_LOGIN) {
!loginUser.userRole || // 如果说当前是未登录状态 那直接给他跳登录页面去
loginUser.userRole === ACCESS_ENUM.NOT_LOGIN if (
) { !loginUser ||
return `/user/login?redirect=${to.fullPath}`; !loginUser.userRole ||
} loginUser.userRole === ACCESS_ENUM.NOT_LOGIN
// 权限不足,跳到无权限页面 ) {
if (!checkAccess(loginUser, needAccess)) { return `/user/login?redirect=${to.fullPath}`;
return "/noAuth"; }
} // 权限不足,跳到无权限页面
} if (!checkAccess(loginUser, needAccess)) {
return ""; return "/noAuth";
}
} }
return "";
};
// 这里接收异步函数,是因为下面要调用 userStore.getLoginUser() // 这里接收异步函数,是因为下面要调用 userStore.getLoginUser()
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
console.log("登陆用户信息", userStore.loginUser); console.log("登陆用户信息", userStore.loginUser);
@@ -44,7 +46,7 @@ router.beforeEach(async (to, from, next) => {
await userStore.getLoginUser(); await userStore.getLoginUser();
loginUser = userStore.loginUser; loginUser = userStore.loginUser;
} }
// 检查是否需要权限访问 // 检查是否需要权限访问
const redirectUrl = redirectWithAccess(to, loginUser); const redirectUrl = redirectWithAccess(to, loginUser);
if (redirectUrl) { if (redirectUrl) {

View File

@@ -2,8 +2,10 @@
interface EnvironmentVariables { interface EnvironmentVariables {
APP_ENV: string; APP_ENV: string;
API_BASE_URL: string; API_BASE_URL: string;
APP_CONFIG_FILE: string;
} }
export const ENV: EnvironmentVariables = { export const ENV: EnvironmentVariables = {
APP_ENV: import.meta.env.VITE_APP_ENV, APP_ENV: import.meta.env.VITE_APP_ENV,
API_BASE_URL: import.meta.env.VITE_API_URL, API_BASE_URL: import.meta.env.VITE_API_URL,
APP_CONFIG_FILE: import.meta.env.VITE_APP_CONFIG_FILE,
}; };

View File

@@ -6,6 +6,8 @@ import path from "path";
export default defineConfig(({ command, mode }) => { export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd(), ""); const env = loadEnv(mode, process.cwd(), "");
console.log("当前环境:", env.VITE_APP_ENV); console.log("当前环境:", env.VITE_APP_ENV);
console.log("使用的环境配置文件为:", env.VITE_APP_CONFIG_FILE);
console.log("当前环境 API 基础 URL:", env.VITE_API_URL);
return { return {
define: { define: {
__APP_ENV__: JSON.stringify(env.VITE_APP_ENV), __APP_ENV__: JSON.stringify(env.VITE_APP_ENV),