uniapp开发微信小程序请求超时设置【亲测有效】

在Hbuilderx中 使用uniapp开发微信小程序时 封装请求方法

请求代码如下

function requestFun(app) {
	// get请求
	app.config.globalProperties._get = function(path, data, success, fail, complete) {
		data = data || {};
		data.token = uni.getStorageSync('token') || '';
		uni.request({
			url: this.websiteUrl + '/' + path,
			data: data,
			dataType: 'json',
			method: 'GET',
			header: {
				'token': uni.getStorageSync('token') || ''
			},
			success: (res) => {
				if (res.statusCode === 500) {
					// 
					this.showError('请求超时,请稍后重试')
					this.showLoading = false
					return false;
				}
				if (res.statusCode !== 200 || typeof res.data !== 'object') {
					return false;
				}
				if (res.data.code === -2) {
					this.showError(res.data.msg, function() {
						uni.removeStorageSync('token');
						this.gotoPage('/pages/index/index', 'reLaunch');
					})
				} else if (res.data.code === -1) {
					// 登录态失效, 重新登录
					this.doLogin();
				} else if (res.data.code === 0) {
					success && success(res.data);
				} else {
					success && success(res.data);
				}
			},
			fail: (res) => {
			
				fail && fail(res);
				if(res.errMsg=="request:fail timeout"){
					this.showError('请求超时,请稍后重试')
					this.showLoading = false
				}
			},
			complete: (res) => {
				console.log(res,'completeres')
				complete && complete(res);
			},
		});
	};
	// post请求 单个参数
	app.config.globalProperties._post = function(path, data, success, fail, complete) {
		data = data || {};
		data.token = uni.getStorageSync('token') || '';
		uni.request({
			url: this.websiteUrl + '/' + path,
			data: data,
			dataType: 'json',
			method: 'POST',
			header: {
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			success: (res) => {
				if (res.statusCode === 500) {
					this.showError('请求超时,请稍后重试')
					this.showLoading = false
					return false;
					}
				if (res.statusCode !== 200 || typeof res.data !== 'object') {
					fail && fail(res);
					return false;
				}
				if (res.data.code === -1) {
					// 登录态失效, 重新登录
					this.doLogin();
				} else if (res.data.code === 0) {
					success && success(res.data);
				} else {
					success && success(res.data);
				}
			},
			fail: (res) => {
				fail && fail(res);
				if(res.errMsg=="request:fail timeout"){
					this.showError('请求超时,请稍后重试')
					this.showLoading = false
				}
			},
			complete: (res) => {
				complete && complete(res);
			},
		});
	};

	// post请求 多个参数
	app.config.globalProperties._postBody = function(path, data, success, fail, complete) {
		data = data || {};
		uni.request({
			url: this.websiteUrl + '/' + path,
			data: data,
			dataType: 'json',
			method: 'POST',
			header: {
				'Content-Type': 'application/json;charset=UTF-8',
				'token': uni.getStorageSync('token') || ''
			},
			success: (res) => {
				if (res.statusCode === 500) {
					// 
					this.showError('请求超时,请稍后重试')
					this.showLoading = false
					return false;
					}
				if (res.statusCode !== 200 || typeof res.data !== 'object') {
					fail && fail(res);
					return false;
				}
				if (res.data.code === -1) {
					// 登录态失效, 重新登录
					this.doLogin();
				} else if (res.data.code === 0) {
					success && success(res.data);
				} else {
					success && success(res.data);
				}
			},
			fail: (res) => {
				fail && fail(res);
				if(res.errMsg=="request:fail timeout"){
					this.showError('请求超时,请稍后重试')
					this.showLoading = false
				}
			},
			complete: (res) => {
				complete && complete(res);
			},
		});
	};
	  
	app.config.globalProperties._upload = function(path, data, success, fail, complete) {
	  	uni.uploadFile({
	  		url: this.websiteUrl + '/' + path,
	  		filePath: data,
	  		name: 'file',
	  		formData: {
				'token': uni.getStorageSync('token') || ''
	  		},
	  		success: (res) => {
	  			success && success(res.data);
	  		},
	  		fail: (res) => {
	  			fail && fail(res);
				if(res.errMsg=="request:fail timeout"){
					this.showError('请求超时,请稍后重试')
					this.showLoading = false
				}
	  		},
	  		complete: (res) => {
	  			complete && complete(res);
	  		},
	  	});
	};
}

export default requestFun

方法:全局配置响应时间

一、进入项目的manifest.json的代码视图模块

在这里插入图片描述
写入代码

"networkTimeout":{
		"request":15000
	},

表示现在request请求响应时间最多为15秒

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端成长营

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值