config.js
3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import axios from 'axios'
import qs from 'qs'
import { Message } from "element-ui";
import {CONSTANT} from '../constant'
import store from '../vuex/store'
axios.interceptors.request.use(config => {
//NProgress.start()
return config
}, error => {
return Promise.reject(error)
})
axios.interceptors.response.use(response => response, error => Promise.resolve(error.response))
function checkStatus(response) {
if(response){
const { headers, status } = response;
if(headers&&headers.sessionstatus == 'timeout'){
window.location.href = '../ots/login';
return;
}
if (status === 200 || status === 304){
return response.data
}
}
return {
returnCode: -404,
message: response ? response.statusText : '',
data: response ? response.statusText : ''
}
}
function checkCode(res) {
if (('returnCode' in res && res.returnCode != 0) || res.error) {
Message({
message: res.message,
type: 'error'
});
}
return res
}
// 从mock获取基础域名配置
const MOCK_CONFIG_URL = 'https://oss-config.test.glsx.com.cn/mock/60a371eafb9c105681999ab8/microCustomizationService/e-shield-oShield-config';
let baseFlag = true;
const getBaseUrl = () => {
return new Promise((resolve, reject) => {
let BASE_URL = store.getters.baseURL;
if(!BASE_URL && baseFlag) {
baseFlag = false;
axios({
url: MOCK_CONFIG_URL,
method: 'get'
}).then((res) => {
const resData = res.data;
if(resData) {
const resBaseURL = resData.axios.baseURL;
store.commit('SET_BASE_URL', resBaseURL);
baseFlag = !resBaseURL;
resolve(resBaseURL);
} else {
baseFlag = true;
resolve(BASE_URL);
}
}).catch((err) => {
baseFlag = true;
resolve(BASE_URL)
});
} else {
resolve(BASE_URL)
}
})
};
export default {
post(url, data) {
getBaseUrl().then((baseUrl) => {
return axios({
method: 'post',
url: `${baseUrl}/ots/${url}`,
data: typeof data == 'string'?data:qs.stringify(data),
timeout: 30000,
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': typeof data == 'string'?'application/json':'application/x-www-form-urlencoded; charset=UTF-8'
}
}).then(checkStatus).then(checkCode)
}, (err) => {
})
},
get(url, params) {
getBaseUrl().then((baseUrl) => {
return axios({
method: 'get',
url: `${baseUrl}/ots/${url}`,
params,
//timeout: 300000,
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
withCredentials:true
}).then(checkStatus).then(checkCode)
}, (err) => {
})
}
}