| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import Vue from 'vue';
- 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') {
- // 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 {
- Vue.prototype.$message.error(data.info);
- return data;
- }
- })
- $axios.onError(error => {
- console.log('err', error);
- // const code = parseInt(error.response && error.response.status)
- // if (code === 400) {
- // redirect('/400')
- // }
- })
- }
|