|
@@ -0,0 +1,135 @@
|
|
|
+import axios from 'axios';
|
|
|
+import store from '$project/store';
|
|
|
+import router from '@/router';
|
|
|
+import Vue from 'vue';
|
|
|
+import $ from 'jquery';
|
|
|
+
|
|
|
+import {
|
|
|
+ getToken
|
|
|
+} from '$project/utils/storage'
|
|
|
+
|
|
|
+axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
|
|
+
|
|
|
+//检测是否可以访问通
|
|
|
+var baseApiList = [process.env.VUE_APP_BACKEND_URL51, 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: BACKEND_URL,
|
|
|
+ timeout: 20000,
|
|
|
+ // 请求头信息
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
+ 'X-Requested-With': 'XMLHttpRequest',
|
|
|
+ },
|
|
|
+});
|
|
|
+
|
|
|
+//request拦截器
|
|
|
+service.interceptors.request.use(config => {
|
|
|
+
|
|
|
+ // if (process.env.NODE_ENV == 'devlopment') {
|
|
|
+ // var Authorization = process.env.VUE_APP_AUTH;
|
|
|
+ // } else {
|
|
|
+ var Authorization = getToken() ? getToken() : '';
|
|
|
+
|
|
|
+ //var Authorization = 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMDAxNSIsImV4cCI6MTYwMTM3NzQyNH0.lmtjaZH3VAreGTS9zPosBHPRQ3_DC2esuY1AV1pI1rM';
|
|
|
+ // }
|
|
|
+
|
|
|
+ // 获取token
|
|
|
+ //const token = store.state.token;
|
|
|
+ //if (token) {
|
|
|
+ config.headers['Authorization'] = Authorization;
|
|
|
+ //}
|
|
|
+ return config;
|
|
|
+}, error => {
|
|
|
+ //对请求错误处理
|
|
|
+ return Promise.reject(error);
|
|
|
+});
|
|
|
+
|
|
|
+// response拦截器
|
|
|
+service.interceptors.response.use(
|
|
|
+ response => {
|
|
|
+ if (response.data.code === 200) {
|
|
|
+ if (response.data.result) {
|
|
|
+ return Promise.resolve(response.data.data);
|
|
|
+ } else {
|
|
|
+ return Promise.reject(response.data.message);
|
|
|
+ }
|
|
|
+ } else if (response.data.code === 415) {
|
|
|
+ //这里不执行,token过期
|
|
|
+ } else {
|
|
|
+ //无code的情况
|
|
|
+ if (response.data.result) {
|
|
|
+ return Promise.resolve(response.data.data);
|
|
|
+ } else {
|
|
|
+ return Promise.reject(response.data.message);
|
|
|
+ }
|
|
|
+
|
|
|
+ // todo 根据返回值判定去向,比如token过期,未登录等等
|
|
|
+
|
|
|
+ // 403 接口需要登录
|
|
|
+ /* if (response.data.code === 403) {
|
|
|
+ store.dispatch('clearToken');
|
|
|
+ router.replace({
|
|
|
+ name: 'Login',
|
|
|
+ query: {
|
|
|
+ redirect: router.currentRoute.fullPath
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } */
|
|
|
+
|
|
|
+ return Promise.reject(response.data.message);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ error => {
|
|
|
+ // alert('网络超时!请重新加载!');
|
|
|
+ //console.error(error);
|
|
|
+ // 判断请求异常信息中是否含有超时timeout字符串
|
|
|
+ if (error.message.includes('timeout')) {
|
|
|
+ return Promise.reject('请求超时,请稍后再试');
|
|
|
+ } else if (error.message.includes('NetworkError')) {
|
|
|
+ return Promise.reject('网络错误,请稍后再试');
|
|
|
+ }
|
|
|
+ return Promise.reject(error);
|
|
|
+ }
|
|
|
+);
|
|
|
+
|
|
|
+export default service;
|