uni-app中封装request请求

老样子第一步看官网→uni.request(OBJECT) | uni-app官网

先看看为封装之前的官网示例:

                

                                                        冲冲冲!!!

1.新建一个utils文件夹在里面新建一个js文件

2.在新建的文件夹里面(看不懂可以评论区留言)

export const BASE_URL = "http://sale.gomicsgene2.com:8092"
if (process.env.NODE_ENV === 'development') {
	// console.log("开发环境");
} else {
	// console.log("生产环境");
}

export const httpRequest = options => {
	return new Promise((resolve, reject) => {
		if (!options.loading) {
			uni.showLoading({
				title: '加载中',
				mask: true
			})
		}
		let header = {
			token: uni.getStorageSync('token')
		}
		uni.request({
			header: options.header || header,
			url: BASE_URL + options.url,
			method: options.method || "GET", // 请求类型,默认为GET
			data: options.data || {}, // 请求参数,默认空对象
			success: res => {
				console.log(res.code)
				if (res.data.code == 200) {
					resolve(res.data)
					uni.hideLoading()
				}
				else if (res.data.code == 20007) {
					console.log(20007)
					resolve(res.data)
					uni.reLaunch({
						url: '/pages_subPackages/login/login'
					})
				}
				else if (res.data.code == 401) {
					console.log(401)
					uni.clearStorage()
					uni.hideLoading()
					uni.reLaunch({
						url: '/pages_subPackages/login/login'
					})
				}else if (res.data.code == 10001 && res.data.code == 20004) {
					console.log(10001)
					resolve(res.data)
					uni.showToast({
						title: '请重新输入,输入有误',
						icon: 'none'
					})
				} else {
					console.log(res.msg)
					uni.hideLoading()
					// if (options.url === 'app/acUser') {
					// 	uni.showModal({
					// 		title: '提示',
					// 		content: res.data.msg,
					// 		confirmColor: '#FF577A',
					// 		showCancel: false
					// 	})
					// 	return
					// }
					if (res.data.code == 10001 && res.data.code == 20004) {
						console.log(10001)
						resolve(res.data)
						uni.showToast({
							title: '请重新输入,输入有误',
							icon: 'none'
						})
					} 
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: err => {
				console.log(13)
				uni.hideLoading()
				reject(err)
				uni.showToast({
					title: err.msg,
					icon: 'none'
				})
			}
		})
	})
}

export const getIndexInfo = options => {
	return new Promise((resolve, reject) => {
		let header = {
			"token": uni.getStorageSync('token'),
		}
		uni.request({
			header: options.header || header,
			url: BASE_URL + options.url,
			method: options.method || "GET", // 请求类型,默认为GET
			data: options.data || {}, // 请求参数,默认空对象
			success: res => {
				if (res.data.code == 200) {
					resolve(res.data)
				} else {
					uni.hideLoading()
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
					return
				}
			},
			fail: err => {
				uni.hideLoading()
				reject(err)
				uni.showToast({
					title: "接口请求失败,请联系管理员",
					icon: 'none'
				})

			}
		})
	})
}

 3.在main.js里面

import {httpRequest , BASE_URL} from 'util/api.js'
Vue.prototype.$httpRequest = httpRequest
Vue.prototype.$requestUrl = BASE_URL

 4.来个使用的示例

url:'请求地址',
method:'这里写请求方法(get,post)',
data:{参数}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值