index.js 3.63 KB
import axios from 'axios'
import { message } from 'antd'
import SYS_CONFIG from '@src/config/constant.js'
import Storage from '@src/utils/localStorage'

export default class Axios {
  static ajax(options) {
    const userInfo = Storage.get('userInfo')
    const isLogin = window.location.href.includes('/login')

    return new Promise((resolve, reject) => {
      
      if (!isLogin) {
        if (!userInfo || !userInfo.token) {
          window.location.href = '#/login'
          return
        }
      }

      axios({
        ...options,
        url: options.url,
        method: options.method ? options.method : 'POST',
        baseURL: SYS_CONFIG.baseApi,
        headers: options.headers ? options.headers : {
          'Content-Type': 'application/json; charset=UTF-8',
          'X-Requested-with': 'XMLHttpRequest',
          'token': !isLogin ? userInfo.token : '',          
        },
        params: options.params ? options.params : '',
        data: JSON.stringify(options.data) === "{}" ? null : JSON.stringify(options.data),

      }).then(res => {       
        if (res.status === 200) {
          let resp = res.data;
          if (res.data.code == 0) {
            resolve(resp.data)
          } else {
            if ((res.data.msg && res.data.msg.includes('token')) || (res.data.msg && res.data.msg.includes('在别处登录')) ) {
              Storage.clearToken()
              window.location.href = '#/login'
              return
            }
            if(res.data.msg && res.data.msg.length < 30){
              message.error(res.data.msg)    
              return
            }
            // message.error('请求超时,请重试');
          }
        } else {
          reject(res.data)
        }
      }).catch(err => {
        message.error('请求超时,请重试');
        reject(err)
      })
    })
  }
 static optAjax(options) {
  const userInfo = Storage.get('userInfo')
    const isLogin = window.location.href.includes('/login')

    return new Promise((resolve, reject) => {
      
      if (!isLogin) {
        if (!userInfo || !userInfo.token) {
          // message.warn('token失效,请重新登录')
          window.location.href = '#/login'
          return
        }
      }

      axios({
        ...options,
        url: options.url,
        method: options.method ? options.method : 'POST',
        baseURL: SYS_CONFIG.baseApi,
        headers: options.headers ? options.headers : {
          'Content-Type': 'application/json; charset=UTF-8',
          'X-Requested-with': 'XMLHttpRequest',
          'token': !isLogin ? userInfo.token : '',          
        },
        params: options.params ? options.params : '',
        data: JSON.stringify(options.data) === "{}" ? null : JSON.stringify(options.data),

      }).then(res => {       
        if (res.status === 200) {
          let resp = res.data;
          if (res.data.code == 0) {
            resolve(resp.data)
          }else if(res.data.code === -2){
            window.location.href = '#/login'
            return
          }else {
            if ((res.data.msg && res.data.msg.includes('token')) || (res.data.msg && res.data.msg.includes('在别处登录')) ) {
              Storage.clearToken()
              window.location.href = '#/login'
              return
            }
            if(res.data.msg && res.data.msg.length < 30){
              message.error(res.data.msg)  
              resolve(resp)           
              return
            }
            // message.error('请求超时,请重试');
          }
        } else {
          reject(res.data)
        }
      }).catch(err => {
        message.error('请求超时,请重试');
        reject(err)
      })
    })
 }
}