| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import Vue from 'vue';
- import qs from 'qs';
- export default function ({ $axios, redirect, req, ...args }) {
- $axios.onRequest(config => {
- const contentType = config.headers['Content-Type'];
- const isUpload = contentType === 'multipart/form-data' || contentType === 'application/json';
- if (!isUpload) {
- config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
- }
- console.log('Before, making request to ', config.url, config.baseURL)
- const referer = config.headers.host || (config.headers.common && config.headers.common.referer);
- const baseUrl = /https?/.test(referer) ? referer : 'https://' + referer;
- const url = config.url;
- // if (referer && !(/https?/.test(url))) {
- if (typeof window === 'undefined') {
- // server http
- config.url = baseUrl + url;
- config.baseURL = baseUrl;
- } else {
- // client http
- config.url = /https?/.test(url) ? `/${url.split('/').slice(3).join('/')}` : url;
- config.baseURL = '';
- }
- // stringify post data
- if (config.method === 'post' && !isUpload) {
- const data = config.data;
- console.log(data)
- if (typeof data != "string") {
- // const cookie = config.headers.cookie || config.headers.common.cookie || '';
- let urlParams = [];
- for(const key in data) {
- if(data.hasOwnProperty(key)) {
- const element = encodeURIComponent(data[key])
- urlParams.push(`${key}=${element}`)
- }
- }
- config.data = urlParams.join('&');
- }
- }
- 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 includePath = ['/user/register', '/cert/type/:id', '/type/vip/', '/cert/', '/user/:id'];
- const needLogin = path => {
- let result = false;
- 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 || data.filename) {
- } else if(data.status === -99) {
- console.log(req && req.url)
- // if (req && req.url && needLogin(req.url)) {
- // redirect('/?loginbox=show');
- // }
- // return {code: '401', message: '请登录!'};
- } else {
- Vue.prototype.$message.error(data.info);
- // return data;
- }
- return res;
- })
- $axios.onError(error => {
- console.log('err', error);
- // const code = parseInt(error.response && error.response.status)
- // if (code === 400) {
- // redirect('/400')
- // }
- })
- }
|