uniapp 请求封装

1.创建request.js文件

javascript 复制代码
export default {
	config: {
		baseUrl: "http://192.168.1.1:0000", // 示例
		header: {
			'Content-Type': 'application/json;charset=UTF-8'
			// 'Content-Type': 'application/x-www-form-urlencoded', 
		},
		data: {},
		method: "GET",
		dataType: "json",
		responseType: "text",
		success() {},
		fail() {},
		complete() {}
	},
	// 请求拦截器
	interceptor: {
		request: null,
		response: null
	},
	request(options) {
		if (!options) {
			options = {}
		}
		options.baseUrl = options.baseUrl || this.config.baseUrl
		options.dataType = options.dataType || this.config.dataType
		options.url = options.baseUrl + options.url
		options.data = options.data || {}
		options.method = options.method || this.config.method
		// 基于 Promise 的网络请求
		return new Promise((resolve, reject) => {
			uni.showLoading()
			let _config = null
			options.complete = (response) => {
				uni.hideLoading()
				// console.log(response, 'response.data',this);
				let {
					code,
					info
				} = response.data
				if (info == "登录信息过期") {
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login'
						})
					}, 1500)
				}
				if (code === 400) {
					uni.showToast({
						title: info,
						icon: "none",
						position: "center",
						duration: 2000
					})
					// reject(response)
				} else if (code === 200) {
					uni.showToast({
						title: info,
						icon: "none",
						position: "center",
						duration: 2000
					})
					resolve(response.data)
				} else if (code === 500) {
					uni.showToast({
						title: info,
						icon: "none",
						position: "center",
						duration: 2000
					})
					resolve(response.data)
				} else {
					resolve(response.data)
				}
			}
			_config = Object.assign({}, this.config, options)
			_config.requestId = new Date().getTime()

			if (this.interceptor.request) {
				this.interceptor.request(_config)
			}
			uni.request(_config);
		});
	},
	// url 为请求路径 data 为需要传递的值 options 进行封装
	// get请求
	get(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	// post请求
	post(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	// put请求
	put(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'PUT'
		return this.request(options)
	},
}

2.在main.js中引入

javascript 复制代码
import api from './request.js'

Vue.prototype.$http = api

3.页面请求接口

javascript 复制代码
async queryName() {
				const data = {
					name:''
				}
				const res = await this.$http.post("接口地址", data)
				console.log(res, 'res');
			},
相关推荐
10年前端老司机37 分钟前
React无限级菜单:一个项目带你突破技术瓶颈
前端·javascript·react.js
阿芯爱编程5 小时前
2025前端面试题
前端·面试
前端小趴菜056 小时前
React - createPortal
前端·vue.js·react.js
晓13136 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
菜包eo7 小时前
如何设置直播间的观看门槛,让直播间安全有效地运行?
前端·安全·音视频
烛阴7 小时前
JavaScript函数参数完全指南:从基础到高级技巧,一网打尽!
前端·javascript
军军君017 小时前
基于Springboot+UniApp+Ai实现模拟面试小工具四:后端项目基础框架搭建下
spring boot·spring·面试·elementui·typescript·uni-app·mybatis
chao_7898 小时前
frame 与新窗口切换操作【selenium 】
前端·javascript·css·selenium·测试工具·自动化·html
天蓝色的鱼鱼8 小时前
从零实现浏览器摄像头控制与视频录制:基于原生 JavaScript 的完整指南
前端·javascript
三原9 小时前
7000块帮朋友做了2个小程序加一个后台管理系统,值不值?
前端·vue.js·微信小程序