当前位置: 首页 > article >正文

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秒

在这里插入图片描述


http://www.kler.cn/a/538629.html

相关文章:

  • 利用NestJS构建高效的RESTful API接口
  • vue学习5
  • 【C++篇】 异常处理
  • harmonyOS生命周期详述
  • redis专栏解读
  • (六)C++的函数模板与类模板
  • [ Spring ] Integrate Spring Boot Service Monitor Prometheus and Grafana
  • 12.8 LangChain Agent 的灵魂:ReAct 框架理论与实战解析
  • 深入理解并行与并发:C++/Python实例详解
  • 迅为RK3568开发板篇OpenHarmony实操HDF驱动控制LED-在产品中新增子系统
  • 链表专题-03
  • vmware虚拟机可以使用Windows的GPU吗
  • 【JavaScript】《JavaScript高级程序设计 (第4版) 》笔记-Chapter6-集合引用类型
  • git撤销上一次的提交
  • C# Winform怎么设计串口,客户端和相机控件界面显示
  • Win11下搭建Kafka环境
  • 2. UVM的基本概念和架构
  • 深度学习在医疗影像分析中的应用
  • [Meet DeepSeek] 如何顺畅使用DeepSeek?告别【服务器繁忙,请稍后再试。】
  • 前端【技术方案】浏览器兼容问题(含解决方案、CSS Hacks、条件注释、特性检测、Polyfill 等)
  • SQL写法:行行比较
  • 【R语言】数据分析
  • fatal:Authentication failed for “http....
  • 现在中国三大运营商各自使用的哪些band频段
  • 【Pycharm+Git+Gitlab】安装部署(粗糙版)
  • 电脑黑屏按什么键恢复?电脑黑屏的解决办法