Request.js 3.9 KB

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