Request.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. console.log(name,'data');
  88. //全局监听口
  89. let interceptors = this.interceptors();
  90. for(var i in interceptors) {
  91. http.interceptors.hasOwnProperty(i) ? http.interceptors[i](interceptors[i]) : '';
  92. }
  93. //监听口
  94. if(data.interceptors != null) {
  95. for(var i in data.interceptors) {
  96. http.interceptors.hasOwnProperty(i) ? http.interceptors[i](data.interceptors[i]) : '';
  97. }
  98. }
  99. // [成功|失败]返回口
  100. http.interceptors.success((res)=>{
  101. resolve(res);
  102. });
  103. //失败返回
  104. http.interceptors.fail((res)=>{
  105. console.log(res);
  106. reject(-1,res);
  107. });
  108. //监听拦截器,告诉调用口(catch)是否被拦截
  109. http.interceptors._intercept(() =>{
  110. console.log("intercept");
  111. reject(-2);
  112. });
  113. var url = "";
  114. if(name.indexOf('http://') == 0 || name.indexOf("https://") == 0) {
  115. url = name;
  116. } else {
  117. url = store.state.http + api[name];
  118. }
  119. if(method == 'get')
  120. http.get(url,post);
  121. if(method == 'post')
  122. http.post(url,post);
  123. if(method == 'file')
  124. http.uploadFile(url,post,img,inputName);
  125. });
  126. }
  127. };
  128. export default Request;