Browse Source

Signed-off-by: liuboyan <632697560@qq.com>
拦截器

liuboyan 4 years ago
parent
commit
6dcdb75369
1 changed files with 12 additions and 0 deletions
  1. 12 0
      utils/request.js

+ 12 - 0
utils/request.js

@@ -1,3 +1,4 @@
+let ajaxTimes = 0;
 const request = (url, data, method = 'GET') => {
 	return new Promise((resolve, reject) => {
 		let header = {
@@ -6,6 +7,11 @@ const request = (url, data, method = 'GET') => {
 		if (uni.getStorageSync('token')) {
 			header.token = 'Bearer ' + uni.getStorageSync('token')
 		}
+		ajaxTimes++;
+		uni.showLoading({
+			title: "加载中",
+			mask: true,
+		});
 		uni.request({
 			url: url,
 			data: data,
@@ -31,6 +37,12 @@ const request = (url, data, method = 'GET') => {
 			},
 			fail: res => {
 				reject(res)
+			},
+			complete: () => {
+				ajaxTimes--;
+				if (ajaxTimes === 0) {
+					uni.hideLoading();
+				}
 			}
 		})
 	});