klm-axios-config.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. document.write("<script language='javascript' src='./static/js/appfunc.min.js'></script>");
  2. // 配置基地址
  3. var request = axios.create({
  4. // baseURL: 'http://192.168.9.85:22000'
  5. // 测试接口
  6. baseURL: 'http://test.ap.shuangkebang.com'
  7. // 模拟接口
  8. // baseURL: 'http://192.168.9.85:22000/mock/11'
  9. });
  10. // 数据响应防抖
  11. let requestflag = true;
  12. // 请求拦截器
  13. request.interceptors.request.use(
  14. config => {
  15. // 该处可以将config打印出来看一下,该部分将发送给后端(server端)
  16. // console.log(config);
  17. config.headers.Authorization = 'Bearer ' + PublicLib.getCookieInfo('token');;
  18. if(requestflag){
  19. // config.header = {"":""};
  20. if(config.method === 'post'){
  21. requestflag = false;
  22. }
  23. return config; // 对config处理完后返回,下一步将向后端发送请求
  24. }
  25. },
  26. error => { // 当发生错误时,执行该部分代码
  27. console.log(error); //调试用
  28. return Promise.reject(error)
  29. }
  30. );
  31. // 响应拦截器
  32. request.interceptors.response.use(
  33. response => { // 该处为后端返回整个内容
  34. // tips('登录失效');
  35. const res = response.data; // 该处可将后端数据取出,提前做一个处理
  36. requestflag = true;
  37. return res;
  38. },
  39. error => {
  40. console.log(error.response.status);
  41. if(error.response.status === 401) {
  42. PublicLib.GoBack({Level:0});
  43. };
  44. return Promise.reject(error);
  45. }
  46. );
  47. // 测试接口
  48. // const testInterface = (url) => {
  49. // return request.get(url);
  50. // };
  51. // GET请求
  52. const getRequest = (url,parameter) => {
  53. console.log(parameter)
  54. return request.get(url,{params:{value:encryption(parameter)}});
  55. };
  56. // POST请求
  57. const postRequest = (url,parameter) => {
  58. let param = new URLSearchParams();
  59. param.append('value', encryption(parameter));
  60. console.log(parameter);
  61. return request({
  62. url,
  63. method: 'post',
  64. headers: {
  65. // 'Access-Control-Allow-Origin':'*'
  66. 'Content-Type': 'application/x-www-form-urlencoded'
  67. },
  68. data: param,
  69. })
  70. };
  71. // let param = new URLSearchParams();
  72. // param.append('value', '{"Mobile":"' + Mobile + '","ReferenceCode":"' + ReferenceCode + '","LoginPwd":"' + LoginPwd + '","MobileCode":"' + MobileCode + '","MobileCode":"' + that.mobileCode + '"}');
  73. // param.append('token', getCookieInfo('Token'));
  74. document.write("<script language='javascript' src='./static/js/publicfn.js'></script>");