| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import {getStrValue, isObjNull} from "./common";
- import store from './../redux/store/store'
- export function fetchPost(url, params, header) {
- if (window.navigator.onLine == false) {
- return Promise.reject('网络连接失败,请检查网络连接')
- }
- const userInfo = store.getState().redUserInfo
- if (isObjNull(header)) {
- header = {}
- }
- let formData = new FormData()
- if (params) {
- for (let key in params) {
- // if ((typeof params[key]) === 'string') {
- // formData.append(key, encodeURI(params[key].toString()))
- // } else {
- formData.append(key, params[key])
- // }
- }
- }
- const request = fetch(url, {
- method: 'POST',
- body: formData,
- mode: 'cors',
- // credentials: 'include',
- // cache: "force-cache",
- headers: new Headers({
- 'Accept': 'application/json',
- // "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
- "Authorization": userInfo.token,
- ...header
- })
- })
- return fetchResult(request)
- }
- export function fetchPostObj(url, params, header) {
- if (window.navigator.onLine == false) {
- return Promise.reject('网络连接失败,请检查网络连接')
- }
- const userInfo = store.getState().redUserInfo
- if (isObjNull(header)) {
- header = {}
- }
- const request = fetch(url, {
- method: 'POST',
- body: params,
- mode: 'cors',
- // credentials: 'include',
- // cache: "force-cache",
- headers: new Headers({
- 'Accept': 'application/json',
- // "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
- "Authorization": userInfo.token,
- ...header
- })
- })
- return fetchResult(request)
- }
- export function fetchGet(url, params, header) {
- if (window.navigator.onLine == false) {
- return Promise.reject('网络连接失败,请检查网络连接')
- }
- const userInfo = store.getState().redUserInfo
- if (isObjNull(header)) {
- header = {}
- }
- if (params) {
- let paramsArray = [];
- //拼接参数
- Object.keys(params).forEach(key =>
- paramsArray.push(key + '=' + encodeURI(getStrValue(params, key).toString())))
- if (paramsArray.length > 0) {
- if (url.search(/\?/) === -1) {
- url += '?' + paramsArray.join('&')
- } else {
- url += '&' + paramsArray.join('&')
- }
- }
- }
- const request = fetch(url, {
- method: 'GET',
- mode: 'cors',
- // credentials: 'include',
- // cache: "force-cache",
- headers: new Headers({
- 'Accept': 'application/json',
- "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
- "Authorization": userInfo.token,
- ...header
- })
- })
- return fetchResult(request)
- }
- /**
- * 处理网络请求结果
- * @param request
- * @returns {*}
- */
- function fetchResult(request) {
- try {
- return request.then(response => {
- if (response.status == 200) {
- return response;
- } else {
- throw response
- }
- }).catch(error => {
- if (error.json) {
- return error.json()
- } else {
- return Promise.reject('请求异常')
- }
- }).then(result => {
- if (result.status == 200) {
- let resultJson = result.json();
- return resultJson;
- } else {
- if (result.exceptionInfo) {
- if (result.exceptionInfo.length > 30) {
- throw '接口请求异常'
- } else {
- throw result.exceptionInfo
- }
- } else {
- throw result
- }
- }
- }).then(result => {
- console.log(result)
- if (result.success) {
- return result
- } else {
- if (result.exceptionInfo) {
- if (result.exceptionInfo.length > 30) {
- throw '接口请求异常'
- } else {
- throw result.exceptionInfo
- }
- } else if (result.message) {
- if (result.message.length > 30) {
- throw '接口请求异常'
- } else {
- throw result.message
- }
- } else {
- throw result
- }
- }
- })
- } catch (e) {
- return Promise.reject('请求异常')
- }
- }
|