import axios from 'axios';
import constant from '@/js/const';
import alert from '@/js/alertView';
let env = process.env.NODE_ENV;
var needLogin = "";
let root = '';
let yxHeader;
if (env === 'development') {
console.log("api");
} else if (env === 'production') {
console.log("pro");
root = '';
} else {
throw '请检查process.env.NODE_ENV的值,是否符合这些值之一:development,production';
}
Date.prototype.format = function (fmt) {
var o = {
"y+": this.getFullYear(),
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"H+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds() //秒
};
if (!fmt) {
fmt = 'yyyy-MM-dd HH:mm:ss';
}
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
}
// 自定义判断元素类型JS
function toType(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}
// 参数过滤函数
function filterNull(o) {
for (var key in o) {
if (o[key] === null) {
delete o[key];
}
if (toType(o[key]) === 'string') {
o[key] = o[key].trim();
} else if (toType(o[key]) === 'date') {
o[key] = (o[key].format());
} else if (toType(o[key]) === 'object') {
o[key] = filterNull(o[key]);
} else if (toType(o[key]) === 'array') {
o[key] = filterNull(o[key]);
}
}
return o;
}
function apiAxios(method, url, params, success, failure, authFail) {
console.log('url:' + url);
if (params) {
params = filterNull(params);
}
var base = "";
if (url.indexOf(".html") != -1) {
base = "";
} else {
base = root;
}
axios({
method: method,
url: url,
data: method === 'POST' || method === 'PUT' || method === 'DELETE' ? params : null,
params: method === 'GET' ? params : null,
baseURL: base,
withCredentials: true
}).then(function (res) {
if (res.status >= 200 && res.status <= 210) {
if (success) {
success(res);
}
} else {
//不走
// window.alert('error: ' + JSON.stringify(res.data));
}
}).catch(function (err) {
let res = err.response;
if (err && res) {
console.log(res.status);
if (res.status == 504) {
alert.eduToast("服务器连接失败!请检查您的网络或服务器!!",2000);
return;
} else if (res.status == 401) {
console.log('------------------:status'+res.status);
console.log('------------------:authFail'+authFail);
localStorage.setItem('login', '');
showHeaderSignin();
} else if(res.status == 750){
var host = window.location.host;
window.location.href = window.location.href.split(host)[0] + host + '?addressCode=' + constant.addressCode();
}
if (failure) {
failure(res);
} else {
alert.eduToast(res.data,2000);
}
} else {
if(authFail){
localStorage.setItem('login', '');
}else{
console.log(err);
}
}
});
}
// 返回在vue模板中的调用接口
export default {
get: function (url, params, success, failure,authFail) {
return apiAxios('GET', url, params, success, failure,authFail);
},
post: function (url, params, success, failure) {
return apiAxios('POST', url, params, success, failure);
},
put: function (url, params, success, failure) {
return apiAxios('PUT', url, params, success, failure);
},
delete: function (url, params, success, failure) {
return apiAxios('DELETE', url, params, success, failure);
},
initHeader: function (headerModel) {
yxHeader = headerModel;
console.log('------------------:initHeader'+yxHeader);
if (needLogin && needLogin == "1") {
yxHeader.mainLogin();
}
}
};
function showHeaderSignin() {
localStorage.setItem('login', '');
console.log('------------------:yxHeader'+yxHeader);
if (yxHeader) {
if(localStorage.getItem('login') == "logining"){
alert.eduToast('登录已过期请重新登录!',2000);
}
yxHeader.mainLogin();
} else {
needLogin = "1";
}
}