请求接口封装以及请求

// import Vue from 'vue';
import axios from "axios"; // 引用axios
import config from "@/api/config";
  import store from '@/store';
//  import { ACCESS_TOKEN } from '@/store/mutations.type'; 
const instance = axios.create({
  baseURL: config.baseUrl.dev,
  timeout: 60000,
});


const err = (error) => {
    store.dispatch('setLoading', false);
    if (error.response) {
        if (error.response.status === 400 || error.response.status === 500) {
            store.dispatch('showModal', {
                type: 'alert',
                title: '抱歉',
                content: `请求错误:网络超时`,
                confirmBtn: '知道了'
            });
        }
        if (error.response.status === 403) {
            store.dispatch('showModal', {
                type: 'alert',
                title: '抱歉',
                content: '操作被禁止!',
                confirmBtn: '知道了'
            });
        }
    } else {
        store.dispatch('showToast', '当前网络差');
    }
    return Promise.reject(error);
};

//请求添加token
instance.interceptors.request.use((config) => {

  // const token = Vue.ls.get(ACCESS_TOKEN);
  // if (token) {
  //     config.headers['Authorization'] = 'Bearer ' + token;
  // } 
  if (config.header && config.header['Content-Type']) {
    if (config.header['Content-Type'] == 'application/x-www-form-urlencoded') {
      const formData = new FormData();
      let params = config.data;
      if (params) {
        Object.keys(params).forEach((key) => {
          formData.append(key, params[key]);
        });
        config.data = formData;
      }
      0
    }
  }
  if (config.header && config.header['responseType']) {
    config.responseType = config.header['responseType'];
  }
  if (config.header && config.header['downType']) {
    config.downType = config.header['downType'];
  }
  return config;
}, err);

//get请求
export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    instance
      .get(url, {
        withCredentials: true,
        params: params,
      })
      .then((response) => {
        resolve(response);
      })
      .catch((err) => {
        reject(err);
      });
  });
}
//post请求
export function post(url, data = {}) {
  return new Promise((resolve, reject) => {
    instance.post(url,{ withCredentials: true}, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}




//兴趣点
export const getInterestPlace = () => get("/interestPlace/tree");
//人员轨迹 
export const getTrajectoryWithInCircle = (params) => get("/bigScreen/trajectoryWithInCircle",params);


 TrajectoryWithInCircle(){ 
      let params = {x:"595704.826589992",y:"5242547.84022083",r:"5000"};
       this.loadview = true; 
       getTrajectoryWithInCircle(params)
        .then((response) => { 
          console.log(333,response)
          this.loadview = false;
        })
        .catch((error) => {
          console.log(error);
           this.loadview= false
        });
    }

猜你喜欢

转载自blog.csdn.net/snow_living/article/details/126888767