Newer
Older
operation_web / src / api / APIindex.js
import axios from 'axios';
import router from '@/router';

import { Message } from "element-ui";
import * as locasto from "./../util/item";

axios.defaults.timeout = 120000;
axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded;charset=utf-8";

// 添加请求拦截器
axios.interceptors.request.use(
  function(config) {
    // 添加headers到post请求中
    // config.headers.post['Content-Type'] = 'application/json;charset=utf-8';
    // let USERNO = JSON.parse(locasto.getItem('USERNO'));
    config.headers.post["Content-Type"] = "application/json;charset=utf-8";
    if (
      config.url == "http://www.yunplc.com:7080/exlog" ||
      config.url.indexOf("120.27.155.137:7080") != -1
    ) {
      return config;
    }
    let USERNO = locasto.getItem("USERNO");
    let token = locasto.getItem("token");
    // console.log(USERNO)
    if (USERNO) {
      config.headers.USERNO = USERNO;
    }
    if (token) {
      config.headers.token = token;
    }
    // if (!store.getters.disting) {
    //     // store.commit('system/loading', true);
    // }
    // 在发送请求之前做些什么
    return config;
  },
  function(error) {
    // 对请求错误做些什么
    // store.commit('system/loading', false);
    return Promise.reject(error);
  }
);

// 添加响应拦截器
axios.interceptors.response.use(
  function(response) {
    // 对响应数据做点什么
    // store.commit('system/loading', false);

    // const status = Number(response.status) || 200;

    // if (status !== 200 || response.data.code === 1) {
    //     Message({
    //         'message': `数据请求出现${response.data.code},请联系管理员 !`,
    //         'type': 'error'
    //     });
    // }
    return response;
  },
  function(error) {
    // 对响应错误做点什么
    // Message({
    //     'message': '请求服务超时 , 服务器关闭或系统服务异常 , 请联系管理员 !',
    //     'type': 'error',
    //     'duration': 5000
    // });
    // store.commit('system/loading', false);
    // return Promise.reject(error);
  }
);

export const http = {
  get(url, params = {}) {
    return new Promise((resolve, reject) => {
      axios
        .get(url, params)
        .then(response => {
          resolve(response);
        })
        .catch(error => {
          reject(error);
        });
    });
  },
  delete(url, params) {
    return new Promise((resolve, reject) => {
      axios
        .delete(url, params)
        .then(response => {
          resolve(response);
        })
        .catch(error => {
          reject(error);
        });
    });
  },
  post(url, params = {}) {
    return new Promise((resolve, reject) => {
      axios
        .post(url, params)
        .then(function(data) {
          resolve(data);
        })
        .catch(error => {
          reject(error);
        });
    });
  },
  put(url, params) {
    return new Promise((resolve, reject) => {
      axios
        .put(url, params)
        .then(function(data) {
          resolve(data);
        })
        .catch(error => {
          reject(error);
        });
    });
  }
};