index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import api from './request';
  2. import { getCookie,setCookie } from '@utils/index'
  3. import {message} from 'antd'
  4. const axios=require('axios');
  5. axios.defaults.baseURL = 'http://192.168.2.237:8871';//'http://192.168.2.237:8871/';
  6. const post=(url,data)=>{
  7. return axios({
  8. method:'post',
  9. url:url,
  10. data
  11. });
  12. };
  13. const xPost=(url,data)=>{
  14. let param = new URLSearchParams();
  15. for(let i in data){
  16. param.append(i,data[i]);
  17. }
  18. return axios({
  19. method:'post',
  20. contentType:'application/x-www-form-urlencoded',
  21. url:url,
  22. data:param
  23. });
  24. };
  25. // 导出
  26. const expJson = (url,data) =>{
  27. return axios({
  28. method:'post',
  29. url,
  30. data,
  31. contentType: "application/vnd.ms-excel;charset=UTF-8" ,
  32. responseType: 'blob' //必须添加,否则会乱码
  33. });
  34. }
  35. const get=(url,data)=>{
  36. return axios({
  37. method:'get',
  38. url:url,
  39. data
  40. });
  41. };
  42. function interceptors(){
  43. //拦截所有请求添加token
  44. axios.interceptors.request.use(function (req) {
  45. const tokenStr = localStorage.getItem('token');
  46. const sysId = getCookie('systemId');
  47. const hisId = getCookie('hospitalId');
  48. if (tokenStr) {
  49. (req.headers.Authorization = `Bearer ${tokenStr}`);
  50. sysId&&(req.headers.softwareId=sysId);
  51. hisId&&(req.headers.hospitalId=hisId);
  52. return req;
  53. } else {
  54. return req;
  55. }
  56. }, function (error) {
  57. return Promise.reject(error);
  58. });
  59. //拦截响应
  60. axios.interceptors.response.use(function (res) {
  61. const code = res.data.code;
  62. if (code===401) { //401/403返回登录
  63. localStorage.removeItem("token");
  64. localStorage.removeItem("systemId");
  65. localStorage.removeItem("hospitalId");
  66. localStorage.removeItem("software")
  67. setCookie();
  68. //localStorage.removeItem("hospitalId");
  69. message.error("登录信息失效,请重新登录");
  70. window.location = '/';
  71. }else{
  72. return res;
  73. }
  74. }, function (error) {
  75. return Promise.reject(error);
  76. });
  77. }
  78. const obj = {
  79. post,
  80. xPost,
  81. get,
  82. api,
  83. interceptors
  84. };
  85. export default obj;