axios.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. import { Notify } from "vant";
  3. import Vue from 'vue';
  4. import axios from "axios";
  5. // Full config: https://github.com/axios/axios#request-config
  6. axios.defaults.baseURL = 'http://121.40.217.77:8082/';
  7. // axios.defaults.baseURL = 'https://121.40.217.77:8084/';
  8. console.log(axios.defaults.baseURL)
  9. // axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
  10. // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
  11. // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  12. let config = {
  13. // baseURL: process.env.baseURL || process.env.apiUrl || ""
  14. // timeout: 60 * 1000, // Timeout
  15. // withCredentials: true, // Check cross-site Access-Control
  16. };
  17. const _axios = axios.create(config);
  18. _axios.interceptors.request.use(
  19. function(config) {
  20. // Do something before request is sent
  21. return config;
  22. },
  23. function(error) {
  24. // Do something with request error
  25. return Promise.reject(error);
  26. }
  27. );
  28. // Add a response interceptor
  29. _axios.interceptors.response.use(
  30. function(response) {
  31. // Do something with response data
  32. if (!response.data.success) Notify({ type: 'danger', message: response.data.message })
  33. return response.data;
  34. },
  35. function(error) {
  36. // Do something with response error
  37. return Promise.reject(error);
  38. }
  39. );
  40. Plugin.install = function(Vue, options) {
  41. console.log(options)
  42. Vue.axios = _axios;
  43. window.axios = _axios;
  44. Object.defineProperties(Vue.prototype, {
  45. axios: {
  46. get() {
  47. return _axios;
  48. }
  49. },
  50. $axios: {
  51. get() {
  52. return _axios;
  53. }
  54. },
  55. });
  56. };
  57. Vue.use(Plugin)
  58. export default Plugin;