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

发布于:2025-02-10 ⋅ 阅读:(46) ⋅ 点赞:(0)

在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秒

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到