import axios from 'axios'; let env = process.env.NODE_ENV; var needLogin = ""; let root = ''; var header=''; if (env === 'development') { console.log("api"); root = ''; } 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("服务器连接失败!请检查您的网络或服务器!!"); return; } else if (res.status == 401) { if (authFail) { localStorage.setItem('login', ''); authFail(res); } else { // var msg = "客户端未登录请重新登录!"; // if (confirm(msg) == true) { showHeaderSignin(); // sessionStorage.setItem('needLogin', '1'); // window.location.href = window.location.href.split(host)[0] + host; // let argu = {needLogin:true}; // this.$router.push({ // name: 'tzdIndex', // params: argu // }); // window.location.reload(); // } return; } } else if(res.status == 750){ var host = window.location.host; window.location.href = window.location.href.split(host)[0] + host; } if (failure) { failure(res); } else { window.alert(res.data); } } else { if(authFail){ localStorage.setItem('login', ''); authFail(res); }else{ window.alert(err); } } // else if (err && !res) { // // 跨域也会出现此提示 // var msg = "客户端未登录请重新登录(如非登录超时请反馈给技术)!"; // if (confirm(msg) == true) { // Cookies.remove('user'); // Cookies.remove('password'); // Cookies.remove('access'); // window.location.reload(); // } // } }); } // 返回在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) { header = headerModel; if (needLogin && needLogin == "1") { header.showSignin(); } } }; function showHeaderSignin() { if (header) { if(localStorage.getItem('login') == "logining"){ alert.eduToast('登录已过期请重新登录!',2000); } localStorage.setItem('login', ''); header.showSignin(); } else { localStorage.setItem('login', ''); needLogin = "1"; } }