uniapp接口请求封装

1.新建request.js

// #ifdef H5
if (process.env.NODE_ENV === 'development') {
	//本地环境,即开发环境
	baseUrl = 'http://localhost:8000'
} else {
	//线上环境
	baseUrl = 'https://*******.**'
	console.log = () => {}
}
// #endif
 
 
//封装request请求
const sendRequest = (url, method = 'GET', data = {}, contentType) => {
	//判断header提交数据类型
	let types = '';
	if (method == 'POST' && !contentType) {
		types = 'application/x-www-form-urlencoded'
	} else if (method == 'POST' && contentType) {
		types = contentType
	} else {
		types = 'application/json';
	}
 
	// #ifdef H5
	var bases = baseUrl + url;
	// #endif
 
	var token = uni.getStorageSync('token') || '';
	return new Promise(function(resolve, reject) {
		uni.showLoading({
			title: '加载中...'
		});
		uni.request({
			url: bases,
			data: data,
			method: method,
			header: {
				'Content-Type': 'application/json',
				'Accept': 'application/json, text/javascript, */*; q=0.01',
				'Authorization':'Bearer '+ uni.getStorageSync("token"),
			},
			success(res) {
				uni.hideLoading();
				var code = res.data.code;
				console.log("res",res)
				switch (code) {
					case 20001:
						uni.showModal({
							title: '登录提示',
							content: '无权访问!请重新登录后再来操作',
							showCancel:false,
							success:ress => {
								uni.reLaunch({
									url:'/pages'
								})
							}
						})
						break;
					case 20002:
						uni.showModal({
							title: '登录提示',
							content: '身份已过期,请重新登录后再来操作!',
							showCancel:false,
							success:ress => {
								uni.redirectTo({
									url:'/pages'
								})
							}
						})
						break;
					case 20003:
						uni.showModal({
							title: '登录提示',
							content: 'Token生成失败,请重新登录!',
							showCancel:false,
							success:ress => {
								uni.redirectTo({
									url:'/pages'
								})
							}
						})
						break;
					case 20005:
						uni.showModal({
							title: '登录提示',
							content: '访问被禁止,请重新登录!',
							showCancel:false,
							success:ress => {
								uni.redirectTo({
									url:'/pages'
								})
							}
						})
						break;
					case 500:
						uni.showModal({
							title: '登录提示',
							content: '系统错误,请重新登录!',
							success:ress => {
								if (ress.confirm) {
									uni.redirectTo({
										url:'/pages'
									})
								}
							}
						})
						break;
						
					default:
						resolve(res);
						break;
				}
			},
			fail(err) {
				uni.hideLoading();
				uni.showToast({title:"出错啦,请稍后再试",icon:"error"});
				reject(err);
			}
		})
	})
}
 
module.exports.sendRequest = sendRequest

main.js

import http from './common/vmeitime-http/request.js'
Vue.config.productionTip = false
Vue.prototype.$http = http

GET

	this.$http.sendRequest(`/api/details/${id}`, 'GET', {}).then(res => {
		//成功回调
		if(res.data) {
			console.log('val——',res.data)
		} else {
			uni.showToast({title:'数据不存在',icon:"error"});
		}
	}).catch(err => {
		//请求失败
	})

POST

	this.$http.sendRequest('/api/login', 'POST', {}).then(res => {
		//成功回调
	}).catch(err => {
		//请求失败
	})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值