request.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import axios from "axios";
  2. import qs from "qs";
  3. import { Message, Loading } from 'element-ui'
  4. import {judgmentNetworkReturnAddress } from "@/utils/public";
  5. let loadingInstance = {};
  6. let options = {
  7. spinner:"",
  8. background: 'rgba(255, 255, 255, 0.1)'
  9. };
  10. let loadingCount = 0;
  11. /*
  12. */
  13. //判定http或者https
  14. let urlText = window.location.href.split('://')[0]+'://';
  15. // axios中请求配置有baseURL选项,表示请求URL公共部分
  16. axios.defaults.baseURL = urlText+judgmentNetworkReturnAddress();
  17. //获取后台浏览器跳转过来的地址携带的token
  18. let afterUrl = window.location.search.substring(1);
  19. let identity = afterUrl.substr(9, afterUrl.length);
  20. if(identity.length>0){
  21. //Authorization
  22. localStorage.setItem('Authorization',identity)
  23. localStorage.setItem('deptLevel','2')////这个是登陆后,刘波给你的院校的类型
  24. }
  25. //post请求头
  26. axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8";
  27. // 表示跨域请求时是否需要使用凭证 允许跨域携带cookie信息
  28. axios.defaults.withCredentials = false;
  29. // 允许跨域
  30. axios.defaults.headers.post["Access-Control-Allow-Origin-Type"] = "*";
  31. //axios.defaults.headers.common["Authorization"] = 'Bearer 1c03c353-160c-42ea-bb38-88c7a831de48';//`Bearer ${localStorage.getItem('access_token')`;
  32. axios.defaults.headers.common["Authorization"] = 'Bearer '+localStorage.getItem('Authorization')+''
  33. //设置超时
  34. axios.defaults.timeout = 15000;
  35. axios.interceptors.request.use(
  36. config => {
  37. loadingInstance = Loading.service(options)
  38. loadingCount ++;
  39. return config;
  40. },
  41. error => {
  42. loadingCount --;
  43. if(loadingCount===0){
  44. loadingInstance.close();
  45. }
  46. return Promise.reject(error);
  47. }
  48. );
  49. axios.interceptors.response.use(
  50. response => {
  51. loadingCount --;
  52. if(loadingCount===0) {
  53. loadingInstance.close();
  54. }
  55. if (response.status == 200) {
  56. if(response.data.code == 200){
  57. return Promise.resolve(response);
  58. }else{
  59. // Message({
  60. // message: response.data.msg,
  61. // type: 'error',
  62. // duration: 5 * 1000,
  63. // offset:100
  64. // })
  65. }
  66. } else {
  67. Message({
  68. message: response.data.msg,
  69. type: 'error',
  70. duration: 5 * 1000,
  71. offset:100
  72. })
  73. }
  74. },
  75. error => {
  76. loadingCount --;
  77. if(loadingCount===0){
  78. loadingInstance.close();
  79. }
  80. Message({
  81. message: error,
  82. type: 'error',
  83. duration: 5 * 1000,
  84. offset:100
  85. })
  86. }
  87. );
  88. export default {
  89. /**
  90. * @param {String} url
  91. * @param {Object} data
  92. * @returns Promise
  93. */
  94. post(url, data) {
  95. return new Promise((resolve, reject) => {
  96. axios({
  97. method: 'post',
  98. url,
  99. data: qs.stringify(data),
  100. })
  101. .then(res => {
  102. resolve(res.data)
  103. })
  104. .catch(err => {
  105. reject(err)
  106. });
  107. })
  108. },
  109. get(url, data,type) {
  110. if(type=='video'){
  111. axios.defaults.baseURL =localStorage.getItem('cameraExtranetAgent')
  112. }else{
  113. axios.defaults.baseURL = urlText+judgmentNetworkReturnAddress()
  114. }
  115. return new Promise((resolve, reject) => {
  116. axios({
  117. method: 'get',
  118. url,
  119. params: data,
  120. })
  121. .then(res => {
  122. resolve(res.data)
  123. })
  124. .catch(err => {
  125. reject(err)
  126. })
  127. })
  128. },
  129. baseUrl: urlText+judgmentNetworkReturnAddress()
  130. };