vue中前端接口实现

采用axios实现前后台数据交互,安装axios和qs

import axios from 'axios'
import qs from 'qs'

//post请求头
axios.defaults.baseURL = 'http://192.168.30.193:8080';
//axios.defaults.baseURL = '';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

// http response 拦截器
axios.interceptors.response.use(
    response => {
      let link = window.location.href;
      let l = link.substring(link.indexOf('#'), link.length);
      switch(response.data.code){
        case 200:
            return response;
        case 900:
            location.href = `/?redirect=/selfstation${l}`;
            return;
      }
    },
    error => {
      if (error.response) {
        switch (error.response.data.code) {
          case 0:
            this.$toast.center('获取数据失败');
        }
      }
    return Promise.reject(error.response.data);
});

//post传值
export const postMarketingCensus = (data) => {
    let param = new URLSearchParams();
    if(typeof data === 'string'){
        param.append('type', data);
    }else{
        param.append('type', '2');
        param.append('condition', JSON.stringify(data));
    }
    return axios.post(`${axios.defaults.baseURL}/plus/census/marketing_census`, param);
};
//post传值2
export const postStatisticsDataList = (params) => {
    return axios.post(`${axios.defaults.baseURL}/plus/tovisit/getStatisticsDataList`, qs.stringify(params));
};
//get传值
export const getDisclosureCheckedBrands = (n) => {
    return axios.get(`${axios.defaults.baseURL}/contract/risk/grtb?crm_risk_id=${n}`);
}

 

posted @ 2020-02-14 17:10  shirliey  阅读(...)  评论(...编辑  收藏