uniapp封装promise请求

// 必须对外暴露一个方法,而且得返回一个promise对象const BASE_URL = 'http://119.29.194.223:88'function request({ url, method = 'GET', isAuth, data = {},}) { return new Promise((resolve, reject) => { // 默认header是空对象,用户无须登陆态,header可以传空对象 let header = {} // 需.
摘要由CSDN通过智能技术生成
// 必须对外暴露一个方法,而且得返回一个promise对象
const BASE_URL = 'http://119.29.194.223:88'

function request({
	url,
	method = 'GET',
	isAuth,
	data = {},
}) {
	return new Promise((resolve, reject) => {

		// 默认header是空对象,用户无须登陆态,header可以传空对象
		let header = {}

		// 需要登陆态
		if (isAuth) {
			let token = uni.getStorageSync('token')
			// 如果没有token,跳转login
			if (!token) {
				uni.navigateTo({
					url: '/pages/login/login.vue'
				})
				// return reject(new Error('请先登录'))
			}
			// 如果需要登陆态,就传递token,否则就不传
			header.Authorization = token
		}

		// 请求前,开启Loading
		uni.showLoading({
			title: '加载中...',
			mask: true
		});
		
		uni.request({
			url: BASE_URL + url,
			method,
			// header:header,
			header,
			data,
			success: res => {
				// 解构返回的数据
				let {
					msg,
					status,
					data
				} = res.data
				if (status === 1000) {
					// 如果meta.status===200,才表示数据是正确
					r
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
uniapp封装axios请求可以通过以下步骤进行: 第一步:安装依赖 你可以使用axios-miniprogram-adapter这个依赖来适配小程序网络请求的适配器。这样可以避免uniapp在使用axios请求时报"adapter is not a function"错误。你可以通过以下命令安装依赖: ``` npm i axios@0.26.0 axios-miniprogram-adapter ``` 或者 ``` yarn add axios@0.26.0 axios-miniprogram-adapter ``` 第二步:axios二次封装uniapp中,你可以对axios进行二次封装,以便更好地适配uniapp请求需求。你可以参考以下代码示例: ```javascript // 引入axios和适配器 import axios from 'axios' import adapter from 'axios-miniprogram-adapter' // 创建axios实例 const instance = axios.create({ adapter }) // 设置基本URL instance.defaults.baseURL = 'your_base_url' // 封装请求函数 function request(options) { return new Promise((resolve, reject) => { instance(options) .then(response => { resolve(response.data) }) .catch(error => { reject(error) }) }) } // 导出请求函数 export default request ``` 这样,你就可以在uniapp中使用封装好的axios请求了。例如: ```javascript import request from 'your_request_file_path' // 发起请求 request({ url: '/api/example', method: 'GET', data: { // 请求参数 } }) .then(response => { // 处理响应数据 }) .catch(error => { // 处理错误 }) ``` 通过以上步骤,你可以在uniapp封装axios请求,并且使用适配器来解决小程序网络请求的问题。希望对你有帮助! #### 引用[.reference_title] - *1* *2* [uniapp 微信小程序axios库的封装及使用](https://blog.csdn.net/weixin_43743175/article/details/129704892)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [uniapp封装axios?大可不必那么麻烦。](https://blog.csdn.net/m0_56344602/article/details/125390024)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值