fetch-api.js 3.07 KB
import axios from 'axios';
import { Message, MessageBox } from 'element-ui'
import store from '../store'
import { getToken } from '@/utils/auth'
// import {
//   Toast
// } from 'vant';

function Toast(msg) {
  console.log("msg:", msg);
}

// axios的默认url
// axios.defaults.baseURL = ""

// 服务器地址

let requestDomain = "https://ow.go.qudone.com";
if (location.href.indexOf("://k.wxpai.cn") > 0 || location.href.indexOf("://h5.k.wxpai.cn") > 0) {
  requestDomain = "https://api.k.wxpai.cn/bizproxy";
}

/**
 * header不加session的白名单
 */
let headerWhiteList = [
  "kdapi/file/upload"
]

// request拦截器
/*axios.interceptors.request.use(
  config => {
    let addSession = true;
    for (let index = 0; index < headerWhiteList.length; index++) {
      if (config.url.indexOf(headerWhiteList[index]) > -1) {
        addSession = false;
        break;
      }
    }
    if (addSession) {
      //config.headers['adminSessionId'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
    }
    return config
  },
  error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  }
)*/
// 响应拦截器
axios.interceptors.response.use(
  response => {
    let res = response.data;
    console.log(res);
    if (response.data.code === 200) {
      return Promise.resolve(response.data);
    } else {
      if (res.code == 404) {
        store.dispatch('FedLogOut').then(() => {
          location.reload() // 为了重新实例化vue-router对象 避免bug
        })
      } else {
        if (res.errMsg) {
          Message({
            message: res.errMsg,
            type: 'error',
            duration: 5 * 1000
          })
        }
        return Promise.resolve(response.data);
      }
    }
  },
  error => {
    return Promise.reject(error);
  }
);

//formDataHeaders设置
let formDataHeaders = {
  headers: {
    "Content-Type": "multipart/form-data",
  }
}

/**
 * 封装post方法
 * @param {*} params
 * data数据是 formdata格式
 * 例如:
 *          this.file = file
 let data = new FormData() //使用formData对象
 data.append('path', '/pro/mzczcradmin/')
 data.append('file', file.file)
 */
export const formdata = (url, data) => {
  return axios.post(`${requestDomain}${url}`, data, formDataHeaders).then(res => res);
}


export const request = {
  post(url, data) {
    return axios.post(`${requestDomain}${url}`, data);
  },
  get(url, data) {
    return axios.get(`${requestDomain}${url}`, { params: data });
  },
  form(url, params) {
    let formData = new FormData(); //使用formData对象
    for (let key in params) {
      formData.append(key, params[key]);
    }
    let requestUrl = url.indexOf("://") >= 0 ? `${url}` : `${requestDomain}${url}`;
    return axios.post(requestUrl, formData, formDataHeaders)
  },
  build(url, params){
    let fullUrl = `${requestDomain}${url}`;
    let split = "";
    for(let key in params){
      if(split){
        split = "&";
      } else {
        split = "?"
      }
      fullUrl += split + key +"="+params[key];
    }
    return fullUrl;
  },
  /*test*/
};