axios.api.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Created by Administrator on 2017/9/22.
  3. */
  4. // 引用axios
  5. var axios = require('axios');
  6. var qs = require('qs');
  7. axios.interceptors.request.use((config)=>{
  8. if(config && config.method == "post" && config.url.indexOf("filemanager")==-1)
  9. {
  10. if(config.data) {
  11. config.data = filter_param(config.data);
  12. }
  13. config.data = qs.stringify(config.data);
  14. config.headers["Content-Type"]="application/x-www-form-urlencoded";
  15. }
  16. else if(config && config.method == "get" && config.url.indexOf("filemanager")==-1)
  17. {
  18. if(config.data) {
  19. config.data = filter_param(config.data);
  20. }
  21. }
  22. return config;
  23. })
  24. // 引用superagent
  25. //自定义判断元素类型
  26. function toType(obj)
  27. {
  28. return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
  29. }
  30. // 参数过滤函数
  31. function filter_param(o)
  32. {
  33. for(var key in o)
  34. {
  35. if(o[key]==null)
  36. delete o[key];
  37. if(toType(o[key])=="string")
  38. {
  39. o[key] = o[key].trim();
  40. if(o[key].length==0)
  41. {
  42. delete o[key];
  43. }
  44. }
  45. }
  46. return o;
  47. }
  48. function initData(params)
  49. {
  50. var formMap = new URLSearchParams();
  51. for(var key in params){
  52. formMap.append(key, params[key]);
  53. }
  54. return formMap;
  55. }
  56. /*
  57. 接口处理函数
  58. */
  59. function _api_req(rootUrl,method,url,params,headers,callback1,callback2,errCallback)
  60. {
  61. if(process.env.NODE_ENV === 'production')
  62. {
  63. rootUrl = "/";
  64. }
  65. return axios({
  66. method:method,
  67. url:url,
  68. data:method == "POST" || method == "PUT" ? params:null,
  69. params:method === "GET" || method ==="DELETE" ? params:null,
  70. baseURL: rootUrl,
  71. withCredentials: false,
  72. headers:headers,
  73. onUploadProgress:function(progressEvent){ //原生获取上传进度的事件
  74. if(progressEvent.lengthComputable){
  75. //属性lengthComputable主要表明总共需要完成的工作量和已经完成的工作是否可以被测量
  76. //如果lengthComputable为false,就获取不到progressEvent.total和progressEvent.loaded
  77. if(callback1)
  78. {
  79. callback1(progressEvent);
  80. }
  81. }
  82. },
  83. }).then(res =>{
  84. callback2(res.data);
  85. },error =>{
  86. errCallback(error);
  87. })
  88. }
  89. export default {
  90. get:function(root,url,params,headers,callback1,callback2,errCallback){
  91. root = root!=null ? root:"";
  92. return _api_req(root,"GET","/"+url,params,headers,callback1,callback2,errCallback);
  93. },
  94. put:function(root,url,params,headers,callback1,callback2,errCallback)
  95. {
  96. root = root!=null ? root:"";
  97. return _api_req(root,"PUT","/"+url,params,headers,callback1,callback2,errCallback);
  98. },
  99. post:function(root,url,params,headers,callback1,callback2,errCallback)
  100. {
  101. root = root!=null ? root:"";
  102. return _api_req(root,"POST","/"+url,params,headers,callback1,callback2,errCallback);
  103. },
  104. delete:function(root,url,params,headers,callback1,callback2,errCallback)
  105. {
  106. root = root!=null ? root:"";
  107. return _api_req(root,"DELETE","/"+url,params,headers,callback1,callback2,errCallback);
  108. }
  109. }