export function Fecth (url, data, file, _method) {
if (file) {
// 需要上传文件
return new Promise((resolve, reject) => {
axios({
url: url,
data: data,
headers: {
'Content-Type': 'multipart/form-data'
},
method: 'POST',
withCredentials: true
}).then(response => {
resolve(response)
})
.catch(error => {
reject(error)
})
})
} else {
// 不需要上传文件
return new Promise((resolve, reject) => {
axios({
url: url,
data: data,
transformRequest: [function (data) {
let ret = ''
for (let it in data) {
if ((typeof data[it]) === 'object') {
ret += encodeURIComponent(it) + '=' + encodeURIComponent(JSON.stringify(data[it])) + '&'
} else {
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
}
}
return ret
// let ret = qs.stringify(data)
// return ret
}],
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: _method || 'POST'
}).then(response => {
resolve(response)
}
)
.catch(error => {
reject(error)
}
)
})
}
}