pansl 1 год назад
Родитель
Сommit
754fd74323
2 измененных файлов с 57 добавлено и 11 удалено
  1. 56 10
      common/http.js
  2. 1 1
      pages/client/tuan/detail.nvue

+ 56 - 10
common/http.js

@@ -20,6 +20,26 @@ export default {
 		dataType: "json",
 	},
 	async request(options = {}) {
+		// 请求拦截器
+		uni.addInterceptor('request', {
+			invoke: (request) => {
+				// 在发送请求之前的处理逻辑,例如添加请求头、请求日志等
+				// request.header = {
+				// 	'Content-Type': 'application/json', // 设置请求头
+				// 	// 在这里可以添加其他自定义请求头
+				// };
+				// console.log('Request:', request);
+				return request;
+			},
+		});
+		// 响应拦截器
+		uni.addInterceptor('response', {
+			invoke: (response) => {
+				// 在接收到响应数据之后的处理逻辑,例如统一处理错误码、响应日志等
+				// console.log('Response:', response);
+				return response;
+			},
+		});
 		options.header = options.header || this.config.header;
 		options.method = options.method || this.config.method;
 		options.dataType = options.dataType || this.config.dataType;
@@ -37,18 +57,44 @@ export default {
 		// #endif
 		options.url = this.config.baseUrl + options.url;
 		console.log('options.url ', options.url)
-		return uni.request(options).then(res => {
-			res = res[1].data;
-			if (res.code == 500201) {
-				// 未登录
-				clearToken();
-				getToken();
-				if (filterNoToken.indexOf(options.url) === -1) {
-					return this.request(options);
+		return new Promise((resolve, reject) => {
+			options.success = (e) => {
+				console.log(e, 'successsuccesssuccesssuccesssuccess')
+				let res = e.data;
+				if (e.statusCode === 200) {
+					if (res.code == 500201) {
+						// 未登录
+						clearToken();
+						getToken();
+						if (filterNoToken.indexOf(options.url) === -1) {
+							return this.request(options);
+						}
+					} else {
+						if (res.code != 0) {
+							uni.showToast({
+								title: res.msg,
+								duration: 1000,
+								icon: 'none'
+							});
+						}
+					}
+					resolve(res.data);
+				} else {
+					uni.showToast({
+						title: "网络异常,请稍后再试",
+						duration: 1000,
+						icon: 'none'
+					});
+					reject(new Error('Request failed'));
 				}
 			}
-			return res.data;
-		});
+			options.fail = (err) => {
+				console.log(err)
+				reject(err);
+			}
+			uni.request(options)
+		})
+
 	},
 	get(url, data, options = {}) {
 		options.url = url;

+ 1 - 1
pages/client/tuan/detail.nvue

@@ -276,7 +276,7 @@
 			},
 			unBingewatch() {
 				delShelf(this.video_id).then(res => {
-					console.log(res, 'storeshelfstoreshelfstoreshelf')
+					console.log(res, 'delShelfdelShelfdelShelfdelShelfdelShelf')
 					this.videoInfo.is_binge_watch = !Boolean(this.videoInfo.is_binge_watch)
 				})
 			},