ie导出问题

import { extend } from 'umi-request';
import { notification, Modal } from 'antd';
import { getToken }  from './cookies';
import cloneDeep from 'lodash/cloneDeep';
import defaultSetting from '../defaultSetting';
import { IEVersion } from './utils';
const codeMessage = {
  200: '服务器成功返回请求的数据。',
  201: '新建或修改数据成功。',
  202: '一个请求已经进入后台排队(异步任务)。',
  204: '删除数据成功。',
  400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
  401: '用户没有权限(令牌、用户名、密码错误)。',
  403: '用户得到授权,但是访问是被禁止的。',
  404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
  406: '请求的格式不可得。',
  410: '请求的资源被永久删除,且不会再得到的。',
  422: '当创建一个对象时,发生一个验证错误。',
  500: '服务器发生错误,请检查服务器。',
  502: '网关错误。',
  503: '服务不可用,服务器暂时过载或维护。',
  504: '网关超时。',
};

/**
 * 异常处理程序
 */
const errorHandler = error => {
  const { response = {}, data } = error;
  const errortext = codeMessage[response.status] || response.statusText;
  const { status, url } = response;
  console.dir(error);
  // if (msg && (url.indexOf('/system/menu/getUserMenu') === -1)){
  //   Modal.confirm({
  //     title: '系统提示',
  //     content: msg,
  //     okText: '确定',
  //     cancelText: '取消',
  //     onOk: () =>  {
  //       router.push('/user/login') 
  //     },
  //     onCancel: () =>  {
  //       router.push('/user/login') 
  //     }
  //   })
  //   return ;
  // }
  if (status === 401) {
    // @HACK
    /* eslint-disable no-underscore-dangle */
    // removeToken();
    return;
  }
  if ( status === 500) {
    if (data && data.code !== 700) {
      notification.error({
      message: `请求错误 ${status}: ${url}`,
      description: errortext,
      });
    }
  }

  // environment should not be used
  if (status === 403) {
    // router.push('/403');
    return;
  }
  if (status <= 504 && status >= 500) {
    // router.push('/500');
    return;
  }
  if (status >= 404 && status < 422) {
    // router.push('/404');
  }
};

/**
 * 配置request请求时的默认参数
 */
const request = extend({
  errorHandler, // 默认错误处理
  timeout:10000,
  credentials: 'include', // 默认请求是否带上cookie,
});

request.interceptors.request.use(async (url, options) => {
  const token = getToken();
  const option = cloneDeep(options);
  const headers = {};
  headers['Content-Type'] = 'application/json;charset=utf-8';
  if (options.method.toLowerCase() === 'get') {
    if(url.indexOf('?') === -1) {
      url = url + '?timer=' + new Date().valueOf()
    } else {
      url = `${url}&timer=${new Date().valueOf()}`
    }
  }
  const baseurl = defaultSetting[process.env.NODE_ENV].baseUrl + url;
  if (String(token) !== "undefined") {
    return (
      {
        url: baseurl,
        options: {
          ...option,
          headers:{
            token,
            ...headers
          }
        },
      }
    );
  } else {
    // if (url.indexOf('/system/menu/getUserMenu') !== -1) {
    // } else {
      delete option.headers.token;
      return (
        {
          url: baseurl,
          options: {
            ...option,
            headers: {
              'Content-Type': 'application/json;charset=utf-8'
            }
          },
        }
      );
    // }  
  }
})

request.interceptors.response.use( async(response, options) => {
  if ((options.responseType === 'blob') && (IEVersion() !== -1) &&(IEVersion() !== 'edge')) {
    console.log(111);
      return {data:response.clone()._bodyBlob,response:{ headers: response.headers}};
  }
  if((response.url.indexOf('borderSystem/derive') !== -1) || (response.url.indexOf('/code') !== -1) || (response.url.indexOf('/system/menu/getUserMenu') !== -1) ){// 
    // const res = await response.clone().text();
    // return new Blob([res]);
  } else {
    const res = await response.clone().json();
    const {code } = res;
    if (code === 401) {
      Modal.confirm({
        title: '系统提示',
        content: '登录状态已过期,您可以继续留在该页面,或者重新登录',
        okText: '重新登录',
        cancelText: '取消',
        onOk: () =>  {
          if ( window.g_app._store) {
            window.g_app._store.dispatch({
              type: 'login/loginOut',
            }).then(() => {window.location.reload()});
          }
        },
        onCancel: () =>  {
          Modal.destroyAll()       
        }
      })
    } else if (code === 700) {
      notification.warn({
        message: ``,
        description: res.msg,
      });
    } else if (code === 500) {
      notification.warn({
        message: ``,
        description: res.msg,
      });
    }
  }
  return response;
});

export default request;
export function IEVersion() {
  var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
  var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器  
  var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器  
  var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
  if(isIE) {
      var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
      reIE.test(userAgent);
      var fIEVersion = parseFloat(RegExp["$1"]);
      if(fIEVersion == 7) {
          return 7;
      } else if(fIEVersion == 8) {
          return 8;
      } else if(fIEVersion == 9) {
          return 9;
      } else if(fIEVersion == 10) {
          return 10;
      } else {
          return 6;//IE版本<=7
      }   
  } else if(isEdge) {
      return 'edge';//edge
  } else if(isIE11) {
      return 11; //IE11  
  }else{
      return -1;//不是ie浏览器
  }
}

猜你喜欢

转载自www.cnblogs.com/l8l8/p/12105819.html