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