uniapp uview封装的请求方式

1.下载uview

下载地址
本地引入

2.配置Http请求

配置详细指导

完整如下

以下为在main.js中的引入示例:

// main.js

// 此为main.js本身已有内容
const app = new Vue({
  ...App
})

// http拦截器,此为需要加入的内容,如果不是写在common目录,请自行修改引入路径
import httpInterceptor from '@/common/http.interceptor.js'
// 这里需要写在最后,是为了等Vue创建对象完成,引入"app"对象(也即页面的"this"实例)
Vue.use(httpInterceptor, app)

app.$mount()
// @common/http.interceptor.js
 const config = {
	baseUrl: 'xxx', // 请求的本域名
	method: '', //POST/GET
	// 设置为json,返回后会对数据进行一次JSON.parse()
	dataType: 'json',
	showLoading: true, // 是否显示请求中的loading
	loadingText: '努力加载中~', // 请求loading中的文字提示
	loadingTime: 800, // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
	originalData: false, // 是否在拦截器中返回服务端的原始数据
	loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
	// 配置请求头信息
	header: {
		'content-type': 'application/json;charset=UTF-8'
	},
}
const install = (Vue, vm) => {
	// 此为自定义配置参数,具体参数见上方说明
	Vue.prototype.$u.http.setConfig({
		baseUrl: config.baseUrl,
		loadingText: config.loadingText,
		loadingTime: config.loadingTime,
		// 设置自定义头部content-type
		header: {
			'content-type': 'application/json;charset=UTF-8'
		}
		// ......
	});

	// 请求拦截部分,如配置,每次请求前都会执行,见上方说明
	Vue.prototype.$u.http.interceptor.request = (config) => {
		// 引用token
		// 方式一,存放在vuex的token,假设使用了uView封装的vuex方式
		// 见:https://uviewui.com/components/globalVariable.html
		// config.header.token = vm.token;

		// 方式二,如果没有使用uView封装的vuex方法,那么需要使用$store.state获取
		// config.header.token = vm.$store.state.token;

		// 方式三,如果token放在了globalData,通过getApp().globalData获取
		// config.header.token = getApp().globalData.username;

		// 方式四,如果token放在了Storage本地存储中,拦截是每次请求都执行的
		// 所以哪怕您重新登录修改了Storage,下一次的请求将会是最新值
		// const token = uni.getStorageSync('token');
		// config.header.token = token;
		config.header = {
		                'user-token':'Bearer ' + 'f50575df95a901fa87c8c25c71e44819'
		  }
		// 可以对某个url进行特别处理,此url参数为this.$u.get(url)中的url值
		if (config.url == '/user/login') config.header.noToken = true;
		// 最后需要将config进行return
		// console.log(config,"请求参数")
		return config;
		// 如果return一个false值,则会取消本次请求
		// if(config.url == '/user/rest') return false; // 取消某次请求
	}

	// 响应拦截,如配置,每次请求结束都会执行本方法
	Vue.prototype.$u.http.interceptor.response = (res) => {
		if (res.code == 200) {
			// res为服务端返回值,可能有code,result等字段
			// 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到
			// 如果配置了originalData为true,请留意这里的返回值
			return res.data;
		} else if (res.code == 201) {
			// 假设201为token失效,这里跳转登录
			vm.$u.toast('验证失败,请重新登录');
			setTimeout(() => {
				// 此为uView的方法,详见路由相关文档
				vm.$u.route('/pages/user/login')
			}, 1500)
			return false;
		} else {
			// 如果返回false,则会调用Promise的reject回调,
			// 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中,res为服务端的返回值
			return false;
		}
	}
}

export default {
	install
}

3.发送请求

1.同步请求

export default {
		onLoad() {
			this.$u.get('/ebapi/store_api/hot_search', {
				id: 2
			}).then(res => {
				this.result = res;
			})
			
			this.$u.post('/ebapi/public_api/index', {
				id: 1
			}).then(res => {
				this.result = res;
			})
		}
	}

2.异步请求

export default {
	methods: {
		async login() {
			let ret = await this.$u.post('/user/login');
			// 此处在函数体外写了async,并且使用了await等待返回,所以可以打印ret结果
			// 意味着这里的console.log是等待了几十毫秒请求返回后才执行的
			console.log(ret);
		}
	}
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值