function request(url, option) {
  const options = {
    expirys: isAntdPro(),
    ...option,
  };
  /**
   * Produce fingerprints based on url and parameters
   * Maybe url has the same parameters
   */
  const fingerprint = url + (options.body ? JSON.stringify(options.body) : '');
  const hashcode = hash
    .sha256()
    .update(fingerprint)
    .digest('hex');

  const defaultOptions = {
    credentials: 'include',
  };
  const newOptions = { ...defaultOptions, ...options };
  newOptions.headers = { ...newOptions.headers, ...getCommonHeader() };
  if (
    newOptions.method === 'POST' ||
    newOptions.method === 'PUT' ||
    newOptions.method === 'DELETE'
  ) {
    if (!(newOptions.body instanceof FormData)) {
      newOptions.headers = {
        Accept: 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
        ...newOptions.headers,
      };
      newOptions.body = stringify(newOptions.body);
    } else {
      // newOptions.body is FormData
      newOptions.headers = {
        Accept: 'application/json',
        ...newOptions.headers,
      };
    }
  }

  const expirys = options.expirys && 60;
  // options.expirys !== false, return the cache,
  if (options.expirys !== false) {
    const cached = sessionStorage.getItem(hashcode);
    const whenCached = sessionStorage.getItem(`${hashcode}:timestamp`);
    if (cached !== null && whenCached !== null) {
      const age = (Date.now() - whenCached) / 1000;
      if (age < expirys) {
        const response = new Response(new Blob([cached]));
        return response.json();
      }
      sessionStorage.removeItem(hashcode);
      sessionStorage.removeItem(`${hashcode}:timestamp`);
    }
  }
  return fetch(url, newOptions)
    .then(checkStatus)
    .then(response => cachedSave(response, hashcode))
    .then(async response => {
      // DELETE and 204 do not return data by default
      // using .json will report an error.
      if (newOptions.method === 'DELETE' || response.status === 204) {
        return response.text();
      }

      if (newOptions.isFile) {
        if (response.headers.get("Content-Type").indexOf('application/json') > -1) {
          return response.json()
        }
        return response.blob()
      }

      const res = await response.json();
      //console.log("请求返回", res)

      if (res.code !== 0 && res.code != 200) {
        notification.error({
          message: `错误`,
          description: res.msg || res.message,
        });
        if (res.code == -2 || res.code == 402) {
          Cookie.remove('_tk', { path: '/' });
          window.g_app._store.dispatch({
            type: 'login/logout',
          });
          return res;
        }
      }
      return res;
    })
    .catch(e => {
      const status = e.name;
      if (status === 401 || status === 402) {
        // @HACK
        /* eslint-disable no-underscore-dangle */
        window.g_app._store.dispatch({
          type: 'login/logout',
        });
        return { code: 401 };
      }
      // environment should not be used
      if (status === 403) {
        router.push('/exception/403');
        return { code: 403 };
      }
      if (status <= 504 && status >= 500) {
        router.push('/exception/500');
        return { code: 500 };
      }
      if (status >= 404 && status < 422 && token) {
        router.push('/exception/404');
        return { code: 404 };
      }
    });
}
/**
 * Requests a URL, returning a promise.
 *
 * @param  {string} url       The URL we want to request
 * @param  {object} [option] 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, option) {
  const options = {
    expirys: isAntdPro(),
    ...option,
  };
  const fingerprint = url + (options.body ? JSON.stringify(options.body) : '');
  const hashcode = hash
    .sha256()
    .update(fingerprint)
    .digest('hex');
  const defaultOptions = {
    credentials: 'include',
  };
  const newOptions = { ...defaultOptions, ...options };
  newOptions.headers = { ...newOptions.headers, ...getCommonHeader() };
  if (
    newOptions.method === 'POST' ||
    newOptions.method === 'PUT' ||
    newOptions.method === 'DELETE'
  ) {
    if (!(newOptions.body instanceof FormData)) {
      newOptions.headers = {
        Accept: 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
        ...newOptions.headers,
      };
      newOptions.body = stringify(newOptions.body);
    } else {
      newOptions.headers = {
        Accept: 'application/json',
        ...newOptions.headers,
      };
    }
  }
  const expirys = options.expirys && 60;
  if (options.expirys !== false) {
    const cached = sessionStorage.getItem(hashcode);
    const whenCached = sessionStorage.getItem(`${hashcode}:timestamp`);
    if (cached !== null && whenCached !== null) {
      const age = (Date.now() - whenCached) / 1000;
      if (age < expirys) {
        const response = new Response(new Blob([cached]));
        return response.json();
      }
      sessionStorage.removeItem(hashcode);
      sessionStorage.removeItem(`${hashcode}:timestamp`);
    }
  }
  return fetch(url, newOptions)
    .then(checkStatus)
    .then(response => cachedSave(response, hashcode))
    .then(async response => {
      if (newOptions.method === 'DELETE' || response.status === 204) {
        return response.text();
      }
      if (newOptions.isFile) {
        if (response.headers.get("Content-Type").indexOf('application/json') > -1) {
          return response.json()
        }
        return response.blob()
      }
      const res = await response.json();
      if (res.code !== 0 && res.code != 200) {
        notification.error({
          message: `错误`,
          description: res.msg || res.message,
        });
        if (res.code == -2 || res.code == 402) {
          Cookie.remove('_tk', { path: '/' });
          window.g_app._store.dispatch({
            type: 'login/logout',
          });
          return res;
        }
      }
      return res;
    })
    .catch(e => {
      const status = e.name;
      if (status === 401 || status === 402) {
        window.g_app._store.dispatch({
          type: 'login/logout',
        });
        return { code: 401 };
      }
      if (status === 403) {
        router.push('/exception/403');
        return { code: 403 };
      }
      if (status <= 504 && status >= 500) {
        router.push('/exception/500');
        return { code: 500 };
      }
      if (status >= 404 && status < 422 && token) {
        router.push('/exception/404');
        return { code: 404 };
      }
    });
}
function request ( url , option ) { const options = { expirys : isAntdPro ( ) , ... option , } ; const fingerprint = url + ( options . body ? JSON . stringify ( options . body ) : '' ) ; const hashcode = hash . sha256 ( ) . update ( fingerprint ) . digest ( 'hex' ) ; const defaultOptions = { credentials : 'include' , } ; const newOptions = { ... defaultOptions , ... options } ; newOptions . headers = { ... newOptions . headers , ... getCommonHeader ( ) } ; if ( newOptions . method === 'POST' || newOptions . method === 'PUT' || newOptions . method === 'DELETE' ) { if ( ! ( newOptions . body instanceof FormData ) ) { newOptions . headers = { Accept : 'application/json' , 'Content-Type' : 'application/x-www-form-urlencoded; charset=utf-8' , ... newOptions . headers , } ; newOptions . body = stringify ( newOptions . body ) ; } else { newOptions . headers = { Accept : 'application/json' , ... newOptions . headers , } ; } } const expirys = options . expirys && 60 ; if ( options . expirys !== false ) { const cached = sessionStorage . getItem ( hashcode ) ; const whenCached = sessionStorage . getItem ( ` ${ hashcode } ` ) ; if ( cached !== null && whenCached !== null ) { const age = ( Date . now ( ) - whenCached ) / 1000 ; if ( age < expirys ) { const response = new Response ( new Blob ( [ cached ] ) ) ; return response . json ( ) ; } sessionStorage . removeItem ( hashcode ) ; sessionStorage . removeItem ( ` ${ hashcode } ` ) ; } } return fetch ( url , newOptions ) . then ( checkStatus ) . then ( response => cachedSave ( response , hashcode ) ) . then ( async response => { if ( newOptions . method === 'DELETE' || response . status === 204 ) { return response . text ( ) ; } if ( newOptions . isFile ) { if ( response . headers . get ( "Content-Type" ) . indexOf ( 'application/json' ) > - 1 ) { return response . json ( ) } return response . blob ( ) } const res = await response . json ( ) ; if ( res . code !== 0 && res . code != 200 ) { notification . error ( { message : ` description : res . msg || res . message , } ) ; if ( res . code == - 2 || res . code == 402 ) { Cookie . remove ( '_tk' , { path : '/' } ) ; window . g_app . _store . dispatch ( { type : 'login/logout' , } ) ; return res ; } } return res ; } ) . catch ( e => { const status = e . name ; if ( status === 401 || status === 402 ) { window . g_app . _store . dispatch ( { type : 'login/logout' , } ) ; return { code : 401 } ; } if ( status === 403 ) { router . push ( '/exception/403' ) ; return { code : 403 } ; } if ( status <= 504 && status >= 500 ) { router . push ( '/exception/500' ) ; return { code : 500 } ; } if ( status >= 404 && status < 422 && token ) { router . push ( '/exception/404' ) ; return { code : 404 } ; } } ) ; }
Requests a URL, returning a promise.
Requests a URL returning a promise .
/**
   * Produce fingerprints based on url and parameters
   * Maybe url has the same parameters
   */ // newOptions.body is FormData // options.expirys !== false, return the cache, // DELETE and 204 do not return data by default // using .json will report an error. //console.log("请求返回", res) // @HACK /* eslint-disable no-underscore-dangle */ // environment should not be used