refactor(ui): 界面升级与代码规范优化
This commit is contained in:
@@ -44,7 +44,7 @@ function extractFileName(contentDisposition: string): string {
|
||||
* downloadFile(response, "用户列表.xlsx");
|
||||
* ```
|
||||
*/
|
||||
export function downloadFile(response: { data: any; headers: Record<string, string> }, customFileName?: string): void {
|
||||
export function downloadFile(response: { data: any; headers: any }, customFileName?: string): void {
|
||||
try {
|
||||
const fileData = response.data;
|
||||
const contentDisposition = response.headers["content-disposition"];
|
||||
|
||||
@@ -4,6 +4,7 @@ import { ApiCodeEnum } from "@/enums/api";
|
||||
import { useUserStoreHook } from "@/stores/user";
|
||||
import { usePermissionStoreHook } from "@/stores/permission";
|
||||
import { AuthStorage, redirectToLogin } from "@/utils/auth";
|
||||
import type { ApiResult } from "@/api/common";
|
||||
|
||||
// 记录已重试的请求,防止无限循环
|
||||
const retriedConfigs = new WeakSet<InternalAxiosRequestConfig>();
|
||||
@@ -34,7 +35,7 @@ http.interceptors.request.use(
|
||||
|
||||
// 响应拦截器
|
||||
http.interceptors.response.use(
|
||||
(response: AxiosResponse<ApiResponse>): any => {
|
||||
(response: AxiosResponse<ApiResult>): any => {
|
||||
const { responseType } = response.config;
|
||||
|
||||
// 二进制数据直接返回
|
||||
@@ -60,7 +61,7 @@ http.interceptors.response.use(
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
const { code, msg } = response.data as ApiResponse;
|
||||
const { code, msg } = response.data as ApiResult;
|
||||
|
||||
// Token 过期:尝试刷新 token 后自动重试一次
|
||||
if (code === ApiCodeEnum.ACCESS_TOKEN_INVALID) {
|
||||
|
||||
@@ -1,29 +1,18 @@
|
||||
import { ThemeMode } from "@/enums";
|
||||
import { themeColorNames } from "@/settings";
|
||||
import type { ThemeColorMap, ThemeColorName } from "@/settings";
|
||||
|
||||
const SYSTEM_DARK_MEDIA = "(prefers-color-scheme: dark)";
|
||||
|
||||
// 辅助函数:将十六进制颜色转换为 RGB
|
||||
function hexToRgb(hex: string): [number, number, number] {
|
||||
const bigint = parseInt(hex.slice(1), 16);
|
||||
return [(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255];
|
||||
}
|
||||
|
||||
// 辅助函数:将 RGB 转换为十六进制颜色
|
||||
function rgbToHex(r: number, g: number, b: number): string {
|
||||
return `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}`;
|
||||
}
|
||||
|
||||
// 辅助函数:调整颜色亮度
|
||||
/** function adjustBrightness(hex: string, factor: number, theme: string): string {
|
||||
const rgb = hexToRgb(hex);
|
||||
// 是否是暗黑模式
|
||||
const isDarkMode = theme === "dark" ? 0 : 255;
|
||||
const newRgb = rgb.map((val) =>
|
||||
Math.max(0, Math.min(255, Math.round(val + (isDarkMode - val) * factor)))
|
||||
) as [number, number, number];
|
||||
return rgbToHex(...newRgb);
|
||||
} */
|
||||
|
||||
/**
|
||||
* 加深颜色值
|
||||
* @param {String} color 颜色值字符串
|
||||
@@ -39,7 +28,7 @@ export function getDarkColor(color: string, level: number): string {
|
||||
/**
|
||||
* 变浅颜色值
|
||||
* @param {String} color 颜色值字符串
|
||||
* @param {Number} level 加深的程度,限0-1之间
|
||||
* @param {Number} level 变浅的程度,限0-1之间
|
||||
* @returns {String} 返回处理后的颜色值
|
||||
*/
|
||||
export const getLightColor = (color: string, level: number): string => {
|
||||
@@ -49,39 +38,17 @@ export const getLightColor = (color: string, level: number): string => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 生成主题色
|
||||
* @param primary 主题色
|
||||
* @param theme 主题类型
|
||||
* Element Plus 运行时需要 base、light-1..9 和 dark-2。
|
||||
* 这里从完整颜色方案一次性生成,避免主色和功能色来自不同体系。
|
||||
*/
|
||||
export function generateThemeColors(primary: string, theme: ThemeMode) {
|
||||
export function generateThemeColors(palette: ThemeColorMap, theme: ThemeMode) {
|
||||
const resolvedTheme = resolveThemeMode(theme);
|
||||
const colors: Record<string, string> = {
|
||||
primary,
|
||||
};
|
||||
const colors: Record<string, string> = {};
|
||||
|
||||
// 生成 primary 浅色/深色变体
|
||||
for (let i = 1; i <= 9; i++) {
|
||||
colors[`primary-light-${i}`] =
|
||||
resolvedTheme === ThemeMode.LIGHT
|
||||
? `${getLightColor(primary, i / 10)}`
|
||||
: `${getDarkColor(primary, i / 10)}`;
|
||||
}
|
||||
|
||||
colors["primary-dark-2"] =
|
||||
resolvedTheme === ThemeMode.LIGHT
|
||||
? `${getLightColor(primary, 0.2)}`
|
||||
: `${getDarkColor(primary, 0.3)}`;
|
||||
|
||||
// 语义色(success / warning / danger / info)
|
||||
const semanticColors: Record<string, string> = {
|
||||
success: "#22c55e",
|
||||
warning: "#faad14",
|
||||
danger: "#ff4d4f",
|
||||
info: "#788896",
|
||||
};
|
||||
|
||||
Object.entries(semanticColors).forEach(([name, base]) => {
|
||||
themeColorNames.forEach((name: ThemeColorName) => {
|
||||
const base = palette[name];
|
||||
colors[name] = base;
|
||||
|
||||
for (let i = 1; i <= 9; i++) {
|
||||
colors[`${name}-light-${i}`] =
|
||||
resolvedTheme === ThemeMode.LIGHT
|
||||
@@ -123,9 +90,8 @@ export function applyTheme(colors: Record<string, string>) {
|
||||
el.style.setProperty(`--el-color-${key}`, value);
|
||||
});
|
||||
|
||||
// 确保主题色立即生效,强制重新渲染
|
||||
requestAnimationFrame(() => {
|
||||
// 触发样式重新计算
|
||||
// 给依赖 CSS 变量的组件一次明确的样式刷新信号。
|
||||
el.style.setProperty("--theme-update-trigger", Date.now().toString());
|
||||
});
|
||||
}
|
||||
@@ -148,8 +114,8 @@ export function toggleDarkMode(isDark: boolean) {
|
||||
*
|
||||
* @param isBlue 布尔值,表示是否开启深蓝色侧边栏颜色方案
|
||||
*/
|
||||
export function toggleSidebarColor(isBuleSidebar: boolean) {
|
||||
if (isBuleSidebar) {
|
||||
export function toggleSidebarColor(isBlueSidebar: boolean) {
|
||||
if (isBlueSidebar) {
|
||||
document.documentElement.classList.add("sidebar-color-blue");
|
||||
} else {
|
||||
document.documentElement.classList.remove("sidebar-color-blue");
|
||||
|
||||
Reference in New Issue
Block a user