src/utils/request.js
/**
 * Requests a URL, returning a promise.
 *
 * @param  {string} url       The URL we want to request
 * @param  {object} [options] The options we want to pass to "fetch"
 * @return {object}           An object containing either "data" or "err"
 */
Requests a URL, returning a promise.
Requests a URL returning a promise .
function request(url, options) {
  const defaultOptions = {
    credentials: 'include',
  };
  const newOptions = { ...defaultOptions, ...options };
  if (newOptions.method === 'POST' || newOptions.method === 'PUT') {
    newOptions.headers = {
      Accept: 'application/json',
      'Content-Type': 'application/json; charset=utf-8',
      ...newOptions.headers,
    };
    newOptions.body = JSON.stringify(newOptions.body);
  }
  const checkUrl = url.indexOf('?');
  let newUrl = url;
  const userData = getLocalStorage('userData');
  let submitGlobal = {
    _token : userData.Token,
    _uid : userData.UserId
  }
  if(checkUrl > 0){
    newUrl += `&${stringify(submitGlobal)}`;
  }else{
    newUrl += `?${stringify(submitGlobal)}`;
  }
  const Send = fetch(newUrl, newOptions)
    .then(checkStatus)
    .then((response) => {
      if (newOptions.method === 'DELETE' || response.status === 204) {
        return response.text();
      }
      return response.json();
    });
  Send.then(function(data){
    if(data.Code === 2006 || data.Code === 1005){
      message.warning(<div style={{width:400}}>
        <h3>登录已过期</h3><p>用户登录已过期，点击重新登录按钮跳转到登录页面</p><Button onClick={resetLogin}>确认</Button></div>,0);
    }
    if(data.Code){
      notification.error({
        message: `请求错误 ${data.Code}`,
        description: data.Message,
      });
    }
  });
  return Send
}
function request ( url , options ) { const defaultOptions = { credentials : 'include' , } ; const newOptions = { ... defaultOptions , ... options } ; if ( newOptions . method === 'POST' || newOptions . method === 'PUT' ) { newOptions . headers = { Accept : 'application/json' , 'Content-Type' : 'application/json; charset=utf-8' , ... newOptions . headers , } ; newOptions . body = JSON . stringify ( newOptions . body ) ; } const checkUrl = url . indexOf ( '?' ) ; let newUrl = url ; const userData = getLocalStorage ( 'userData' ) ; let submitGlobal = { _token : userData . Token , _uid : userData . UserId } if ( checkUrl > 0 ) { newUrl += ` ${ stringify ( submitGlobal ) } ` ; } else { newUrl += ` ${ stringify ( submitGlobal ) } ` ; } const Send = fetch ( newUrl , newOptions ) . then ( checkStatus ) . then ( ( response ) => { if ( newOptions . method === 'DELETE' || response . status === 204 ) { return response . text ( ) ; } return response . json ( ) ; } ) ; Send . then ( function ( data ) { if ( data . Code === 2006 || data . Code === 1005 ) { message . warning ( < div style = { { width : 400 } } > < h3 > 登录已过期</h3><p>用户 登 录 已过 期 ， 点 击 重新登录按钮跳转到登录页面</p><Button onClick={resetLogin}>确认</Button></div>,0); } if ( data . Code ) { notification . error ( { message : ` od e}`, description : data . Message , } ) ; } } ) ; return Send }
Requests a URL, returning a promise.
Requests a URL returning a promise .