You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.8 KiB
65 lines
1.8 KiB
import Vue from "vue";
|
|
import axios from 'axios';
|
|
const qs = require('qs');
|
|
var apiServer = (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl);
|
|
var http = axios.create({
|
|
timeout: 10000,
|
|
headers: {},
|
|
transformRequest: data => {
|
|
if (typeof data === 'object') {
|
|
return qs.stringify(data, { arrayFormat: 'brackets' });
|
|
}
|
|
return data;
|
|
}
|
|
});
|
|
export default {
|
|
axios: http,
|
|
$post(url, data, callback, error) {
|
|
// 添加 headers 配置项
|
|
const config = {
|
|
headers: {
|
|
'Content-Type': 'application/json;charset=UTF-8',
|
|
'token': Vue.cookie.get('token')
|
|
}
|
|
};
|
|
let p = http.post(apiServer +url, JSON.stringify(data), config);
|
|
if (callback) {
|
|
p.then(res => {
|
|
callback(res);
|
|
})
|
|
}
|
|
if (error) {
|
|
p.catch(err => {
|
|
error(err);
|
|
});
|
|
}
|
|
return p;
|
|
},
|
|
file:(url, data, succ, err )=>{
|
|
let requestUrl = apiServer + url;
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.open('POST', requestUrl, true);
|
|
xhr.responseType = "blob";
|
|
xhr.withCredentials = true;
|
|
xhr.setRequestHeader("X-API", "true");
|
|
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
|
xhr.setRequestHeader("token", Vue.cookie.get('token') );
|
|
xhr.onreadystatechange = function (e) {
|
|
if (xhr.readyState === 4) {
|
|
if (xhr.status === 200) {
|
|
succ(xhr.response)
|
|
} else {
|
|
var fr = new FileReader();
|
|
fr.onload = function(e) {
|
|
var text = fr.result;
|
|
console.log(text);
|
|
err ? err(text) : alert("请求发生错误:" + text)
|
|
};
|
|
fr.readAsText(xhr.response);
|
|
}
|
|
}
|
|
};
|
|
xhr.send(data);
|
|
},
|
|
apiServer: apiServer
|
|
};
|