(function() { axios.defaults.timeout = 1000 * 60; axios.defaults.baseurl = ''; //http request 拦截器 axios.interceptors.request.use( function(config) { config.headers = { 'content-type': 'application/x-www-form-urlencoded', 'cache-control': 'no-cache', 'pragma': 'no-cache', 'x-requested-with': 'xmlhttprequest' } if (config.method === 'post' && config.headers["content-type"] === "application/x-www-form-urlencoded") { config.data = qs.stringify(config.data, { allowdots: true }); } return config; }, function(error) { return promise.reject(err); } ); //http response 拦截器 axios.interceptors.response.use( function(response) { //登录失效 if (response.data.bizcode == "401" && ms.isloginredirect) { window.parent.location.href = ms.base + "/" + ms.login + "?backurl=" + encodeuricomponent(window.parent.location.href); return; } return response; }, function(error) { return promise.reject(error) } ) function ajax(conf) { if (conf != undefined) { var _axios = axios.create({ baseurl: conf.baseurl == undefined ? axios.defaults.baseurl : conf.baseurl, timeout: conf.timeout == undefined ? axios.defaults.timeout : conf.timeout, headers: conf.headers == undefined ? null : conf.headers, }); _axios.interceptors.request.use( function(config) { if (config.method === 'post' && config.headers["content-type"] === "application/x-www-form-urlencoded") { config.data = qs.stringify(config.data, { allowdots: true }); } return config; }, function(error) { return promise.reject(err); } ); return _axios; } return axios; } /** * 封装get方法 * @param url * @param data * @returns {promise} */ function get(url, params) { if (params == undefined) { params = {} } return new promise(function(resolve, reject) { ajax().get(url, { params: params }) .then(function(response) { resolve(response.data); }) .catch(function(err) { reject(err) }) }) } /** * 封装post请求 * @param url * @param data * @returns {promise} */ function post(url, data, conf) { if (data == undefined) { data = {} } return new promise(function(resolve, reject) { ajax(conf).post(url, data, conf) .then(function(response) { resolve(response.data); }, function(err) { reject(err) }) }) } /** * 封装patch请求 * @param url * @param data * @returns {promise} */ function patch(url, data, conf) { if (data == undefined) { data = {} } return new promise(function(resolve, reject) { ajax(conf).patch(url, data, conf) .then(function(response) { resolve(response); }, function(err) { reject(err) }) }) } /** * 封装put请求 * @param url * @param data * @returns {promise} */ function put(url, data, conf) { if (data == undefined) { data = {} } return new promise(function(resolve, reject) { ajax(conf).put(url, data, conf) .then(function(response) { resolve(response.data); }, function(err) { reject(err) }) }) } var http = { get: get, post: post, put: put, patch: patch } if (typeof ms != "object") { window.ms = {}; } window.ms.http = http; window.ms.isloginredirect = true; }());