|
|
|
|
|
let http = {
|
|
|
proxy: null,
|
|
|
isJson: function(obj) {
|
|
|
var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() ==
|
|
|
"[object object]" &&
|
|
|
!obj.length;
|
|
|
return isjson;
|
|
|
},
|
|
|
reqErr : function(status){
|
|
|
if(status && status == '401'){
|
|
|
// 登录超时,刷新当前页面===>跳转到登录页面
|
|
|
window.location.reload();
|
|
|
}
|
|
|
},
|
|
|
post: function(requestUrl, data, callback) {
|
|
|
if(http.proxy == null){
|
|
|
const { proxy } = Vue.getCurrentInstance()
|
|
|
http.proxy = proxy;
|
|
|
}
|
|
|
|
|
|
const loading = http.proxy.$global.showLoading();
|
|
|
var access_token = 'access_token=' + http.getToken();
|
|
|
if (requestUrl.indexOf('?') == -1) {
|
|
|
requestUrl += '?' + access_token;
|
|
|
} else {
|
|
|
requestUrl += '&' + access_token;
|
|
|
}
|
|
|
$.ajax({
|
|
|
url: sessionStorage.getItem('domainName') + requestUrl,
|
|
|
method: "post",
|
|
|
processData: false,
|
|
|
contentType: "application/json;charset=UTF-8",
|
|
|
dataType: "JSON",
|
|
|
async: true,
|
|
|
data: JSON.stringify(data),
|
|
|
error: function(xhr, textStatus) {
|
|
|
loading.close();
|
|
|
console.log("==>",requestUrl, xhr, textStatus)
|
|
|
http.reqErr(textStatus.status);
|
|
|
},
|
|
|
success: function(data, textStatus, jqXHR) {
|
|
|
if (callback) {
|
|
|
callback(data);
|
|
|
}
|
|
|
loading.close()
|
|
|
//console.log("==>", requestUrl, jqXHR, textStatus)
|
|
|
|
|
|
}
|
|
|
});
|
|
|
|
|
|
},
|
|
|
get: function(requestUrl, data, callback,errFunc) {
|
|
|
if(http.proxy == null){
|
|
|
const { proxy } = Vue.getCurrentInstance()
|
|
|
http.proxy = proxy;
|
|
|
}
|
|
|
const loading = http.proxy.$global.showLoading();
|
|
|
var access_token = 'access_token=' + http.getToken();
|
|
|
if (requestUrl.indexOf('?') == -1) {
|
|
|
requestUrl += '?' + access_token;
|
|
|
} else {
|
|
|
requestUrl += '&' + access_token;
|
|
|
}
|
|
|
$.ajax({
|
|
|
url: sessionStorage.getItem('domainName') + requestUrl,
|
|
|
method: "get",
|
|
|
headers: {
|
|
|
"Authorization": "Bearer " + http.getToken() + ""
|
|
|
},
|
|
|
data: data,
|
|
|
error: function(xhr, textStatus) {
|
|
|
loading.close();
|
|
|
console.log("==>",requestUrl, xhr, textStatus)
|
|
|
http.reqErr(textStatus.status);
|
|
|
|
|
|
if(errFunc){
|
|
|
errFunc();
|
|
|
}
|
|
|
|
|
|
},
|
|
|
success: function(data, textStatus, jqXHR) {
|
|
|
if (callback) {
|
|
|
callback(data);
|
|
|
}
|
|
|
loading.close();
|
|
|
//console.log("==>", requestUrl, jqXHR, textStatus)
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
downloadFile(fileName, content) {
|
|
|
var filename = fileName;
|
|
|
var a = document.createElement('a')
|
|
|
var blob = new Blob([content])
|
|
|
a.download = filename
|
|
|
a.href = URL.createObjectURL(blob)
|
|
|
a.click()
|
|
|
URL.revokeObjectURL(blob)
|
|
|
},
|
|
|
getToken() {
|
|
|
return localStorage.getItem('access_token');
|
|
|
},
|
|
|
|
|
|
// showMsg(msg, type = 'success') {
|
|
|
// ElementPlus.ElMessage.success({
|
|
|
// message: msg,
|
|
|
// type: type,
|
|
|
// })
|
|
|
// },
|
|
|
|
|
|
// showLoading(callback) {
|
|
|
// const loading = ElementPlus.ElLoading.service({
|
|
|
// lock: true,
|
|
|
// text: '加载中...',
|
|
|
// spinner: 'el-icon-loading',
|
|
|
// background: 'rgba(0, 0, 0, 0.7)',
|
|
|
// });
|
|
|
|
|
|
// if (callback) {
|
|
|
// callback(loading);
|
|
|
// }
|
|
|
// return loading;
|
|
|
// }
|
|
|
}
|
|
|
|
|
|
export default http |