Skip to content

Instantly share code, notes, and snippets.

@ohcrfpv
Last active December 4, 2018 02:36
Show Gist options
  • Save ohcrfpv/67f4e0d24154294135242f41e8313370 to your computer and use it in GitHub Desktop.
Save ohcrfpv/67f4e0d24154294135242f41e8313370 to your computer and use it in GitHub Desktop.
通用请求类
import 'whatwg-fetch';
import queryString from 'query-string';
import { fetchMessage, fetchRootDomain } from './helper';
import { toast } from 'react-toastify';
export default class Request {
constructor(token) {
this.baseUrl = fetchRootDomain() + '/';
this.token = token;
}
fetchHeader() {
if (!this.token) {
return {
// 'Content-Type': 'application/json'
}
}
return {
// 'Content-Type': 'application/json',
'Authorization': 'Bearer ' + this.token
}
}
baseReturn(response) {
return response.then((response)=> {
if (response.status === 200) {
var rs = response.json();
// return rs;
return new Promise((resolve, reject)=> {
rs.then((data)=> {
let code = data.resData.code;
if (code === 200) {
resolve(data.resData);
} else {
let m = fetchMessage(data.resData.msg);
toast.error(m);
reject(m);
}
if (code === 401) {
console.log(401);
localStorage.removeItem('persist:root');
window.location.assign('/login');
} else if (code === 402) {
toast.error('无效的域名');
} else if (code === 403) {
toast.error('无效的IP');
} else if (code === 404) {
toast.error('该IP被禁止访问');
}
}).catch((e) => {
// console.dir(e);
toast.error('服务器返回格式错误');
// reject(e);
});
});
}
// if (response.status === 401) {
// console.log(401);
// // store.dispatch(loginFailure());
// window.location.assign(redirect_url);
// }
//
// if (response.status === 500) {
// console.log(500);
// toast.error('服务器错误500');
// }
return null;
}).catch((e)=> {
console.dir(e);
// toast.error(e.message);
toast.error('服务器异常');
throw e;
});
}
delete(path) {
var url = this.baseUrl + path;
let response = fetch(url, {
method: 'DELETE',
headers: this.fetchHeader()
});
return this.baseReturn(response);
}
post(path, params) {
var url = this.baseUrl + path;
var formData = new FormData();
for (var key in params) {
formData.append(key, params[key]);
}
var headers = this.fetchHeader();
// console.log(headers);
let response = fetch(url, {
method: 'POST',
headers: headers,
body: formData
});
return this.baseReturn(response);
}
put(path, params) {
var url = this.baseUrl + path;
let response = fetch(url, {
method: 'PUT',
headers: this.fetchHeader(),
body: JSON.stringify(params)
});
return this.baseReturn(response);
}
get(path, params) {
var url = this.baseUrl + path;
if (params) {
url = url + '?' + queryString.stringify(params);
}
let response = fetch(url, {
method: 'GET',
headers: this.fetchHeader()
});
return this.baseReturn(response);
}
}
// helper.js
//
// const { NODE_ENV } = process.env;
//
// export function fetchMessage(messageObj) {
// var m = '';
// var t = typeof messageObj;
//
// if (t === 'string') {
// return messageObj;
// }
//
// if (t === 'object') {
// var keys = Object.keys(messageObj);
// if (keys) {
// return messageObj[keys[0]];
// }
// return m;
// }
// return m;
// }
//
// export function fetchRootDomain() {
// var rootDomain = 'http://apiadmin.' + document.domain.split('.').slice(-2).join('.');
//
// if (NODE_ENV === 'development') {
// rootDomain = 'http://apiadmin.xxx.com';
// }
//
// return rootDomain;
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment