| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- 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 = (req && req.headers && req.headers.host) || (config.headers.common && config.headers.common.referer);
- const baseUrl = /https?/.test(referer) ? referer : 'http://' + 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;
- /**
- * 设备判断
- */
- const getDeviceType = function() {
- const strCookie = process.server
- ? req.headers.cookie
- : document.cookie;
- //将多cookie切割为多个名/值对
- const arrCookie = strCookie.split("; ");
- let xAPP = "";
- //遍历cookie数组,处理每个cookie对
- for(let i=0; i<arrCookie.length; i++){
- const arr = arrCookie[i].split("=");
- //找到名称为x_app的cookie,并返回它的值
- if ("x_app" === arr[0]){
- xAPP = arr[1];
- break;
- }
- }
- if (xAPP.toLowerCase().indexOf('ios') !== -1) { //判断iPhone|iPad|iPod|iOS APP
- return 'ios';
- } else if (xAPP.toLowerCase().indexOf('android') !== -1) { //判断Android app
- return 'android';
- } else { // pc OR H5 todo 判断UA,精准识别是H5还是PC
- return 'pc';
- }
- }
- console.log('res:::::::::')
- // console.log(res);
- console.log(res.config)
- console.log(data)
- // req && req.url && needLogin(req.url)
- let needLogin = false
- if (res.config && res.config.data && typeof res.config.data === 'string') {
- const queryData = res.config.data
- //将多cookie切割为多个名/值对
- const arrQuery = queryData.split("&");
- //遍历cookie数组,处理每个cookie对
- for(let i=0; i<arrQuery.length; i++){
- const arr = arrQuery[i].split("=");
- //找到名称为x_app的cookie,并返回它的值
- if ("needLogin" === arr[0] && (arr[1] === 'true' || arr[1] === true)){
- needLogin = true;
- break;
- }
- }
- }
- if (data.status === 1 || data.filename) {
- } else if (needLogin && data.status === -99) {
- const deviceType = getDeviceType();
- if (deviceType === 'ios' || deviceType === 'android') {
- if (process.server) {
- redirect('proginn://login')
- } else {
- location.href = 'proginn://login'
- }
- } else {
- if (process.server) {
- redirect('https://www.proginn.com/?loginbox=show')
- } else {
- location.href = 'https://www.proginn.com/?loginbox=show'
- }
- }
- 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')
- // }
- });
- }
|