Skip to content

Commit

Permalink
feat: 接口请求公共提示加上国际化支持
Browse files Browse the repository at this point in the history
  • Loading branch information
yuntian001 committed Nov 7, 2022
1 parent 12159b3 commit dfaeb8b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/locales/lang/en/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
"并": "And",
"退出登录": "Logout",
"页面不见了": "The page is gone",
"去": "Go"
"去": "Go",
"操作失败,请稍后重试":"Operation failed, please try again later",
"返回值解析失败":"Failed to parse the return value",
"请求异常,请联系管理员":"The request is abnormal, Please contact the administrator"
}
10 changes: 5 additions & 5 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { closeLoading, loading } from '@/utils/loading';
import { useUserStore } from '@/store';
import { useUserStore,useGlobalStore } from '@/store';
import axios, { AxiosRequestConfig } from 'axios';
import { ElMessage } from 'element-plus';
import log from './log';
import { useRequest, Options, setGlobalOptions } from 'vue-request';

const t = (...args:[string|number])=>useGlobalStore().i18n.t(...args);
const service = axios.create({
baseURL: '/', // url = base url + request url
timeout: 10000, // request timeout
Expand All @@ -25,7 +25,7 @@ service.interceptors.request.use(
(error) => {
// 对请求错误做些什么
log.error(error); // for debug
throw Error('请求异常,请联系管理员'); // 改写错误信息
throw Error(t('请求异常,请联系管理员')); // 改写错误信息
},
);
service.interceptors.response.use(
Expand All @@ -38,7 +38,7 @@ service.interceptors.response.use(
// 超出 2xx 范围的状态码都会触发该函数。
// 对响应错误做点什么
log.error(error); // for debug
throw Error('操作失败,请稍后重试');
throw Error(t('操作失败,请稍后重试'));
},
);

Expand Down Expand Up @@ -81,7 +81,7 @@ function request<R, P extends unknown[] = [], T = boolean>(
!options?.noLoading && loading();
const { data: res } = await service(await axiosConfig(...args));
if (!res || res.code === undefined) {
throw Error('返回值解析失败');
throw Error(t('返回值解析失败'));
}
// 401:认证失败
if (res.code === '401') {
Expand Down

0 comments on commit dfaeb8b

Please sign in to comment.