Http.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. class Http {
  2. //头部文件
  3. header = {}
  4. //远程地址
  5. baseUrl = ""
  6. params = {}
  7. //函数存放口
  8. baseCall = {
  9. before: [], //请求之前
  10. after: [], //请求之后
  11. success: [], //请求成功
  12. fail: [], //请求失败
  13. complete: [] ,//请求完毕
  14. _intercept:null,//拦截器专用
  15. }
  16. //传递格式
  17. type = "json"
  18. //监听器
  19. interceptors = {
  20. //请求之前
  21. before: (call) => {
  22. this.baseCall.before.push(call);
  23. },
  24. //请求之后
  25. after: (call) => {
  26. this.baseCall.after.push(call);
  27. },
  28. //请求成功
  29. success: (call) => {
  30. this.baseCall.success.push(call);
  31. },
  32. //请求失败
  33. fail: (call) => {
  34. this.baseCall.fail.push(call);
  35. },
  36. //请求完毕
  37. complete: (call) => {
  38. this.baseCall.complete.push(call);
  39. },
  40. //被拦截
  41. _intercept:(call)=>{
  42. this.baseCall._intercept = call;
  43. }
  44. };
  45. constructor() {
  46. }
  47. setParams(key,value) {
  48. this.params[key] = value;
  49. }
  50. /**
  51. * get提交
  52. * @param {Object} url
  53. * @param {Object} post
  54. */
  55. get(url, post) {
  56. this.baseUrl = url;
  57. this.params = post || {_t:new Date().getTime()};
  58. this.buld('get');
  59. }
  60. /**
  61. * post提交
  62. * @param {Object} url
  63. * @param {Object} post
  64. */
  65. post(url, post) {
  66. this.baseUrl = url;
  67. this.setHeader('content-type', "application/x-www-form-urlencoded");
  68. this.params = post;
  69. this.buld('post');
  70. }
  71. /**
  72. * 上传文件
  73. * @param {Object} url
  74. * @param {Object} post
  75. */
  76. uploadFile(url,post,img,name) {
  77. this.baseUrl = url;
  78. this.params = post;
  79. this.upFile(img,name);
  80. }
  81. /**
  82. * 提交数据
  83. */
  84. buld(method) {
  85. //提交之前
  86. for (var i in this.baseCall.before) {
  87. var b = this.baseCall.before[i].call(this);
  88. //进行拦截
  89. if(b != null && b === false) {
  90. this.intercept();
  91. return;
  92. }
  93. }
  94. // console.log(this.baseUrl,'this.baseUrl')
  95. // console.log(this.header,'this.header')
  96. uni.request({
  97. url: this.baseUrl,
  98. method: method,
  99. data: this.params,
  100. header: this.header,
  101. dataType: this.type,
  102. success: res => {
  103. //提交之后
  104. for (var i in this.baseCall.after) {
  105. var b = this.baseCall.after[i].call(this,res);
  106. //进行拦截
  107. if(b != null && b === false) {
  108. this.intercept();
  109. return;
  110. }
  111. }
  112. if (res.statusCode == 200) {
  113. for (var i in this.baseCall.success) {
  114. this.baseCall.success[i].call(this,res.data);
  115. }
  116. } else {
  117. for (var i in this.baseCall.fail) {
  118. this.baseCall.fail[i].call(this,res.data);
  119. }
  120. }
  121. },
  122. fail: (res) => {
  123. /**
  124. uni.showModal({
  125. title:"错误提示",
  126. content:JSON.stringify(res)
  127. });
  128. console.log(res); **/
  129. for (var i in this.baseCall.fail) {
  130. this.baseCall.fail[i].call(this,res.errMsg);
  131. }
  132. },
  133. complete: (res) => {
  134. if(res.errMsg == 'request:ok') {
  135. for (var i in this.baseCall.complete) {
  136. this.baseCall.complete[i].call(this,res);
  137. }
  138. } else {
  139. console.log(res);
  140. for (var i in this.baseCall.fail) {
  141. this.baseCall.fail[i].call(this,res.errMsg);
  142. }
  143. }
  144. }
  145. });
  146. }
  147. upFile(img,name) {
  148. var that = this;
  149. //提交之前
  150. for (var i in this.baseCall.before) {
  151. var b = this.baseCall.before[i].call(this);
  152. //进行拦截
  153. if(b != null && b === false) {
  154. this.intercept();
  155. return;
  156. }
  157. }
  158. uni.uploadFile({
  159. url : this.baseUrl,
  160. filePath : img,
  161. name : name,
  162. formData: this.params,
  163. header:this.header,
  164. success : (res)=>{
  165. try{
  166. res = JSON.parse(res.data);
  167. for (var i in this.baseCall.success) {
  168. this.baseCall.success[i].call(this,res);
  169. }
  170. }catch(e) {
  171. for (var i in this.baseCall.fail) {
  172. this.baseCall.fail[i].call(this,e);
  173. }
  174. }
  175. }
  176. });
  177. }
  178. /**
  179. * 提交头部数据
  180. * @param {Object} key
  181. * @param {Object} value
  182. */
  183. setHeader(key, value) {
  184. this.header[key] = value;
  185. return this;
  186. }
  187. /**
  188. * 设置类型
  189. * @param {Object} type
  190. */
  191. setType(type) {
  192. this.type = type;
  193. }
  194. /**
  195. * 告诉监听者,被拦截了,可以进行反应操作
  196. */
  197. intercept() {
  198. if(this.baseCall._intercept != null) {
  199. this.baseCall._intercept.call(this);
  200. }
  201. }
  202. };
  203. export default Http;