You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
3.7 KiB
122 lines
3.7 KiB
import Vue from "vue";
|
|
import axios from 'axios';
|
|
import router from '@/router'
|
|
const qs = require('qs');
|
|
var apiServer = (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl);
|
|
|
|
const resolveAcceptLanguage = () => {
|
|
const locale = localStorage.getItem('locale') || 'cn';
|
|
return locale === 'en' ? 'en-US' : 'zh-CN';
|
|
};
|
|
|
|
const resolveAdvancedSearchHeaders = () => {
|
|
const route = router && router.currentRoute ? router.currentRoute : {}
|
|
let routePath = String(route.path || '').trim()
|
|
if (!routePath) {
|
|
return {}
|
|
}
|
|
if (routePath.charAt(0) !== '/') {
|
|
routePath = '/' + routePath
|
|
}
|
|
const key = 'advanced_search_context_' + routePath
|
|
let matchedKey = key
|
|
let raw = sessionStorage.getItem(key)
|
|
if (!raw && routePath.indexOf('/modules/') === 0) {
|
|
matchedKey = 'advanced_search_context_' + routePath.substring('/modules'.length)
|
|
raw = sessionStorage.getItem(matchedKey)
|
|
}
|
|
if (!raw && routePath.indexOf('/modules/') !== 0) {
|
|
matchedKey = 'advanced_search_context_' + '/modules' + routePath
|
|
raw = sessionStorage.getItem(matchedKey)
|
|
}
|
|
if (!raw) {
|
|
return {}
|
|
}
|
|
try {
|
|
const context = JSON.parse(raw)
|
|
if (!context || !context.menuUrl || !Array.isArray(context.conditions) || context.conditions.length <= 0) {
|
|
sessionStorage.removeItem(matchedKey)
|
|
return {}
|
|
}
|
|
sessionStorage.removeItem(matchedKey)
|
|
return {
|
|
'X-Advanced-Search-Menu': String(context.menuUrl),
|
|
'X-Advanced-Search-Conditions': encodeURIComponent(JSON.stringify(context.conditions))
|
|
}
|
|
} catch (e) {
|
|
sessionStorage.removeItem(matchedKey)
|
|
return {}
|
|
}
|
|
}
|
|
|
|
var http = axios.create({
|
|
timeout: 10000,
|
|
headers: {},
|
|
transformRequest: data => {
|
|
if (typeof data === 'object') {
|
|
return qs.stringify(data, { arrayFormat: 'brackets' });
|
|
}
|
|
return data;
|
|
}
|
|
});
|
|
export default {
|
|
axios: http,
|
|
$post(url, data, callback, error) {
|
|
// 添加 headers 配置项
|
|
const config = {
|
|
headers: {
|
|
'Content-Type': 'application/json;charset=UTF-8',
|
|
'token': Vue.cookie.get('token'),
|
|
'Accept-Language': resolveAcceptLanguage()
|
|
}
|
|
};
|
|
const advancedHeaders = resolveAdvancedSearchHeaders()
|
|
Object.keys(advancedHeaders).forEach(key => {
|
|
config.headers[key] = advancedHeaders[key]
|
|
})
|
|
let p = http.post(apiServer +url, JSON.stringify(data), config);
|
|
if (callback) {
|
|
p.then(res => {
|
|
callback(res);
|
|
})
|
|
}
|
|
if (error) {
|
|
p.catch(err => {
|
|
error(err);
|
|
});
|
|
}
|
|
return p;
|
|
},
|
|
file:(url, data, succ, err )=>{
|
|
let requestUrl = apiServer + url;
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.open('POST', requestUrl, true);
|
|
xhr.responseType = "blob";
|
|
xhr.withCredentials = true;
|
|
xhr.setRequestHeader("X-API", "true");
|
|
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
|
xhr.setRequestHeader("token", Vue.cookie.get('token') );
|
|
xhr.setRequestHeader("Accept-Language", resolveAcceptLanguage());
|
|
const advancedHeaders = resolveAdvancedSearchHeaders()
|
|
Object.keys(advancedHeaders).forEach(key => {
|
|
xhr.setRequestHeader(key, advancedHeaders[key]);
|
|
})
|
|
xhr.onreadystatechange = function (e) {
|
|
if (xhr.readyState === 4) {
|
|
if (xhr.status === 200) {
|
|
succ(xhr.response)
|
|
} else {
|
|
var fr = new FileReader();
|
|
fr.onload = function(e) {
|
|
var text = fr.result;
|
|
console.log(text);
|
|
err ? err(text) : alert("请求发生错误:" + text)
|
|
};
|
|
fr.readAsText(xhr.response);
|
|
}
|
|
}
|
|
};
|
|
xhr.send(data);
|
|
},
|
|
apiServer: apiServer
|
|
};
|