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.

65 lines
1.8 KiB

11 months ago
11 months ago
11 months ago
11 months ago
  1. import Vue from "vue";
  2. import axios from 'axios';
  3. const qs = require('qs');
  4. var apiServer = (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl);
  5. var http = axios.create({
  6. timeout: 10000,
  7. headers: {},
  8. transformRequest: data => {
  9. if (typeof data === 'object') {
  10. return qs.stringify(data, { arrayFormat: 'brackets' });
  11. }
  12. return data;
  13. }
  14. });
  15. export default {
  16. axios: http,
  17. $post(url, data, callback, error) {
  18. // 添加 headers 配置项
  19. const config = {
  20. headers: {
  21. 'Content-Type': 'application/json;charset=UTF-8',
  22. 'token': Vue.cookie.get('token')
  23. }
  24. };
  25. let p = http.post(apiServer +url, JSON.stringify(data), config);
  26. if (callback) {
  27. p.then(res => {
  28. callback(res);
  29. })
  30. }
  31. if (error) {
  32. p.catch(err => {
  33. error(err);
  34. });
  35. }
  36. return p;
  37. },
  38. file:(url, data, succ, err )=>{
  39. let requestUrl = apiServer + url;
  40. var xhr = new XMLHttpRequest();
  41. xhr.open('POST', requestUrl, true);
  42. xhr.responseType = "blob";
  43. xhr.withCredentials = true;
  44. xhr.setRequestHeader("X-API", "true");
  45. xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  46. xhr.setRequestHeader("token", Vue.cookie.get('token') );
  47. xhr.onreadystatechange = function (e) {
  48. if (xhr.readyState === 4) {
  49. if (xhr.status === 200) {
  50. succ(xhr.response)
  51. } else {
  52. var fr = new FileReader();
  53. fr.onload = function(e) {
  54. var text = fr.result;
  55. console.log(text);
  56. err ? err(text) : alert("请求发生错误:" + text)
  57. };
  58. fr.readAsText(xhr.response);
  59. }
  60. }
  61. };
  62. xhr.send(data);
  63. },
  64. apiServer: apiServer
  65. };