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     }   }); }; 

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

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

  

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

  

相关内容

热门资讯

第三方技巧!hhpoker软件... 您好,hhpoker可以控制吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
最终!德普之星透视软件免费入口... 最终!德普之星透视软件免费入口官网,aapoker透视脚本,切实存在有挂(今日头条)1、下载好德普之...
日前!模拟器打开hhpoker... 日前!模拟器打开hhpoker,德普辅助软件,总是确实有挂(有挂解惑)1、进入到模拟器打开hhpok...
2026版总结!werplan... 2026版总结!werplan免费挂下载,德州圈脚本,好像真的有挂(有挂技巧)一、werplan免费...
反观!wpk真吗,hhpoke... 反观!wpk真吗,hhpoker破解工具,都是真的有挂(证实有挂)运hhpoker破解工具辅助工具,...
在玩家背景下!hhpoker透... 在玩家背景下!hhpoker透视脚本下载,hhpoker辅助挂,一直确实有挂(有挂详情)1、让任何用...
黑科技辅助挂!fishpoke... 黑科技辅助挂!fishpoker透视底牌,hhpoker有没有作弊挂,一直真的是有挂(真的有挂)1、...
今天上午!aapoker怎么选... 今天上午!aapoker怎么选牌,wepoker私人辅助器,都是是真的有挂(有挂实锤)1、不需要AI...
据目击者称!wpk德州局透视,... 据目击者称!wpk德州局透视,wpk是真的还是假的,真是存在有挂(有挂规律)1、玩家可以在wpk是真...
第三方技巧!xpoker辅助工... 第三方技巧!xpoker辅助工具,wpk俱乐部怎么作弊,一直是有挂(有挂透明挂)1、上手简单,内置详...