繁花不似锦
// 拦截ajax请求,检测是否超时,以重新登录$(document).ajaxComplete((event, xhr, settings) => { if (xhr.status === 200) { if (settings.dataType === 'json' && xhr.responseJSON !== void 0) { let result = xhr.responseJSON; if (2001 === result.code) { // 没有session登录信息时跳转至登录页 global.location.href = "/main-login.html"; } } } else if (xhr.status === 401) {} else { global.location.href = "/main-login.html"; }});export default function(options) { const defaultOptions = { dataType: 'json', cache: true, jsonp: 'callback' }; options.data = processRequest(options); //url这里加一些代理路径。。。 options.url = options.url; options.headers = { "Accept": "application/json", "Content-Type": "application/json" }; return $.ajax({...defaultOptions, ...options }).then(processResponse);};// 标准化传给后台的参数function processRequest(r) { const str = r.data || {}; if ('get' == r.method) { if ($.isEmptyObject(str) || null == str) { return { t: new Date().getTime() }; } else { return { //添加时间戳随机数 params: JSON.stringify(str), t: new Date().getTime() }; } } else { return JSON.stringify(str); }}// 标准化后台相应数据格式function processResponse(r) { let str = {}; if (r.rows) { str = r; str.code = 0; str.list = r.rows; delete str.rows; } else { if (!r.error) { if (0 <= r.code) { str = r; } else { str.code = 0; str.data = r; } } else { str.code = -1; str.message = r.message || r.error; } } return str;}