uniapp封装request请求
创始人
2025-01-07 21:05:45
0

第一步:在你的根文件下创建一个 request 的目录;然后在request文件下创建两个api.js和index.js

第二步封装接口:index .js中:(''https://xxxxx.xxxxx.cn'',改成自己的)

// 全局请求封装 const base_url = 'https://xxxxx.xxxxx.cn' // 请求超出时间 const timeout = 5000   // 需要修改token,和根据实际修改请求头 export default (params) => { 	let url = params.url; 	let method = params.method || "get"; 	let data = params.data || {}; 	let header = { 		'Blade-Auth': uni.getStorageSync('token') || '', 		'Content-Type': 'application/json;charset=UTF-8', 		'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0', 		'Tenant-Id': uni.getStorageSync('tenantId') || 'xxx', // avue配置相关 		...params.header 	} 	if (method == "post") { 		header = { 			'Content-Type': 'application/json' 		}; 	} 	return new Promise((resolve, reject) => { 		uni.request({ 			url: base_url + url, 			method: method, 			header: header, 			data: data,             timeout, 			success(response) { 				const res = response 				// 根据返回的状态码做出对应的操作 				//获取成功 				// console.log(res.statusCode); 				if (res.statusCode == 200) { 					resolve(res.data); 				} else { 					uni.clearStorageSync() 					switch (res.statusCode) { 						case 401: 							uni.showModal({ 								title: "提示", 								content: "请登录", 								showCancel: false, 								success() { 									setTimeout(() => { 										uni.navigateTo({ 											url: "/pages/login/index", 										}) 									}, 1000); 								}, 							}); 							break; 						case 404: 							uni.showToast({ 								title: '请求地址不存在...', 								duration: 2000, 							}) 							break; 						default: 							uni.showToast({ 								title: '请重试...', 								duration: 2000, 							}) 							break; 					} 				} 			}, 			fail(err) { 				console.log(err) 				if (err.errMsg.indexOf('request:fail') !== -1) { 					uni.showToast({ 						title: '网络异常', 						icon: "error", 						duration: 2000 					}) 				} else { 					uni.showToast({ 						title: '未知异常', 						duration: 2000 					}) 				} 				reject(err);   			}, 			complete() { 				// 不管成功还是失败都会执行 				uni.hideLoading(); 				uni.hideToast(); 			} 		}); 	}).catch(() => {}); };

api.js中:(这两个是我自己的接口封装示例,改成自己的)

// 引入 request 文件 import request from './index.js'   // 获取学习列表详细信息登录 export const getLogin = ({ iphone, pwd }) => {   return request({     url: `/index.php/index/User/webLogin`,     method: 'post',     data: {       iphone,       pwd     }   }); }; // web用户的注册和修改接口 export const getZhuCe = ({ iphone, pwd ,webname}) => {   return request({     url: `/index.php/index/User/addWebuser`,     method: 'post',     data: {       iphone,       pwd, 	  webname     }   }); }; 

以下是我在页面中使用接口的示例代码

这是第一个接口里的使用实例代码:

  

这是第二个接口里的使用实例代码:

  

相关内容

热门资讯

技巧辅助挂!pokermast... 技巧辅助挂!pokermaster修改器,丹东约战麻将辅助器,演示教程(有挂细节)1、点击下载安装,...
现场直击!wepokerplu... 现场直击!wepokerplus万能挂,丰城双剑新版最强高分攻略,操作教程(有挂方针)1.丰城双剑新...
插件辅助挂!wepoker有辅... 插件辅助挂!wepoker有辅助器吗,乐平包王攻略,学习教程(有挂方略)1、首先打开乐平包王攻略辅助...
据玩家消息!拱趴大菠萝辅助神器... 据玩家消息!拱趴大菠萝辅助神器,多乐跑得快辅助器,机巧教程(证实有挂)1、在拱趴大菠萝辅助神器插件功...
此事备受玩家关注!来玩app破... 此事备受玩家关注!来玩app破解版,h5能反杀吗,绝活教程(有挂详细)1、打开软件启动之后找到中间准...
值得注意的是!aapoker破... 值得注意的是!aapoker破解侠是真的吗,蜀山四川游戏修改工具,经验教程(有挂助手)1、金币登录送...
第三方辅助!wepoker脚本... 第三方辅助!wepoker脚本,广东星悦有外开挂辅助器吗,法门教程(有挂分析)广东星悦有外开挂辅助器...
此事引发广泛关注!德州透视脚本... 此事引发广泛关注!德州透视脚本,崇阳斗棋辅助脚本视频,诀窍教程(的确有挂)暗藏猫腻,小编详细说明崇阳...
黑科技辅助挂!wepoker买... 黑科技辅助挂!wepoker买脚本靠谱吗,情怀七喜游戏辅助,法门教程(有挂方法)1、每一步都需要思考...
方法辅助挂!aapoker怎么... 方法辅助挂!aapoker怎么设置提高好牌几率,蘑菇云辅助使用视频,绝活儿教程(讲解有挂)1、完成蘑...