Skip to content

Commit

Permalink
fix: 修复接口请求目标地址异常
Browse files Browse the repository at this point in the history
  • Loading branch information
lingting committed Jul 21, 2023
1 parent b0463f5 commit 3eb15eb
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config/config.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
'process.env': {
msg: '现在是 dev 环境!',
request: {
prefix: 'api',
prefix: '/api',
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion config/config.prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
'process.env': {
msg: '现在是 prod 环境!',
request: {
prefix: 'api',
prefix: '/api',
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion config/config.uat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
'process.env': {
msg: '现在是 uat 环境!',
request: {
prefix: 'api',
prefix: '/api',
},
},
},
Expand Down
12 changes: 5 additions & 7 deletions src/config/RequestConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@ import type { RequestInterceptor, ResponseError, ResponseInterceptor } from 'umi
*/
const customerRequestInterceptor: RequestInterceptor = (url, options) => {
// 处理请求地址
// @ts-ignore
const newUrl = `${process.env.request?.prefix || 'api'}/${
url.startsWith('/') ? url.substring(1) : url
}`;
const envRequest = ((process.env.request as unknown) as Record<string, any>) || {};
const prefix = envRequest.prefix || '/api';
const suffix = url.startsWith('/') ? url.substring(1) : url;
const uri = `${prefix}/${suffix}`;
const headers: any = { ...options.headers };

// 添加token
const token = Token.get();
if (!headers.Authorization && token && isLogin()) {
headers.Authorization = `Bearer ${token}`;
}

// 添加语言
if (!headers['Accept-Language']) {
headers['Accept-Language'] = I18n.getLocal();
}

return { url: newUrl, options: { ...options, headers } };
return { url: uri, options: { ...options, headers } };
};

/**
Expand Down

0 comments on commit 3eb15eb

Please sign in to comment.