import qs from 'qs'; export default function ({ $axios, redirect, req, ...args }) { $axios.onRequest(config => { config.headers['Content-Type'] = 'application/x-www-form-urlencoded'; console.log('Before, making request to ', config.url, config.baseURL) const referer = config.headers.common && config.headers.common.referer; const url = config.url; if (referer && !(/https?/.test(url))) { // server http config.url = referer.split('/').slice(0, 3).join('/') + url; config.baseURL = referer.split('/').slice(0, 3).join('/'); } else { // client http config.url = /https?/.test(url) ? `/${url.split('/').slice(3).join('/')}` : url; config.baseURL = ''; } // stringify post data if (config.method === 'post') { console.log(config); // config.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; const data = config.data; const cookie = config.headers.cookie || config.headers.common.cookie || ''; let formData = '' for(const key in data) { if(data.hasOwnProperty(key)) { const element = data[key] formData += `${key}=${element}&` } } formData += cookie.split(';').join('&'); config.data = formData; // config.data = qs.stringify(config.data); } console.log('After, making request to ', config.url, config.baseURL) return config; }) $axios.onResponse(res => { const data = res.data; // 不跳转到login的页面reg list const excludePath = ['/user/register', '/cert/type/:id', '/type/vip/', '/cert/', '/user/:id']; const needLogin = path => { let result = true; excludePath.forEach(reg => { reg = reg.replace(/:id/, ''); if (RegExp(reg).test(path)) { result = false; } }) return result; } req && req.url && needLogin(req.url) if(data.status === 1) { return res; } else if(data.status === -99) { if (req && req.url && needLogin(req.url)) { redirect('/?loginbox=show'); } return res; } else { return res; } }) $axios.onError(error => { console.log('err', error); // const code = parseInt(error.response && error.response.status) // if (code === 400) { // redirect('/400') // } }) }