From ed33ffe90e51bb490a9aadcc41bf0a942cd06e3d Mon Sep 17 00:00:00 2001 From: yuntian001 <479820787@qq.com> Date: Wed, 28 Sep 2022 11:47:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20log=E6=94=B9=E4=B8=BA=E7=94=A8?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E7=B1=BB=E6=89=93=E5=8D=B0=EF=BC=8C=E4=BE=BF?= =?UTF-8?q?=E4=BA=8E=E6=B3=A8=E9=87=8A=E6=8E=89error=E5=92=8Cwarn=E6=89=93?= =?UTF-8?q?=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/locales/helper.ts | 5 +++-- src/utils/log.ts | 8 ++++++++ src/utils/request.ts | 11 ++++++----- 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 src/utils/log.ts diff --git a/src/locales/helper.ts b/src/locales/helper.ts index 7c57637f..28a695a0 100644 --- a/src/locales/helper.ts +++ b/src/locales/helper.ts @@ -4,6 +4,7 @@ import { event, mitter } from '@/event'; import { loading, closeLoading } from '@/utils/loading'; import { Language } from 'element-plus/es/locale'; import { useGlobalStore, useSettingStore } from '@/store'; +import log from '@/utils/log'; type GlobaleI18n = Composer; const messageMap: Map> = new Map(); export type MessageImport = [(locale: string) => Promise<{ default: LocaleMessages }>, string?]; @@ -33,7 +34,7 @@ export const loadMessage =

= { default: LocaleMessag let timeOut: NodeJS.Timeout; if (config.loadMessageConfig.timeOut) { timeOut = setTimeout(() => { - console.warn('加载语言包超时'); + log.warn('加载语言包超时'); isLoading && closeLoading(); resolve(null); }, config.loadMessageConfig.timeOut); @@ -50,7 +51,7 @@ export const loadMessage =

= { default: LocaleMessag .catch((e) => { timeOut && clearTimeout(timeOut); if (import.meta.env.DEV && config.loadMessageConfig.errorWarning) { - console.warn(`语言包${mapName}加载失败:`, e); + log.warn(`语言包${mapName}加载失败:`, e); } isLoading && closeLoading(); resolve(null); diff --git a/src/utils/log.ts b/src/utils/log.ts new file mode 100644 index 00000000..93a98a88 --- /dev/null +++ b/src/utils/log.ts @@ -0,0 +1,8 @@ +export default { + warn(message?: string, ...optionalParams: any[]) { + console.warn(`[meadmin warn]:${message}`, ...optionalParams); + }, + error(message?: string, ...optionalParams: any[]) { + console.error(`[meadmin error]:${message}`, ...optionalParams); + }, +}; diff --git a/src/utils/request.ts b/src/utils/request.ts index 4cfe2d2b..bee43f22 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -1,6 +1,7 @@ import { useUserStore } from '@/store'; import axios, { AxiosRequestConfig } from 'axios'; import { ElMessage } from 'element-plus'; +import log from './log'; import { useRequest, Options, setGlobalOptions } from 'vue-request'; import { loading, closeLoading } from './loading'; @@ -23,7 +24,7 @@ service.interceptors.request.use( }, (error) => { // 对请求错误做些什么 - console.error(error); // for debug + log.error(error); // for debug throw Error('请求异常,请联系管理员'); // 改写错误信息 }, ); @@ -36,7 +37,7 @@ service.interceptors.response.use( (error) => { // 超出 2xx 范围的状态码都会触发该函数。 // 对响应错误做点什么 - console.error(error); // for debug + log.error(error); // for debug throw Error('操作失败,请稍后重试'); }, ); @@ -58,11 +59,11 @@ function request( axiosConfig: (...args: P) => AxiosRequestConfig, options?: RequestOptions, ): ReturnType>; -function request( +function request( axiosConfig: (...args: P) => AxiosRequestConfig, options: RequestOptions, returnAxios?: T, -): T extends true ? (...args: P) => Promise : ReturnType>; +): T extends boolean ? (...args: P) => Promise : ReturnType>; /** * 请求函数 * @param axiosConfig axios的配置项 @@ -70,7 +71,7 @@ function request( +function request( axiosConfig: (...args: P) => AxiosRequestConfig, options?: RequestOptions, returnAxios?: T,