http.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import axios from 'axios';
  2. import router from '../router';
  3. import qs from 'qs';
  4. import CryptoJS from 'crypto-js';
  5. /* eslint-disable */
  6. let baseUrl = process.env.VUE_APP_BASE_URL;
  7. const axiosInstance = axios.create({
  8. baseURL: baseUrl
  9. });
  10. function decrypt(content) {
  11. const key = CryptoJS.enc.Hex.parse('2181E9E80460B852859EE455AC5203D9');
  12. const cipherText = CryptoJS.enc.Hex.parse(content);
  13. var decrypted = CryptoJS.AES.decrypt(
  14. {
  15. ciphertext: cipherText
  16. },
  17. key,
  18. {
  19. mode: CryptoJS.mode.ECB,
  20. padding: CryptoJS.pad.Pkcs7
  21. }
  22. );
  23. return decrypted.toString(CryptoJS.enc.Utf8);
  24. }
  25. function encrypt(content) {
  26. const key = CryptoJS.enc.Hex.parse('2181E9E80460B852859EE455AC5203D9');
  27. var encrypted = CryptoJS.AES.encrypt(content, key, {
  28. mode: CryptoJS.mode.ECB,
  29. padding: CryptoJS.pad.Pkcs7
  30. });
  31. return encrypted.ciphertext.toString(CryptoJS.enc.Hex);
  32. }
  33. axiosInstance.interceptors.request.use(
  34. function(config) {
  35. config.headers = config.headers || {};
  36. let token = localStorage.getItem('token');
  37. if (token) {
  38. config.headers['Authorization'] = 'Bearer ' + token;
  39. }
  40. return config;
  41. },
  42. function(error) {
  43. return Promise.reject(error);
  44. }
  45. );
  46. axiosInstance.interceptors.response.use(
  47. function(response) {
  48. if (response.headers['content-encrypted'] === 'true') {
  49. let decrypted = decrypt(response.data);
  50. if (response.headers['content-type'].includes('application/json')) {
  51. response.data = JSON.parse(decrypted);
  52. } else {
  53. response.data = decrypted;
  54. }
  55. }
  56. return response;
  57. },
  58. function(error) {
  59. console.log(error);
  60. if (401 === error.response.status) {
  61. if (router.currentRoute.name !== 'login') {
  62. // router.replace({
  63. // name: 'login',
  64. // params: {
  65. // from: router.currentRoute.name,
  66. // },
  67. // });
  68. } else {
  69. }
  70. }
  71. return Promise.reject(error.response.data);
  72. }
  73. );
  74. export default {
  75. baseUrl: baseUrl,
  76. axios: axiosInstance,
  77. install(_Vue, options) {
  78. _Vue.prototype.$baseUrl = baseUrl;
  79. _Vue.prototype.$axios = axiosInstance;
  80. _Vue.prototype.$http = {
  81. get(url, params) {
  82. params = params || {};
  83. return new Promise((resolve, reject) => {
  84. axiosInstance
  85. .get(
  86. url,
  87. {
  88. params: params
  89. },
  90. { withCredentials: true }
  91. )
  92. .then(res => {
  93. resolve(res.data);
  94. })
  95. .catch(e => {
  96. reject(e);
  97. });
  98. });
  99. },
  100. post(url, body, options) {
  101. options = options || {};
  102. options.headers = options.headers || {};
  103. body = body || {};
  104. if (!(body instanceof FormData)) {
  105. if (options.body === 'json') {
  106. body = encrypt(JSON.stringify(body));
  107. options.headers['content-type'] = 'application/json';
  108. } else {
  109. body = qs.stringify(body);
  110. }
  111. }
  112. return new Promise((resolve, reject) => {
  113. axiosInstance
  114. .post(url, body, { withCredentials: true, headers: options.headers })
  115. .then(res => {
  116. resolve(res.data);
  117. })
  118. .catch(e => {
  119. reject(e);
  120. });
  121. });
  122. }
  123. };
  124. }
  125. };