Request.js 4.1 KB

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