其他分享
首页 > 其他分享> > axios封装

axios封装

作者:互联网

import { axios } from '@/utils/request'

// get-post-exports定义
export function axiosCommon(method, url, data, blob) {
const params = {
url: url,
method: method
}
if (method === 'get' || method === 'delete') {
params.params = data
} else if (method === 'post') {
params.data = data
}
if (blob && blob !== 'down') {
const formData = new FormData()
formData.append('file', data)
params.data = formData
}
if (blob === 'down') {
params.responseType = 'blob'
return new Promise(resolve => {
axios(params).then(res => {
resolve(res)
})
})
} else {
return new Promise(resolve => {
axios(params)
.then(res => {
if (res.code === 200) {
resolve(res.result)
} else {
alert(res.message)
}
})
.catch(error => {
alert(error)
})
})
}
}

export const upLoad = data => axiosCommon('post', '/xxx/xxx/xxx/xxx', data, true)

export const get/post/delete= data => axiosCommon('post', '/xxx/xxx/xxx/xxx', data, false)

import {upLoad} from '/api/xxx'

upLoad({}).then(res=>{}).catch(err=>{})

标签:axios,封装,res,xxx,params,blob,data,method
来源: https://www.cnblogs.com/youngMan-MrS/p/16492262.html