Http.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. uni.request({
  95. url: this.baseUrl,
  96. method: method,
  97. data: this.params,
  98. header: this.header,
  99. dataType: this.type,
  100. success: res => {
  101. //提交之后
  102. for (var i in this.baseCall.after) {
  103. var b = this.baseCall.after[i].call(this,res);
  104. //进行拦截
  105. if(b != null && b === false) {
  106. this.intercept();
  107. return;
  108. }
  109. }
  110. if (res.statusCode == 200) {
  111. for (var i in this.baseCall.success) {
  112. this.baseCall.success[i].call(this,res.data);
  113. }
  114. } else {
  115. for (var i in this.baseCall.fail) {
  116. this.baseCall.fail[i].call(this,res.data);
  117. }
  118. }
  119. },
  120. fail: (res) => {
  121. /**
  122. uni.showModal({
  123. title:"错误提示",
  124. content:JSON.stringify(res)
  125. });
  126. console.log(res); **/
  127. for (var i in this.baseCall.fail) {
  128. this.baseCall.fail[i].call(this,res.errMsg);
  129. }
  130. },
  131. complete: (res) => {
  132. if(res.errMsg == 'request:ok') {
  133. for (var i in this.baseCall.complete) {
  134. this.baseCall.complete[i].call(this,res);
  135. }
  136. } else {
  137. console.log(res);
  138. for (var i in this.baseCall.fail) {
  139. this.baseCall.fail[i].call(this,res.errMsg);
  140. }
  141. }
  142. }
  143. });
  144. }
  145. upFile(img,name) {
  146. var that = this;
  147. //提交之前
  148. for (var i in this.baseCall.before) {
  149. var b = this.baseCall.before[i].call(this);
  150. //进行拦截
  151. if(b != null && b === false) {
  152. this.intercept();
  153. return;
  154. }
  155. }
  156. uni.uploadFile({
  157. url : this.baseUrl,
  158. filePath : img,
  159. name : name,
  160. formData: this.params,
  161. header:this.header,
  162. success : (res)=>{
  163. try{
  164. res = JSON.parse(res.data);
  165. for (var i in this.baseCall.success) {
  166. this.baseCall.success[i].call(this,res);
  167. }
  168. }catch(e) {
  169. for (var i in this.baseCall.fail) {
  170. this.baseCall.fail[i].call(this,e);
  171. }
  172. }
  173. }
  174. });
  175. }
  176. /**
  177. * 提交头部数据
  178. * @param {Object} key
  179. * @param {Object} value
  180. */
  181. setHeader(key, value) {
  182. this.header[key] = value;
  183. return this;
  184. }
  185. /**
  186. * 设置类型
  187. * @param {Object} type
  188. */
  189. setType(type) {
  190. this.type = type;
  191. }
  192. /**
  193. * 告诉监听者,被拦截了,可以进行反应操作
  194. */
  195. intercept() {
  196. if(this.baseCall._intercept != null) {
  197. this.baseCall._intercept.call(this);
  198. }
  199. }
  200. };
  201. export default Http;