import Request from './request' import apiList from './api' // import store from '../../store/index.js' import Tools from '../tools.js' const TAG_NAME="REQUEST INDEX.JS"; let cacheSessions = {}; //会话存储 //data._isRefresh用于第二次强制请求 export default function api(url, data = {},isNeedLoading=false) { const request = new Request(); let api = getApiObj(url); // console.log('api---',api,url); let cacheType = api.cacheType; const cacheKey = getCacheKey('',url,data) /** * 正常:return加工后config,用于真正request * 异常:调用cancel后,request不在向下进行。type:2 resolve缓存结束。1reject */ request.interceptor.request((config, cancel) => { /* 请求之前拦截器 */ const token = uni.getStorageSync('token') || ''; // console.log('request.interceptor.request',config); let res; //_isRefresh用于第二次请求 if( cacheType === 'local' && !data._isRefresh){ res = uni.getStorageSync(cacheKey); }else if( cacheType === 'session' && !data._isRefresh){ // console.log('request.interceptor cacheType',cacheType,cacheSessions[cacheKey]) // 第一次session--要判断否则JSON.parse报错 if(cacheSessions[cacheKey]) res = JSON.parse(cacheSessions[cacheKey]); } if(res){ //调用者继续向下运行 cancel,本方法继续向下进行 cancel('已获得请求值,不再请求',res,2) // console.log('已获得请求值,不再请求---',cacheType,cacheSessions,res); return; }else if (api.auth && !token) { //request 调用者直接报错不再向下进行。本方法继续向下进行 cancel('暂未登录,已阻止此次API请求~') setTimeout(()=>{ uni.redirectTo({ url:'/pages/index/loginMobile' }) },800) return; } //正常情况下,返config用于下步请求~ // console.log("request index",config); if (token) { config.data={...config.data,token}; } return config; }); request.interceptor.response((response) => { /* 请求之后拦截器 */ //200情况 const data = response.data; if( cacheType === 'local'){ (data && data.code === 1) && uni.setStorage({key: cacheKey,data: data}) }else if(cacheType === 'session'){ // console.log('request.interceptor response before--',cacheKey,cacheSessions[cacheKey],JSON.stringify(data.data || '')); (data && data.code === 1) && (cacheSessions[cacheKey] = JSON.stringify(data || '')); //统一request.js message还要用提示 // console.log('request.interceptor response after----',cacheSessions[cacheKey]) } return response; }, (error) => { // 除200都在 ---request.js由validateStatus决定 console.log('error--',error) if (error.statusCode) { if(error.statusCode === 401){ const pages = getCurrentPages() if(pages.length > 1 && pages[pages.length - 1].route== 'pages/login/login'){ //解决同时多个请求,都跳登录页情况 reject(TAG_NAME,'已在登录页,无需在次进入') return; } // console.log(TAG_NAME,options.url,'重新登录~') uni.removeStorageSync('token') uni.navigateTo({url:'/pages/login/login'}) return; // setTimeout(()=>{ // //已有提示 // uni.redirectTo({ // url:'/pages/index/loginMobile' // }) // },1200) } Tools.msg(error.statusCode+' '+(error.data.msg|| '请求失败~')) }else{ Tools.msg('请求失败~') } return error; }) return request.request({ url: api.url, data, method: api.method, isNprogress: isNeedLoading || api.isNprogress, header: api.header, }) } function getApiObj(url) { let apiArray = url.split("."); let api = apiList; apiArray.forEach(v => { api = api[v]; }); return api; } function getCacheKey(k, url,data={}){ // mydate.getDay()+ mydate.getHours()+ mydate.getMinutes()+mydate.getSeconds()+mydate.getMilliseconds()+ Math.round(Math.random() * 10000); const key = k ? k : url; return "cache-"+key+JSON.stringify(data) }