Skip to content

Commit

Permalink
refactor: log改为用统一类打印,便于注释掉error和warn打印
Browse files Browse the repository at this point in the history
  • Loading branch information
yuntian001 committed Sep 28, 2022
1 parent 842dda0 commit ed33ffe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/locales/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown, unknown, unknown, any>;
const messageMap: Map<string, Record<any, any>> = new Map();
export type MessageImport = [(locale: string) => Promise<{ default: LocaleMessages<VueMessageType> }>, string?];
Expand Down Expand Up @@ -33,7 +34,7 @@ export const loadMessage = <P extends Record<any, any> = { default: LocaleMessag
let timeOut: NodeJS.Timeout;
if (config.loadMessageConfig.timeOut) {
timeOut = setTimeout(() => {
console.warn('加载语言包超时');
log.warn('加载语言包超时');
isLoading && closeLoading();
resolve(null);
}, config.loadMessageConfig.timeOut);
Expand All @@ -50,7 +51,7 @@ export const loadMessage = <P extends Record<any, any> = { 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);
Expand Down
8 changes: 8 additions & 0 deletions src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -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);
},
};
11 changes: 6 additions & 5 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -23,7 +24,7 @@ service.interceptors.request.use(
},
(error) => {
// 对请求错误做些什么
console.error(error); // for debug
log.error(error); // for debug
throw Error('请求异常,请联系管理员'); // 改写错误信息
},
);
Expand All @@ -36,7 +37,7 @@ service.interceptors.response.use(
(error) => {
// 超出 2xx 范围的状态码都会触发该函数。
// 对响应错误做点什么
console.error(error); // for debug
log.error(error); // for debug
throw Error('操作失败,请稍后重试');
},
);
Expand All @@ -58,19 +59,19 @@ function request<R, P extends unknown[] = []>(
axiosConfig: (...args: P) => AxiosRequestConfig,
options?: RequestOptions<R, P>,
): ReturnType<typeof useRequest<R, P>>;
function request<R, P extends unknown[] = [], T extends true | undefined = true | undefined>(
function request<R, P extends unknown[] = [], T extends boolean | undefined = boolean | undefined>(
axiosConfig: (...args: P) => AxiosRequestConfig,
options: RequestOptions<R, P>,
returnAxios?: T,
): T extends true ? (...args: P) => Promise<R> : ReturnType<typeof useRequest<R, P>>;
): T extends boolean ? (...args: P) => Promise<R> : ReturnType<typeof useRequest<R, P>>;
/**
* 请求函数
* @param axiosConfig axios的配置项
* @param options vue request配置项+自定义配置项参考 RequestOptions
* @param returnAxios
* @returns
*/
function request<R, P extends unknown[] = [], T = true>(
function request<R, P extends unknown[] = [], T = boolean>(
axiosConfig: (...args: P) => AxiosRequestConfig,
options?: RequestOptions<R, P>,
returnAxios?: T,
Expand Down

0 comments on commit ed33ffe

Please sign in to comment.