mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-27 22:55:59 +00:00
feat: 重构前端项目结构并优化代码
refactor: 迁移前端资源文件至web目录 feat: 新增多种图标资源 style: 统一代码风格和格式化配置 docs: 更新README和文档说明 chore: 更新依赖和配置文件 fix: 修复部分类型定义和枚举 perf: 优化路由和组件加载逻辑
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>) {
|
||||
app.use(InstallCodeMirror);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
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 Fastapi Admin 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>) {
|
||||
app.use(ElementPlus);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import type { App } from 'vue';
|
||||
import i18n from '@/locales';
|
||||
|
||||
export function initI18n(app: App<Element>) {
|
||||
app.use(i18n);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import type { App } from 'vue';
|
||||
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
|
||||
|
||||
// 注册所有图标
|
||||
export function initElIcons(app: App<Element>) {
|
||||
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
|
||||
app.component(key, component);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import type { App } from 'vue';
|
||||
|
||||
import { initDirectives } from '@/directives';
|
||||
import { initRouter } from '@/router';
|
||||
import { initStore } from '@/store';
|
||||
import { initElIcons } from './icons';
|
||||
import { initPermission } from './permission';
|
||||
import { initI18n } from './i18n';
|
||||
import { initCodeMirror } from './codemirror';
|
||||
import { initElementPlus } from './elementPlus';
|
||||
|
||||
export default {
|
||||
install(app: App<Element>) {
|
||||
// 自定义指令(directive)
|
||||
initDirectives(app);
|
||||
// 路由(router)
|
||||
initRouter(app);
|
||||
// 状态管理(store)
|
||||
initStore(app);
|
||||
// 国际化
|
||||
initI18n(app);
|
||||
// Element-plus图标
|
||||
initElIcons(app);
|
||||
// 路由守卫
|
||||
initPermission(app);
|
||||
// 注册 CodeMirror
|
||||
initCodeMirror(app);
|
||||
// 注册 ElementPlus
|
||||
initElementPlus(app);
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* 插件统一导出
|
||||
* 集中管理第三方库的封装和配置
|
||||
*/
|
||||
|
||||
export * from './echarts';
|
||||
@@ -0,0 +1,102 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user