mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
refactor(projects): 代码优化
This commit is contained in:
@ -42,5 +42,5 @@ export const ERROR_STATUS = {
|
||||
/** 不弹出错误信息的code */
|
||||
export const NO_ERROR_MSG_CODE: (string | number)[] = [];
|
||||
|
||||
/** token失效需要刷新token的code */
|
||||
/** token失效需要刷新token的code(这里的66666只是个例子,需要将后端表示token过期的code填进来) */
|
||||
export const REFRESH_TOKEN_CODE: (string | number)[] = [66666];
|
||||
|
@ -51,7 +51,7 @@ defineProps<Props>();
|
||||
const theme = useThemeStore();
|
||||
const { isMobile } = useBasicLayout();
|
||||
|
||||
const showButton = import.meta.env.PROD && import.meta.env.VITE_VERCEL !== '1';
|
||||
const showButton = import.meta.env.PROD && import.meta.env.VITE_VERCEL !== 'Y';
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
@ -20,7 +20,7 @@ defineOptions({ name: 'SettingDrawer' });
|
||||
|
||||
const app = useAppStore();
|
||||
|
||||
const showButton = import.meta.env.DEV || import.meta.env.VITE_VERCEL === '1';
|
||||
const showButton = import.meta.env.DEV || import.meta.env.VITE_VERCEL === 'Y';
|
||||
</script>
|
||||
|
||||
<style scoped></style>
|
||||
|
@ -5,10 +5,10 @@ import { constantRoutes } from './routes';
|
||||
import { scrollBehavior } from './helpers';
|
||||
import { createRouterGuard } from './guard';
|
||||
|
||||
const { VITE_HASH_ROUTE = '0', VITE_BASE_URL } = import.meta.env;
|
||||
const { VITE_HASH_ROUTE = 'N', VITE_BASE_URL } = import.meta.env;
|
||||
|
||||
export const router = createRouter({
|
||||
history: VITE_HASH_ROUTE === '1' ? createWebHashHistory(VITE_BASE_URL) : createWebHistory(VITE_BASE_URL),
|
||||
history: VITE_HASH_ROUTE === 'Y' ? createWebHashHistory(VITE_BASE_URL) : createWebHistory(VITE_BASE_URL),
|
||||
routes: transformAuthRoutesToVueRoutes(constantRoutes),
|
||||
scrollBehavior
|
||||
});
|
||||
|
@ -2,7 +2,7 @@ import { getEnvConfig } from '~/.env-config';
|
||||
import { createRequest } from './request';
|
||||
|
||||
const envConfig = getEnvConfig(import.meta.env);
|
||||
const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === '1';
|
||||
const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y';
|
||||
|
||||
export const request = createRequest({ baseURL: isHttpProxy ? envConfig.proxy : envConfig.url });
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { unref } from 'vue';
|
||||
import { defineStore } from 'pinia';
|
||||
import { router } from '@/router';
|
||||
import { useRouterPush } from '@/composables';
|
||||
import { fetchLogin, fetchUserInfo } from '@/service';
|
||||
import { useRouterPush } from '@/composables';
|
||||
import { getUserInfo, getToken, setUserInfo, setToken, setRefreshToken, clearAuthStorage } from '@/utils';
|
||||
import { useTabStore } from '../tab';
|
||||
import { useRouteStore } from '../route';
|
||||
|
26
src/typings/env.d.ts
vendored
26
src/typings/env.d.ts
vendored
@ -1,17 +1,17 @@
|
||||
/**
|
||||
* env环境类型
|
||||
*后台服务的环境类型
|
||||
* - dev: 后台开发环境
|
||||
* - test: 后台测试环境
|
||||
* - prod: 后台生产环境
|
||||
*/
|
||||
type EnvType = 'dev' | 'test' | 'prod';
|
||||
type ServiceEnvType = 'dev' | 'test' | 'prod';
|
||||
|
||||
/** env环境配置 */
|
||||
interface EnvConfig {
|
||||
/** 后端的请求地址 */
|
||||
/** 后台服务的环境配置 */
|
||||
interface ServiceEnvConfig {
|
||||
/** 请求地址 */
|
||||
url: string;
|
||||
/** 代理标识, 用于拦截地址转发代理(如:"/api",这个和后端路径有无 "/api" 路径没有任何关系) */
|
||||
proxy: string;
|
||||
/** 代理标识, 用于拦截地址转发代理(和后端请求路径中有无该路径没有关系) */
|
||||
proxy: '/proxy-flag';
|
||||
}
|
||||
|
||||
interface ImportMetaEnv {
|
||||
@ -32,19 +32,19 @@ interface ImportMetaEnv {
|
||||
/** 路由首页的路径 */
|
||||
readonly VITE_ROUTE_HOME_PATH: Exclude<AuthRoute.RoutePath, '/' | '/not-found-page' | '/:pathMatch(.*)*'>;
|
||||
/** vite环境类型 */
|
||||
readonly VITE_ENV_TYPE?: EnvType;
|
||||
readonly VITE_ENV_TYPE?: ServiceEnvType;
|
||||
/** 开启请求代理 */
|
||||
readonly VITE_HTTP_PROXY?: '1' | '0';
|
||||
readonly VITE_HTTP_PROXY?: 'Y' | 'N';
|
||||
/** 是否开启打包文件大小结果分析 */
|
||||
readonly VITE_VISUALIZER?: '1' | '0';
|
||||
readonly VITE_VISUALIZER?: 'Y' | 'N';
|
||||
/** 是否开启打包压缩 */
|
||||
readonly VITE_COMPRESS?: '1' | '0';
|
||||
readonly VITE_COMPRESS?: 'Y' | 'N';
|
||||
/** 压缩算法类型 */
|
||||
readonly VITE_COMPRESS_TYPE?: 'gzip' | 'brotliCompress' | 'deflate' | 'deflateRaw';
|
||||
/** hash路由模式 */
|
||||
readonly VITE_HASH_ROUTE?: '1' | '0';
|
||||
readonly VITE_HASH_ROUTE?: 'Y' | 'N';
|
||||
/** 是否是部署的vercel */
|
||||
readonly VITE_VERCEL?: '1' | '0';
|
||||
readonly VITE_VERCEL?: 'Y' | 'N';
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
|
5
src/typings/expose.d.ts
vendored
5
src/typings/expose.d.ts
vendored
@ -3,4 +3,9 @@ declare namespace Expose {
|
||||
interface BetterScroll {
|
||||
instance: import('@better-scroll/core').BScrollInstance;
|
||||
}
|
||||
|
||||
interface ImageVerify {
|
||||
/** 获取图片验证码 */
|
||||
getImgCode(): void;
|
||||
}
|
||||
}
|
||||
|
4
src/typings/system.d.ts
vendored
4
src/typings/system.d.ts
vendored
@ -24,8 +24,8 @@ declare namespace Service {
|
||||
/**
|
||||
* 请求的错误类型:
|
||||
* - axios: axios错误:网络错误, 请求超时, 默认的兜底错误
|
||||
* - http: 请求成功,响应的状态码非200的错误
|
||||
* - backend: 请求成功,响应的状态码为200,由后端定义的业务错误
|
||||
* - http: 请求成功,响应的http状态码非200的错误
|
||||
* - backend: 请求成功,响应的http状态码为200,由后端定义的业务错误
|
||||
*/
|
||||
type RequestErrorType = 'axios' | 'http' | 'backend';
|
||||
|
||||
|
@ -1,4 +1,7 @@
|
||||
/** 执行策略模式 */
|
||||
/**
|
||||
* 策略模式
|
||||
* @param actions 每一种可能执行的操作
|
||||
*/
|
||||
export function exeStrategyActions(actions: Common.StrategyAction[]) {
|
||||
actions.some(item => {
|
||||
const [flag, action] = item;
|
||||
|
@ -15,7 +15,7 @@ type ErrorStatus = keyof typeof ERROR_STATUS;
|
||||
|
||||
/**
|
||||
* 处理axios请求失败的错误
|
||||
* @param error - 错误
|
||||
* @param axiosError - 错误
|
||||
*/
|
||||
export function handleAxiosError(axiosError: AxiosError) {
|
||||
const error: Service.RequestError = {
|
||||
@ -75,7 +75,7 @@ export function handleResponseError(response: AxiosResponse) {
|
||||
// 请求成功的状态码非200的错误
|
||||
const errorCode: ErrorStatus = response.status as ErrorStatus;
|
||||
const msg = ERROR_STATUS[errorCode] || DEFAULT_REQUEST_ERROR_MSG;
|
||||
Object.assign(error, { type: 'backend', code: errorCode, msg });
|
||||
Object.assign(error, { type: 'http', code: errorCode, msg });
|
||||
}
|
||||
|
||||
showErrorMsg(error);
|
||||
|
@ -18,15 +18,12 @@ function hasErrorMsg(error: Service.RequestError) {
|
||||
* @param error
|
||||
*/
|
||||
export function showErrorMsg(error: Service.RequestError) {
|
||||
if (!error.msg) return;
|
||||
if (!NO_ERROR_MSG_CODE.includes(error.code)) {
|
||||
if (!hasErrorMsg(error)) {
|
||||
addErrorMsg(error);
|
||||
window.console.warn(error.code, error.msg);
|
||||
window.$message?.error(error.msg, { duration: ERROR_MSG_DURATION });
|
||||
setTimeout(() => {
|
||||
removeErrorMsg(error);
|
||||
}, ERROR_MSG_DURATION);
|
||||
}
|
||||
}
|
||||
if (!error.msg || NO_ERROR_MSG_CODE.includes(error.code) || hasErrorMsg(error)) return;
|
||||
|
||||
addErrorMsg(error);
|
||||
window.console.warn(error.code, error.msg);
|
||||
window.$message?.error(error.msg, { duration: ERROR_MSG_DURATION });
|
||||
setTimeout(() => {
|
||||
removeErrorMsg(error);
|
||||
}, ERROR_MSG_DURATION);
|
||||
}
|
||||
|
@ -56,6 +56,6 @@ async function transformFile(formData: FormData, key: string, file: File[] | Fil
|
||||
);
|
||||
} else {
|
||||
// 单文件
|
||||
await formData.append(key, file);
|
||||
formData.append(key, file);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user