http.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import axios from 'axios';
  2. // import Cookies from "js-cookie";
  3. import {
  4. Message
  5. } from 'element-ui';
  6. axios.defaults.timeout = 5000;
  7. axios.defaults.baseURL = 'http://manage.ai160.com/';
  8. /***
  9. *
  10. * 复制于 https://www.cnblogs.com/ldlx-mars/p/7908950.html
  11. *
  12. */
  13. //http request 拦截器
  14. axios.interceptors.request.use(
  15. config => {
  16. // const token = Cookies('token'); //注意使用的时候需要引入cookie方法,推荐js-cookie
  17. config.data = JSON.stringify(config.data);
  18. console.log(sessionStorage.getItem('uid'))
  19. const uid = sessionStorage.getItem('uid') ? sessionStorage.getItem('uid') : '';
  20. config.headers = {
  21. 'Content-Type': 'application/json',
  22. 'uid': uid,
  23. }
  24. // if(token){
  25. // config.params = {'X-Token':token}
  26. // }
  27. return config;
  28. },
  29. error => {
  30. return Promise.reject(err);
  31. }
  32. );
  33. //http response 拦截器 暂时不用
  34. axios.interceptors.response.use(
  35. response => {
  36. console.log('response',response)
  37. if (response.data.code == 402) {
  38. // router.push({
  39. // path: "/login",
  40. // // querry: {
  41. // // redirect: Router.currentRoute.fullPath
  42. // // } //从哪个页面跳转
  43. // })
  44. Message.warning({
  45. message: '请登录',
  46. type: 'warning'
  47. });
  48. window.location.href = '/manageWeb/'
  49. }
  50. return response;
  51. },
  52. error => {
  53. return Promise.reject(error)
  54. }
  55. )
  56. /**
  57. * 封装get方法
  58. * @param url
  59. * @param data
  60. * @returns {Promise}
  61. */
  62. export function fetch(url, params = {}) {
  63. return new Promise((resolve, reject) => {
  64. axios.get(url, {
  65. params: params
  66. })
  67. .then(response => {
  68. resolve(response.data);
  69. })
  70. .catch(err => {
  71. reject(err)
  72. })
  73. })
  74. }
  75. /**
  76. * 封装post请求
  77. * @param url
  78. * @param data
  79. * @returns {Promise}
  80. */
  81. export function post(url, data = {}) {
  82. console.log(data)
  83. return new Promise((resolve, reject) => {
  84. axios.post(url, data)
  85. .then(response => {
  86. resolve(response.data);
  87. }, err => {
  88. reject(err)
  89. })
  90. })
  91. }
  92. /**
  93. * 封装patch请求
  94. * @param url
  95. * @param data
  96. * @returns {Promise}
  97. */
  98. export function patch(url, data = {}) {
  99. return new Promise((resolve, reject) => {
  100. axios.patch(url, data)
  101. .then(response => {
  102. resolve(response.data);
  103. }, err => {
  104. reject(err)
  105. })
  106. })
  107. }
  108. /**
  109. * 封装put请求
  110. * @param url
  111. * @param data
  112. * @returns {Promise}
  113. */
  114. export function put(url, data = {}) {
  115. return new Promise((resolve, reject) => {
  116. axios.put(url, data)
  117. .then(response => {
  118. resolve(response.data);
  119. }, err => {
  120. reject(err)
  121. })
  122. })
  123. }