uniapp网络请求token统一封装。

const BASE_url = 'http://192.18.1.50:8201'  
export const myRequest = (options)=>{  //options 为传入的对象
    return new Promise((resolve,reject)=>{
        uni.request({
            url: BASE_url + options.url, 
            method: options.method || 'GET',
            data: options.data || {},
						header:{Authorization:'Bearer ' + uni.getStorageSync('token')},
            success: (res)=>{ 
				// console.log(res.data);
                resolve(res.data)           
            },
            fail: (err)=>{
				// console.log(err);
                uni.showToast({
                    title: 'network error!' , 
					 icon:"error",
					 duration:1000
                }) 
                reject(err)    
            }       
        })    
    })
}

在其他 post 调用

			async change(e) {
				let {index} = e.detail ;
				const res = await  this.$myRequest({
					url: '/xxxx-portal/deviceRemote/operating',
					method: "POST",
					data: {
						deviceCode: this.deviceCode,
						instructName: "operating-six-grid-device",
						data: {
							'DLV': index,
							'UID':uni.getStorageSync("memberId") ,
						}
					}
				});
				console.log(res);
				if (res.code == 200) {
					uni.showToast({
						title: `第${index+1}个关卡开始了`,
						icon: 'none'
					})
				} else {
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					})
				} 
				// uni.showToast({
				// 	title: `点击第${index+1}个宫格`,
				// 	icon: 'none'
				// })
			},

在其他地方get 调用

		async getpersoninfo(){
			 const res = await  this.$myRequest({
			 	url:'/xxx-portal/sso/info' ,
			 })   
			 if(res){ 
			 
				 uni.setStorageSync("memberId",res.data.id)
			 } 
		 },
以下是一个较为完整的uniapp网络请求接口封装代码: ```js // 封装请求函数 function request(url, data = {}, method = 'GET') { return new Promise((resolve, reject) => { uni.request({ url, data, method, header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + uni.getStorageSync('token') }, success: res => { // 请求成功 if (res.statusCode === 200) { resolve(res.data) } else { reject(res) } }, fail: err => { // 请求失败 reject(err) } }) }) } // 定义接口基础地址 const baseUrl = 'https://api.example.com' // 封装接口请求函数 export function login(data) { return request(`${baseUrl}/login`, data, 'POST') } export function getUserInfo(userId) { return request(`${baseUrl}/user/${userId}`) } // ... 还可以继续封装其他接口请求函数 ``` 在上面的代码中,我们在 `request` 函数中添加了一个请求头,用于传递身份验证信息。在每次请求接口时,都会携带一个名为 `Authorization` 的请求头,其值为当前用户的身份验证令牌。这样,在服务端就可以根据身份验证令牌来判断当前用户的身份了。 另外,我们定义了一个 `baseUrl` 常量,用于存储接口的基础地址。这样,在实际使用时,我们只需要在接口请求函数中指定相对地址即可。 在实际使用中,我们可以在需要发送网络请求的地方,直接引入并调用以上封装好的接口请求函数。例如: ```js import { getUserInfo } from '@/api/user' getUserInfo('123456').then(res => { console.log(res) }) ``` 以上代码会调用 `getUserInfo` 函数,向服务器请求 id 为 123456 的用户信息。请求成功后,会将返回的数据打印到控制台中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值