Request.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import api from "../config/api.js";
  2. import Http from "./Http.js";
  3. import After from "./interceptors/After.js";
  4. import Before from "./interceptors/Before.js";
  5. import Complete from "./interceptors/Complete.js";
  6. import Success from "./interceptors/Success.js";
  7. import Fail from "./interceptors/Fail.js";
  8. import store from "../store";
  9. let Request = {
  10. //全局 监听器 和 拦截器
  11. interceptors:function(){
  12. return {
  13. after : (new After()).create,
  14. before : (new Before()).create,
  15. success : (new Success()).create,
  16. fail : (new Fail()).create,
  17. complete : (new Complete()).create
  18. };
  19. },
  20. /**
  21. * 获取API接口
  22. * @param {Object} name
  23. */
  24. getApi:function(name){
  25. return store.state.http + api[name];
  26. },
  27. /**
  28. * get 提交数据
  29. * @param {Object} name api类目
  30. * @param {Object} post 提交参数
  31. * @param {Object} data 其他参数 {
  32. type : "json",//默认 返回用json进行解析
  33. header : {key:value},//使用字典模式 传递http头数据
  34. interceptors : {
  35. before:()=>{},//请求之前[返回false表示拦击 | 返回true不拦截]
  36. after:()=>{},//请求之后 [返回false表示拦击 | 返回true不拦截]
  37. success:()=>{},//请求成功 [只能监听]
  38. fail:()=>{},//请求失败 [只能监听]
  39. complete:()=>{}//无论失败和成功 [只能监听]
  40. },//拦截器 | 监听器
  41. //执行顺序
  42. before -> after -> success | fail | complete
  43. }
  44. *
  45. */
  46. get: async function(name,post,data = {}) {
  47. return this.build(name,post,"get",data);
  48. },
  49. /**
  50. * post 提交数据
  51. * @param {Object} name api类目
  52. * @param {Object} post 提交参数
  53. * @param {Object} data 其他参数 参考上面
  54. *
  55. */
  56. post: async function(name,post,data = {}) {
  57. return this.build(name,post,"post",data);
  58. },
  59. /**
  60. * 上传文件
  61. * @param {Object} name api类目
  62. * @param {Object} post 提交参数
  63. * @param {Object} post 文件数据
  64. * @param {Object} post 提交input名字
  65. * @param {Object} data 其他参数 参考上面
  66. *
  67. */
  68. uploadFile: async function(name,img,inputName,post,data = {}) {
  69. return this.build(name,post,"file",data,img,inputName);
  70. },
  71. /**
  72. * 提交数据
  73. */
  74. build: async function(name,post,method,data = {},img = null,inputName=null){
  75. return await new Promise((resolve, reject) => {
  76. let http = new Http();
  77. //类型
  78. if(data.type != null) {
  79. http.setType(data.type);
  80. }
  81. //头部文件
  82. if(data.header != null){
  83. for(var i in data.header) {
  84. http.setHeader(i,data.header[i]);
  85. }
  86. }
  87. //全局监听口
  88. let interceptors = this.interceptors();
  89. for(var i in interceptors) {
  90. http.interceptors.hasOwnProperty(i) ? http.interceptors[i](interceptors[i]) : '';
  91. }
  92. //监听口
  93. if(data.interceptors != null) {
  94. for(var i in data.interceptors) {
  95. http.interceptors.hasOwnProperty(i) ? http.interceptors[i](data.interceptors[i]) : '';
  96. }
  97. }
  98. // [成功|失败]返回口
  99. http.interceptors.success((res)=>{
  100. resolve(res);
  101. });
  102. //失败返回
  103. http.interceptors.fail((res)=>{
  104. console.log(res);
  105. reject(-1,res);
  106. });
  107. //监听拦截器,告诉调用口(catch)是否被拦截
  108. http.interceptors._intercept(() =>{
  109. console.log("intercept");
  110. reject(-2);
  111. });
  112. var url = "";
  113. if(name.indexOf('http://') == 0 || name.indexOf("https://") == 0) {
  114. url = name;
  115. } else {
  116. url = store.state.http + api[name];
  117. }
  118. if(method == 'get')
  119. http.get(url,post);
  120. if(method == 'post')
  121. http.post(url,post);
  122. if(method == 'file')
  123. http.uploadFile(url,post,img,inputName);
  124. });
  125. }
  126. };
  127. export default Request;