vue axios 封装request.js

import axios from 'axios';
axios.defaults.withCredentials = true //跨域
axios.defaults.headers.post['Content-Type'] = 'application/x-www=form-urlencoded'

const service = axios.create({
    timeout: 5000,
});

axios.interceptors.request.use(
    config => {
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

//get请求
service.requestGet = function (url, params) {
    return new Promise((resolve, reject) => {
        axios.get(url, params).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
};
//get请求不带参数
service.requestQuickGet = function (url) {
    return new Promise((resolve, reject) => {
        axios.get(url).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
}
//get导出excel
service.requestExportGet = function (url) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            headers: {
                'Content-Type': 'application/vnd.ms-excel',
            },
        }).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
};

//post请求
service.requestPost = function (url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, params).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
};
//用于下载的post请求
service.requestDownloadPost = function (url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, params, {
            responseType: 'blob',
            headers: {
                'Content-Type': 'application/json; application/octet-stream'
            }
        }).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
},
//post请求
service.requestPostForm =function(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, params, {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
        }).then(res => {
            resolve(res.data)//注意res是axios封装的对象,res.data才是服务端返回的信息
        }).catch(error => {
            reject(error)
        })
    })
};
//put请求
service.requestPut = function (url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, params).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
};
//delete请求
service.requestDelete = function (url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.delete(url, params).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
};

//上传excel文件
// uploadExcel(file) {
//   let formData = new FormData();
//   formData.append('file', file.file);
//   axios({
//     method: "post",
//     url: "/api/orderMaster/excel",
//     data: formData,
//     headers: {'Content-Type': 'multipart/form-data'}
//   }).then(res => {
//     console.log(res);
//     let status = res.status;
//     this.$refs.upload.clearFiles()
//   }).catch(error=>{
//     console.log(error)
//     this.$refs.upload.clearFiles()
//   })
// }


//export const xxxx= (page, size,query) => {
//    return request.requestPost('/xxx/' + page + '/' + size,query)
//}


export default service;

你可能感兴趣的:(vue)