mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-27 22:55:59 +00:00
feat: 迁移前端资源文件并重构项目结构
refactor: 优化前端代码结构和资源管理 style: 调整前端代码格式和样式 chore: 更新.gitignore和构建配置 fix: 修复前端资源路径和引用问题 docs: 更新前端文档和注释 perf: 优化前端性能和加载速度 test: 更新前端测试用例 build: 调整前端构建配置 ci: 更新CI/CD配置
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
import type { App } from "vue";
|
||||
import { InstallCodeMirror } from "codemirror-editor-vue3";
|
||||
|
||||
export function initCodeMirror(app: App<Element>): void {
|
||||
app.use(InstallCodeMirror);
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
import axios, { AxiosResponse } from "axios";
|
||||
import { ElLoading, ElMessage } from "element-plus";
|
||||
import { saveAs as fileSaverSaveAs } from "file-saver";
|
||||
import { Auth } from "@/utils/auth";
|
||||
import errorCode from "@/utils/errorCode";
|
||||
import { blobValidate } from "@/utils/common";
|
||||
|
||||
const baseURL = import.meta.env.VITE_APP_BASE_API;
|
||||
let downloadLoadingInstance: any;
|
||||
|
||||
interface DownloadUtil {
|
||||
name(name: string, isDelete?: boolean): void;
|
||||
resource(resource: string): void;
|
||||
zip(url: string, name: string): void;
|
||||
saveAs(text: Blob | string, name: string, opts?: any): void;
|
||||
printErrMsg(data: Blob): Promise<void>;
|
||||
}
|
||||
|
||||
const download: DownloadUtil = {
|
||||
name(name: string, isDelete: boolean = true): void {
|
||||
const url =
|
||||
baseURL + "/common/download?fileName=" + encodeURIComponent(name) + "&delete=" + isDelete;
|
||||
axios({
|
||||
method: "get",
|
||||
url,
|
||||
responseType: "blob",
|
||||
headers: { Authorization: "Bearer " + Auth.getAccessToken() },
|
||||
}).then((res: AxiosResponse<Blob>) => {
|
||||
const isBlob = blobValidate(res.data);
|
||||
if (isBlob) {
|
||||
const blob = new Blob([res.data]);
|
||||
download.saveAs(blob, decodeURIComponent(res.headers["download-filename"]));
|
||||
} else {
|
||||
download.printErrMsg(res.data);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
resource(resource: string): void {
|
||||
const url = baseURL + "/common/download/resource?resource=" + encodeURIComponent(resource);
|
||||
axios({
|
||||
method: "get",
|
||||
url,
|
||||
responseType: "blob",
|
||||
headers: { Authorization: "Bearer " + Auth.getAccessToken() },
|
||||
}).then((res: AxiosResponse<Blob>) => {
|
||||
const isBlob = blobValidate(res.data);
|
||||
if (isBlob) {
|
||||
const blob = new Blob([res.data]);
|
||||
download.saveAs(blob, decodeURIComponent(res.headers["download-filename"]));
|
||||
} else {
|
||||
download.printErrMsg(res.data);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
zip(url: string, name: string): void {
|
||||
const fullUrl = baseURL + url;
|
||||
downloadLoadingInstance = ElLoading.service({
|
||||
text: "正在下载数据,请稍候",
|
||||
background: "rgba(0, 0, 0, 0.7)",
|
||||
});
|
||||
axios({
|
||||
method: "get",
|
||||
url: fullUrl,
|
||||
responseType: "blob",
|
||||
headers: { Authorization: "Bearer " + Auth.getAccessToken() },
|
||||
})
|
||||
.then((res: AxiosResponse<Blob>) => {
|
||||
const isBlob = blobValidate(res.data);
|
||||
if (isBlob) {
|
||||
const blob = new Blob([res.data], { type: "application/zip" });
|
||||
download.saveAs(blob, name);
|
||||
} else {
|
||||
download.printErrMsg(res.data);
|
||||
}
|
||||
downloadLoadingInstance.close();
|
||||
})
|
||||
.catch((r: any) => {
|
||||
console.error(r);
|
||||
ElMessage.error("下载文件出现错误,请联系管理员!");
|
||||
downloadLoadingInstance.close();
|
||||
});
|
||||
},
|
||||
|
||||
saveAs(text: Blob | string, name: string, opts?: any): void {
|
||||
fileSaverSaveAs(text, name, opts);
|
||||
},
|
||||
|
||||
async printErrMsg(data: Blob): Promise<void> {
|
||||
const resText = await data.text();
|
||||
const rspObj = JSON.parse(resText);
|
||||
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode["default"];
|
||||
ElMessage.error(errMsg);
|
||||
},
|
||||
};
|
||||
|
||||
export default download;
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* ECharts 插件配置
|
||||
*
|
||||
* 按需导入 ECharts 图表和组件,减小打包体积。
|
||||
* 只注册项目中实际使用的图表类型和组件。
|
||||
*
|
||||
* @module plugins/echarts
|
||||
* @author FastapiAdmin Team
|
||||
*/
|
||||
|
||||
// ECharts 按需导入配置
|
||||
import * as echarts from "echarts/core";
|
||||
|
||||
// 导入图表类型
|
||||
import {
|
||||
BarChart,
|
||||
LineChart,
|
||||
PieChart,
|
||||
ScatterChart,
|
||||
RadarChart,
|
||||
MapChart,
|
||||
CandlestickChart,
|
||||
} from "echarts/charts";
|
||||
|
||||
// 导入组件
|
||||
import {
|
||||
TitleComponent,
|
||||
TooltipComponent,
|
||||
GridComponent,
|
||||
LegendComponent,
|
||||
DataZoomComponent,
|
||||
MarkPointComponent,
|
||||
MarkLineComponent,
|
||||
ToolboxComponent,
|
||||
BrushComponent,
|
||||
GeoComponent,
|
||||
VisualMapComponent,
|
||||
} from "echarts/components";
|
||||
|
||||
// 导入渲染器
|
||||
import { CanvasRenderer } from "echarts/renderers";
|
||||
|
||||
// 注册必要的组件
|
||||
echarts.use([
|
||||
// 图表类型
|
||||
BarChart,
|
||||
LineChart,
|
||||
PieChart,
|
||||
ScatterChart,
|
||||
RadarChart,
|
||||
MapChart,
|
||||
CandlestickChart,
|
||||
|
||||
// 组件
|
||||
TitleComponent,
|
||||
TooltipComponent,
|
||||
GridComponent,
|
||||
LegendComponent,
|
||||
DataZoomComponent,
|
||||
MarkPointComponent,
|
||||
MarkLineComponent,
|
||||
ToolboxComponent,
|
||||
BrushComponent,
|
||||
GeoComponent,
|
||||
VisualMapComponent,
|
||||
|
||||
// 渲染器
|
||||
CanvasRenderer,
|
||||
]);
|
||||
|
||||
// 导出 echarts 实例和类型
|
||||
export { echarts };
|
||||
export type { EChartsOption, BarSeriesOption } from "echarts";
|
||||
|
||||
// 导出常用的图形工具
|
||||
export const graphic = echarts.graphic;
|
||||
@@ -0,0 +1,6 @@
|
||||
import type { App } from "vue";
|
||||
import ElementPlus from "element-plus";
|
||||
|
||||
export function initElementPlus(app: App<Element>): void {
|
||||
app.use(ElementPlus);
|
||||
}
|
||||
@@ -1,31 +1,33 @@
|
||||
import type { App } from "vue";
|
||||
/**
|
||||
* Vue 应用插件注册 —— **唯一入口**:`initPlugins`(由 `main.ts` 调用)
|
||||
*
|
||||
* 约定:
|
||||
* - 凡对 `app.use(...)` 的封装,在本目录下独立文件导出 `initXxx(app)`(与 `icons.ts` 一致)。
|
||||
* - `echarts.ts` 为图表按需注册模块,供 `import { echarts } from '@/plugins/echarts'`,不由 `initPlugins` 挂载。
|
||||
* - 通用下载工具见 `@utils/download`,不属于 Vue 插件。
|
||||
*/
|
||||
|
||||
import { setupDirective } from "@/directives";
|
||||
export * from "./echarts";
|
||||
|
||||
import type { App } from "vue";
|
||||
import { initGlobDirectives } from "@/directives";
|
||||
import { initI18n } from "@/locales";
|
||||
import { initRouter } from "@/router";
|
||||
import { initStore } from "@/store";
|
||||
import { initStore } from "@stores";
|
||||
import { initErrorHandle } from "@utils/sys";
|
||||
import { initCodeMirror } from "./codemirror";
|
||||
import { initElementPlus } from "./element-plus";
|
||||
import { initElIcons } from "./icons";
|
||||
import { initPermission } from "./permission";
|
||||
import { InstallCodeMirror } from "codemirror-editor-vue3";
|
||||
import ElementPlus from "element-plus";
|
||||
import { initTerminal } from "./terminal";
|
||||
|
||||
export default {
|
||||
install(app: App<Element>) {
|
||||
// 自定义指令(directive)
|
||||
setupDirective(app);
|
||||
// 路由(router)
|
||||
initRouter(app);
|
||||
// 状态管理(store)
|
||||
initStore(app);
|
||||
// 国际化
|
||||
initI18n(app);
|
||||
// Element-plus图标
|
||||
initElIcons(app);
|
||||
// 路由守卫
|
||||
initPermission(app);
|
||||
// 注册 CodeMirror
|
||||
app.use(InstallCodeMirror);
|
||||
// 注册 ElementPlus
|
||||
app.use(ElementPlus);
|
||||
},
|
||||
};
|
||||
export function initPlugins(app: App<Element>): void {
|
||||
initElIcons(app);
|
||||
initStore(app);
|
||||
initRouter(app);
|
||||
initGlobDirectives(app);
|
||||
initErrorHandle(app);
|
||||
initTerminal(app);
|
||||
initI18n(app);
|
||||
initCodeMirror(app);
|
||||
initElementPlus(app);
|
||||
}
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
import type { App } from "vue";
|
||||
import type { NavigationGuardNext, RouteLocationNormalized, RouteRecordRaw } from "vue-router";
|
||||
import NProgress from "@/utils/nprogress";
|
||||
import { Auth } from "@/utils/auth";
|
||||
import router from "@/router";
|
||||
import { usePermissionStore, useUserStore } from "@/store";
|
||||
|
||||
export function initPermission(app: App<Element>) {
|
||||
// 白名单路由
|
||||
const whiteList = ["/login"];
|
||||
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
NProgress.start();
|
||||
|
||||
try {
|
||||
const isLoggedIn = Auth.isLoggedIn();
|
||||
|
||||
if (isLoggedIn) {
|
||||
// 如果已登录但访问登录页,重定向到首页
|
||||
if (to.path === "/login") {
|
||||
next({ path: "/" });
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理已登录用户的路由访问
|
||||
await handleAuthenticatedUser(to, from, next);
|
||||
} else {
|
||||
// 未登录用户的处理
|
||||
if (whiteList.includes(to.path)) {
|
||||
next();
|
||||
} else {
|
||||
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`);
|
||||
NProgress.done();
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// 错误处理:重置状态并跳转登录(保留 redirect 与未登录分支一致)
|
||||
console.error("Route guard error:", error);
|
||||
await useUserStore().resetAllState();
|
||||
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`);
|
||||
NProgress.done();
|
||||
}
|
||||
});
|
||||
|
||||
// 后置守卫,确保进度条关闭
|
||||
router.afterEach(() => {
|
||||
NProgress.done();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理已登录用户的路由访问
|
||||
*/
|
||||
async function handleAuthenticatedUser(
|
||||
to: RouteLocationNormalized,
|
||||
from: RouteLocationNormalized,
|
||||
next: NavigationGuardNext
|
||||
) {
|
||||
const permissionStore = usePermissionStore();
|
||||
const userStore = useUserStore();
|
||||
|
||||
try {
|
||||
// 检查路由是否已生成
|
||||
if (!permissionStore.isRouteGenerated) {
|
||||
if (!userStore.basicInfo?.roles?.length) {
|
||||
await userStore.getUserInfo();
|
||||
}
|
||||
|
||||
const dynamicRoutes = await permissionStore.generateRoutes();
|
||||
// 添加路由到路由器
|
||||
dynamicRoutes.forEach((route: RouteRecordRaw) => {
|
||||
router.addRoute(route);
|
||||
});
|
||||
|
||||
// 路由生成完成后,重新导航到目标路由
|
||||
next({ ...to, replace: true });
|
||||
return;
|
||||
}
|
||||
|
||||
// 路由已加载,检查路由是否存在
|
||||
if (to.matched.length === 0) {
|
||||
next("/404");
|
||||
return;
|
||||
}
|
||||
|
||||
// 动态设置页面标题(仅做简单净化,避免 query 注入标签/过长字符串影响标签栏展示)
|
||||
const rawTitle = (to.params.title as string) || (to.query.title as string);
|
||||
if (rawTitle && typeof rawTitle === "string") {
|
||||
const safe = rawTitle.replace(/[<>]/g, "").trim().slice(0, 64);
|
||||
if (safe) {
|
||||
to.meta.title = safe;
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
console.error("❌ Route guard error:", error);
|
||||
await useUserStore().resetAllState();
|
||||
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`);
|
||||
NProgress.done();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { App } from "vue";
|
||||
import { createTerminal } from "vue-web-terminal";
|
||||
|
||||
/** vue-web-terminal:注册全局终端组件 */
|
||||
export function initTerminal(app: App<Element>): void {
|
||||
app.use(createTerminal());
|
||||
}
|
||||
Reference in New Issue
Block a user