Browse Source

镜像api切换

wgl 4 years ago
parent
commit
9731f397a6
2 changed files with 86 additions and 2 deletions
  1. 43 1
      src/projects/pension/utils/request.js
  2. 43 1
      src/utils/request.js

+ 43 - 1
src/projects/pension/utils/request.js

@@ -3,6 +3,7 @@ import store from '$project/store';
 import router from '@/router';
 import Vue from 'vue';
 import Qs from 'qs';
+import $ from 'jquery';
 
 import {
 	getToken,
@@ -11,9 +12,50 @@ import {
 
 axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
 
+//检测是否可以访问通
+var baseApiList = [process.env.VUE_APP_BACKEND_URL, process.env.VUE_APP_MIRROR_BACKEND_URL];
+var apiTypeIndex = 0;
+var checkApiGetBackendUrl = function(index) {
+	$.ajax({
+		url: baseApiList[index] + '/mobile/companyInfoApi/link',
+		type: 'GET',
+		async: false, //同步
+		data: {},
+		timeout: 3000,
+		success: function(data, textStatus, jqXHR) {
+
+		},
+		error: function(xhr, textStatus) {
+			console.log(index)
+			console.log(baseApiList[index] + '异常');
+			if (document.title.indexOf('-') > -1) {
+				document.title = document.title.substring(0, document.title.indexOf('-')) + '-线路' + (index + 1);
+			} else {
+				document.title = document.title + '-线路' + (index + 1);
+			}
+
+			if (index == baseApiList.length - 1) {
+				alert('接口无法访问,请联系开发者处理');
+			} else {
+				index++;
+				apiTypeIndex = index;
+				checkApiGetBackendUrl(index);
+			}
+		},
+		complete: function() {
+			console.log('接口通畅校验完成')
+		}
+	})
+}
+checkApiGetBackendUrl(apiTypeIndex);
+
+//只有当刷新的时候才会访问一次校验
+store.commit('SET_API_TYPE', baseApiList[apiTypeIndex]);
+var BACKEND_URL = baseApiList[apiTypeIndex];
+
 // 创建axios实例
 const service = axios.create({
-	baseURL: process.env.VUE_APP_BACKEND_URL,
+	baseURL: BACKEND_URL,
 	timeout: 20000,
 	// 请求头信息
 	headers: {

+ 43 - 1
src/utils/request.js

@@ -3,6 +3,7 @@ import store from '$project/store';
 import router from '@/router';
 import Vue from 'vue';
 import Qs from 'qs';
+import $ from 'jquery';
 
 import {
 	getToken,
@@ -11,9 +12,50 @@ import {
 
 axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
 
+//检测是否可以访问通
+var baseApiList = [process.env.VUE_APP_BACKEND_URL, process.env.VUE_APP_MIRROR_BACKEND_URL];
+var apiTypeIndex = 0;
+var checkApiGetBackendUrl = function(index) {
+	$.ajax({
+		url: baseApiList[index] + '/mobile/companyInfoApi/link',
+		type: 'GET',
+		async: false, //同步
+		data: {},
+		timeout: 3000,
+		success: function(data, textStatus, jqXHR) {
+
+		},
+		error: function(xhr, textStatus) {
+			console.log(index)
+			console.log(baseApiList[index] + '异常');
+			if (document.title.indexOf('-') > -1) {
+				document.title = document.title.substring(0, document.title.indexOf('-')) + '-线路' + (index + 1);
+			} else {
+				document.title = document.title + '-线路' + (index + 1);
+			}
+
+			if (index == baseApiList.length - 1) {
+				alert('接口无法访问,请联系开发者处理');
+			} else {
+				index++;
+				apiTypeIndex = index;
+				checkApiGetBackendUrl(index);
+			}
+		},
+		complete: function() {
+			console.log('接口通畅校验完成')
+		}
+	})
+}
+checkApiGetBackendUrl(apiTypeIndex);
+
+//只有当刷新的时候才会访问一次校验
+store.commit('SET_API_TYPE', baseApiList[apiTypeIndex]);
+var BACKEND_URL = baseApiList[apiTypeIndex];
+
 // 创建axios实例
 const service = axios.create({
-	baseURL: process.env.VUE_APP_BACKEND_URL,
+	baseURL: BACKEND_URL,
 	timeout: 20000,
 	// 请求头信息
 	headers: {