Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[type:fixbug]fixbug/api-error-only-alert #205

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions src/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
*/

import fetch from 'dva/fetch';
import { notification } from 'antd';
import { routerRedux } from 'dva/router';
import {notification} from 'antd';
import store from '../index';
import { getIntlContent } from './IntlUtils'
import {getIntlContent} from './IntlUtils'

const codeMessage = {
200: '服务器成功返回请求的数据。',
Expand All @@ -38,6 +37,7 @@ const codeMessage = {
503: '服务不可用,服务器暂时过载或维护。',
504: '网关超时。',
};

function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
Expand All @@ -55,7 +55,7 @@ function checkStatus(response) {

/**
* check response's code
* @param {} response
* @param {response} response
*/
const checkResponseCode = response => {
if (response.code === 401) {
Expand All @@ -80,9 +80,8 @@ const checkResponseCode = response => {
* @return {object} An object containing either "data" or "err"
*/
export default function request(url, options) {
const defaultOptions = {
};
const newOptions = { ...defaultOptions, ...options };
const defaultOptions = {};
const newOptions = {...defaultOptions, ...options};
if (
newOptions.method === 'POST' ||
newOptions.method === 'PUT' ||
Expand All @@ -91,7 +90,7 @@ export default function request(url, options) {
if (!(newOptions.body instanceof FormData)) {
newOptions.headers = {
Accept: 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json; charset=utf-8',
...newOptions.headers,
};
Expand All @@ -107,11 +106,11 @@ export default function request(url, options) {

// add token
let token = window.sessionStorage.getItem("token");
if(token){
if(!newOptions.headers){
if (token) {
if (!newOptions.headers) {
newOptions.headers = {};
}
newOptions.headers = {...newOptions.headers,"X-Access-Token":token};
newOptions.headers = {...newOptions.headers, "X-Access-Token": token};
}

return fetch(url, newOptions)
Expand All @@ -122,12 +121,12 @@ export default function request(url, options) {
}
return response.json();
}).then(res => {
if(checkResponseCode(res)){
if (checkResponseCode(res)) {
return res;
}
})
.catch(e => {
const { dispatch } = store;
const {dispatch} = store;
const status = e.name;
if (status === 401) {
dispatch({
Expand All @@ -136,18 +135,6 @@ export default function request(url, options) {
dispatch({
type: "global/resetPermission"
});
return;
}
if (status === 403) {
dispatch(routerRedux.push('/exception/403'));
return;
}
if (status <= 504 && status >= 500) {
dispatch(routerRedux.push('/exception/500'));
return;
}
if (status >= 404 && status < 422) {
dispatch(routerRedux.push('/exception/404'));
}
});
}