// 这是一个对http请求出现不同的code的处理 // import router from '../router/index'; import { Loading } from 'element-ui' // import { Promise } from 'core-js'; let loading var currentStates = { // 当前的状态集合 可以设置那些是在请求完成之后关闭的loading 200: true, // 可以在请求之后结束loading 404: false, // 出现404的时候不取消loading 502: false // 出现502的时候不取消loading } var currentStateCode // 当前的状态 // todo 补全状态码报错 var httpEventCode = { code200(data) { // 200拦截 currentStateCode = 200 if (data.data.status === 1) { // router.replace({path:'/login'}) // console.log(data.data.errMsg,'errMsg') } // console.log(data,200) }, code502() { currentStateCode = 502 loading.close() loading = Loading.service({ lock: true, text: '服务器异常502', background: 'rgba(0, 0, 0, 0.7)' }) // 404 拦截 // console.log(data,404) }, code404() { // 404 拦截 currentStateCode = 404 loading.close() loading = Loading.service({ lock: true, text: '请求服务器404', background: 'rgba(0, 0, 0, 0.7)' }) }, code400() { // 400拦截 // console.log(data,400) } } function startRequest(httpObj, requestObj) { console.log(requestObj) if (requestObj && requestObj.udData && requestObj.udData.noLoading) { return } // console.log('请求前') loading = Loading.service({ lock: true, text: '加载中……', // background: 'rgba(0, 0, 0, 0.7)' background: 'rgba(255,255,255,0.6)' }) } function endRequest() { // console.log('请求后') } function concurrentRequests() { // 检测一次多个请求全部完成之后触发 if (currentStates[currentStateCode]) { // 只有正确的code才可以被取消loading loading && loading.close() } } // function defindFlow(o) { // // 自定义流程与返回数据 // // o.requestConfig 请求参数 // // o.res 上一次的返回结果 // // o.Request 总请求方法 // return new Promise((resolve, reject) => { // // console.log(o) // }) // } // defindFlow export { httpEventCode, startRequest, endRequest, concurrentRequests }