import { Message} from 'element-ui'// Import loginInfo from from the element popup'.. /store/loginInfo.js'
import router from '.. /router'// Import routes var root ='http://app.senpuyun.com/'Var axios = require('axios'// reference axios // custom judge element type JSfunction toType(obj) {
  return({}). ToString. Call (obj). The match (/ \ s ([a zA - Z] +) /) [1]. The toLowerCase ()} / / parameter filter functionfunction filterNull(o) {
  for (var key in o) {
    if (o[key] === null || o[key] === ' ' ) {
      delete o[key]
    }
    if (toType(o[key]) === 'string') {
      o[key] = o[key].trim()
    } else if (toType(o[key]) === 'object') {
      o[key] = filterNull(o[key])
    } else if (toType(o[key]) === 'array') {
      o[key] = filterNull(o[key])
    }
  }
  returnO} // Encapsulate axiosfunction apiAxios(method, url, params, success, failure) {
  if (params) {
    params = filterNull(params)
  }
  axios({
      method: method,
      url: url,
      data: method === 'POST' || method === 'PUT' ? params : null,
      params: method === 'GET' || method === 'DELETE' ? params : null,
      baseURL: root,
      withCredentials: true// enable cookies}). Then (function (res) {
      if (res.data.status === 1000 || res.data.status === 1005 || res.data.status === 1010) {
        if (success) {
          if(array.isarray (res.data.data) && res.data.data.length === 0) {// The background data is not standard, Res.data.data. list = []} success(res.data)}}else {
        if (failure) {
          Message.error(res.data.msg)
          failure(res.data)
        } else if (res.data.status === 1016) {
          Message.error('Login failed, please log in again! ')
          loginInfo.setIslogin(false)
          router.push('/login')}else {
          Message.error(res.data.msg)
        }
      }
    })
    .catch(function (err) {
      if (err) {
        err = String(err)
        if (err.includes('Network Error')) {
          Message.error('Please check the network. There's something wrong with the network.')}else if (err.includes('TypeError: Cannot read property')) {
          return false
        } else {
          Message.error('There's something wrong with the server.'}}})} // Returns the call interface in the Vue templateexport default {
  get: function (url, params, success, failure) {
    return apiAxios('GET', url, params, success, failure)
  },
  post: function (url, params, success, failure) {
    return apiAxios('POST', url, params, success, failure)
  },
  put: function (url, params, success, failure) {
    return apiAxios('PUT', url, params, success, failure)
  },
  delete: function (url, params, success, failure) {
    return apiAxios('DELETE', url, params, success, failure)
  },
  root: root
}
Copy the code