|
|
@@ -1,137 +1,85 @@
|
|
|
-import Vue from 'vue';
|
|
|
-import qs from 'qs';
|
|
|
+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 proto = (referer && referer.indexOf('local.proginn.com') !==-1) ? "http://" : "https://"
|
|
|
- const baseUrl = /https?/.test(referer) ? referer : proto + 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;
|
|
|
- });
|
|
|
+export default function({ $axios, redirect, req, store, app, ...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);
|
|
|
+ config.baseURL = store.state.domainConfig.api;
|
|
|
|
|
|
- $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;
|
|
|
+ // stringify post data
|
|
|
+ if (config.method === "post" && !isUpload) {
|
|
|
+ const data = config.data;
|
|
|
+ console.log("stringify req data............");
|
|
|
+ console.log(JSON.stringify(data));
|
|
|
+ if (typeof data != "string") {
|
|
|
+ let urlParams = [];
|
|
|
+ for (const key in data) {
|
|
|
+ if (data.hasOwnProperty(key)) {
|
|
|
+ const element = encodeURIComponent(data[key]);
|
|
|
+ urlParams.push(`${key}=${element}`);
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- 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';
|
|
|
+ config.data = urlParams.join("&");
|
|
|
}
|
|
|
}
|
|
|
- console.log('res:::::::::')
|
|
|
+ console.log("After, making request to ", config.url, config.baseURL);
|
|
|
+ return config;
|
|
|
+ });
|
|
|
+
|
|
|
+ $axios.onResponse(res => {
|
|
|
+ const data = res.data;
|
|
|
+ console.log("res:::::::::");
|
|
|
+ console.log(app.$deviceType);
|
|
|
// 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切割为多个名/值对
|
|
|
+ console.log(res.config.url);
|
|
|
+ console.log(JSON.stringify(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;
|
|
|
+ // 将请求入参重新解析
|
|
|
const arrQuery = queryData.split("&");
|
|
|
- //遍历cookie数组,处理每个cookie对
|
|
|
- for(let i=0; i<arrQuery.length; i++){
|
|
|
+ // 遍历每个入参,看有没有needLogin参数,并且参数是true, 表示该接口如果服务器端返回未登录,则强制跳到登录页面
|
|
|
+ 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
|
|
|
+ if ("needLogin" === arr[0] && (arr[1] === "true" || arr[1] === true)) {
|
|
|
needLogin = true;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if (data.status === 1 || data.filename || data.sign) {
|
|
|
- } else if (needLogin && data.status === -99) {
|
|
|
- const deviceType = getDeviceType();
|
|
|
- if (deviceType === 'ios' || deviceType === 'android') {
|
|
|
+ if (data.status === 1 || data.filename || data.sign) {
|
|
|
+ } else if (needLogin && data.status === -99) {
|
|
|
+ if (app.$deviceType.app) {
|
|
|
if (process.server) {
|
|
|
- redirect('proginn://login')
|
|
|
+ redirect("proginn://login");
|
|
|
} else {
|
|
|
- location.href = 'proginn://login'
|
|
|
+ location.href = "proginn://login";
|
|
|
}
|
|
|
} else {
|
|
|
if (process.server) {
|
|
|
- redirect('https://www.proginn.com/?loginbox=show')
|
|
|
+ redirect(store.state.domainConfig.siteUrl + "/?loginbox=show");
|
|
|
} else {
|
|
|
- location.href = 'https://www.proginn.com/?loginbox=show'
|
|
|
+ location.href = store.state.domainConfig.siteUrl + "/?loginbox=show";
|
|
|
}
|
|
|
}
|
|
|
- console.log(req && req.url);
|
|
|
- // if (req && req.url && needLogin(req.url)) {
|
|
|
- // redirect('/?loginbox=show');
|
|
|
- // }
|
|
|
- // return {code: '401', message: '请登录!'};
|
|
|
- } else {
|
|
|
- // if (!Vue.hasMessage) {
|
|
|
- console.log(data)
|
|
|
- Vue.prototype.$message.closeAll()
|
|
|
- Vue.prototype.$message.error(data.info || '')
|
|
|
- // Vue.hasMessage = true
|
|
|
- // setTimeout(() => {
|
|
|
- // Vue.hasMessage = false
|
|
|
- // }, 3000)
|
|
|
- // }
|
|
|
- // return data;
|
|
|
- }
|
|
|
- return res;
|
|
|
- });
|
|
|
- $axios.onError((error) => {
|
|
|
- console.log('err', error);
|
|
|
- // const code = parseInt(error.response && error.response.status)
|
|
|
- // if (code === 400) {
|
|
|
- // redirect('/400')
|
|
|
- // }
|
|
|
- });
|
|
|
+ console.log(req && req.url);
|
|
|
+ } else {
|
|
|
+ console.log(JSON.stringify(data));
|
|
|
+ Vue.prototype.$message.closeAll();
|
|
|
+ Vue.prototype.$message.error(data.info || "");
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ });
|
|
|
+ $axios.onError(error => {
|
|
|
+ console.log("err", error);
|
|
|
+ });
|
|
|
}
|