uni-request封装

对uni-request的共同参数进行简单的封装,减少重复性请求代码。

创建utils文件夹存放工具类文件,之后在utils中创建request.js文件存放uni.request的请求方法。

request.js代码

​
//手机调试
let remote_ip = uni.getStorageSync("remote_ip")
remote_ip = remote_ip ? remote_ip : "默认IP"

let remote_port = uni.getStorageSync("remote_port")
remote_port = remote_port ? remote_port : "默认端口"

//#ifdef APP-PLUS
let serverUrl = `http://${remote_ip}:${remote_port}/api`
//#endif

//#ifdef H5
//web端调试
let serverUrl = '/dev-api'
//#endif

const $http = {
	request: function(method, url, data, option = {
		showLoading: false,
		noHeader: false,
		cancelLogin: false,
		ignoreError: false
	}) {
		var headers = {
			"Content-Type": "application/json",
			// "authorization": `Token ${uni.getStorageSync('token')}`,
			"Authorization": `${uni.getStorageSync('token')}`
		};
		if (option.noHeader) {
			headers = {
				"Content-Type": "application/json",
			};
		} else {
			headers = {
				"Content-Type": "application/json",
				// "authorization": `Token ${uni.getStorageSync('token')}`,
				"Authorization": `${uni.getStorageSync('token')}`
			};
		}

		return new Promise((resolve, reject) => {
			if (option.showLoading) {
				uni.showLoading({
					title: "加载中",
					mask: true
				});
			}
			if (url.indexOf('undefined') > -1 || url.indexOf('null') > -1) {
				if (option.showLoading) {
					uni.hideLoading()
				}
				reject()
			}

			uni.request({
				url: url,
				header: headers,
				data: data,
				method: method,
				dataType: 'json',
				success: function(res) {
					if (option.showLoading) {
						uni.hideLoading()
					}
					if (res.statusCode !== 200) {
						uni.showToast({
							title: "后台服务调用失败:" + res.statusCode,
							icon: "none"
						})
					} else {
						// console.log('res.data.errorCode:', res.data.errorCode)
						if ("000015" === res.data.errorCode || "000013" === res.data
							.errorCode || "000014" === res
							.data.errorCode) {
							// if ("000000" != res.data.errorCode){//经需求确认,只要状态码不是000000都强制退出
							uni.showToast({
								title: "登录信息失效,请重新登录",
								icon: "none"
							})

							setTimeout(() => {
								uni.reLaunch({
									url: "/pages/login/index"
								})
							}, 500)

						} else if ('32050930' == res.data.errorCode) {
							uni.showToast({
								title: res.data.errorMsg,
								icon: "none"
							})
						} else if (res.data.status !== 'SUCCEED') {
							resolve(res.data)
							uni.showToast({
								title: res.data.errorMsg,
								icon: "none"
							})
						} else {
							resolve(res.data);
						}
					}
				},
				fail: function(err) {
					if (option.showLoading) {
						uni.hideLoading()
					}
					// 与用户输入的IP地址无法建立连接时,给出提示,超时时长大概在20秒左右
					if(url.includes('/auth/auth/login')){
						uni.showToast({
							title: `${uni.getStorageSync("remote_ip")}:${uni.getStorageSync("remote_port")}"服务地址访问失败"`,
							icon: "none",
							duration: 3000
						})
						resolve(err)
					}else{
						reject(err)
					}
				}
			})
		})
	},
	get: function(url, data, option = {
		showLoading: false,
		noHeader: false,
		cancelLogin: false,
		ignoreError: false
	}) {
		let allurl = this.geturl(url);
		return this.request('GET', allurl, data, option)
	},
	post: function(url, data, option = {
		showLoading: false,
		noHeader: false,
		cancelLogin: false,
		ignoreError: false
	}) {
		let allurl = this.geturl(url);
		return this.request('POST', allurl, data, option)
	},
	delete: function(url, data, option = {
		showLoading: false,
		noHeader: false,
		cancelLogin: false,
		ignoreError: false
	}) {
		let allurl = this.geturl(url);
		return this.request('DELETE', allurl, data, option)
	},
	put: function(url, data, option = {
		showLoading: false,
		noHeader: false,
		cancelLogin: false,
		ignoreError: false
	}) {
		let allurl = this.geturl(url);
		return this.request('PUT', allurl, data, option)
	},

	geturl: function(url) {
		//#ifdef APP-PLUS
		if(uni.getStorageSync("remote_ip")){
			serverUrl = `http://${uni.getStorageSync("remote_ip")}:${uni.getStorageSync("remote_port")}/api`
		}
		//#endif
		console.log(url,"url");
		return serverUrl + url
	}
};

module.exports = $http

​

页面使用 

async suree() {
	let res = await this.$http.post('/auth/auth/login', {
			    userAccount:user,
				password:123,
			})
			if(res.status === "SUCCEED"){
					
			}else{
					
			}
},

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值