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

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. import Vue from "vue";
  2. import axios from 'axios';
  3. import router from '@/router'
  4. const qs = require('qs');
  5. var apiServer = (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl);
  6. const resolveAcceptLanguage = () => {
  7. const locale = localStorage.getItem('locale') || 'cn';
  8. return locale === 'en' ? 'en-US' : 'zh-CN';
  9. };
  10. const resolveAdvancedSearchHeaders = () => {
  11. const route = router && router.currentRoute ? router.currentRoute : {}
  12. let routePath = String(route.path || '').trim()
  13. if (!routePath) {
  14. return {}
  15. }
  16. if (routePath.charAt(0) !== '/') {
  17. routePath = '/' + routePath
  18. }
  19. const key = 'advanced_search_context_' + routePath
  20. let matchedKey = key
  21. let raw = sessionStorage.getItem(key)
  22. if (!raw && routePath.indexOf('/modules/') === 0) {
  23. matchedKey = 'advanced_search_context_' + routePath.substring('/modules'.length)
  24. raw = sessionStorage.getItem(matchedKey)
  25. }
  26. if (!raw && routePath.indexOf('/modules/') !== 0) {
  27. matchedKey = 'advanced_search_context_' + '/modules' + routePath
  28. raw = sessionStorage.getItem(matchedKey)
  29. }
  30. if (!raw) {
  31. return {}
  32. }
  33. try {
  34. const context = JSON.parse(raw)
  35. if (!context || !context.menuUrl || !Array.isArray(context.conditions) || context.conditions.length <= 0) {
  36. sessionStorage.removeItem(matchedKey)
  37. return {}
  38. }
  39. sessionStorage.removeItem(matchedKey)
  40. return {
  41. 'X-Advanced-Search-Menu': String(context.menuUrl),
  42. 'X-Advanced-Search-Conditions': encodeURIComponent(JSON.stringify(context.conditions))
  43. }
  44. } catch (e) {
  45. sessionStorage.removeItem(matchedKey)
  46. return {}
  47. }
  48. }
  49. var http = axios.create({
  50. timeout: 10000,
  51. headers: {},
  52. transformRequest: data => {
  53. if (typeof data === 'object') {
  54. return qs.stringify(data, { arrayFormat: 'brackets' });
  55. }
  56. return data;
  57. }
  58. });
  59. export default {
  60. axios: http,
  61. $post(url, data, callback, error) {
  62. // 添加 headers 配置项
  63. const config = {
  64. headers: {
  65. 'Content-Type': 'application/json;charset=UTF-8',
  66. 'token': Vue.cookie.get('token'),
  67. 'Accept-Language': resolveAcceptLanguage()
  68. }
  69. };
  70. const advancedHeaders = resolveAdvancedSearchHeaders()
  71. Object.keys(advancedHeaders).forEach(key => {
  72. config.headers[key] = advancedHeaders[key]
  73. })
  74. let p = http.post(apiServer +url, JSON.stringify(data), config);
  75. if (callback) {
  76. p.then(res => {
  77. callback(res);
  78. })
  79. }
  80. if (error) {
  81. p.catch(err => {
  82. error(err);
  83. });
  84. }
  85. return p;
  86. },
  87. file:(url, data, succ, err )=>{
  88. let requestUrl = apiServer + url;
  89. var xhr = new XMLHttpRequest();
  90. xhr.open('POST', requestUrl, true);
  91. xhr.responseType = "blob";
  92. xhr.withCredentials = true;
  93. xhr.setRequestHeader("X-API", "true");
  94. xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  95. xhr.setRequestHeader("token", Vue.cookie.get('token') );
  96. xhr.setRequestHeader("Accept-Language", resolveAcceptLanguage());
  97. const advancedHeaders = resolveAdvancedSearchHeaders()
  98. Object.keys(advancedHeaders).forEach(key => {
  99. xhr.setRequestHeader(key, advancedHeaders[key]);
  100. })
  101. xhr.onreadystatechange = function (e) {
  102. if (xhr.readyState === 4) {
  103. if (xhr.status === 200) {
  104. succ(xhr.response)
  105. } else {
  106. var fr = new FileReader();
  107. fr.onload = function(e) {
  108. var text = fr.result;
  109. console.log(text);
  110. err ? err(text) : alert("请求发生错误:" + text)
  111. };
  112. fr.readAsText(xhr.response);
  113. }
  114. }
  115. };
  116. xhr.send(data);
  117. },
  118. apiServer: apiServer
  119. };