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');
			},
相关推荐
hashiqimiya12 分钟前
html实现右上角有个图标,鼠标移动到该位置出现手型,点击会弹出登录窗口。
前端·html
古夕12 分钟前
前端文件下载的三种方式:a标签、Blob、ArrayBuffer
前端·javascript·vue.js
纯真时光14 分钟前
Vue3中pinia状态管理库的使用(Composition API 风格)
前端
万少22 分钟前
可可图片编辑 HarmonyOS(5)滤镜效果
前端
Yvonne爱编码1 小时前
后端编程开发路径:从入门到精通的系统性探索
java·前端·后端·python·sql·go
不知名的前端专家1 小时前
uniapp原生插件 TCP Socket 使用文档
网络·tcp/ip·uni-app·netty
GIS之路2 小时前
GDAL 读取遥感影像数据
前端
fakaifa3 小时前
【独立版】智创云享知识付费小程序 v5.0.23+小程序 搭建教程
小程序·uni-app·知识付费·源码下载·智创云享独立版
IT_陈寒3 小时前
Spring Boot 3.2 新特性全解析:这5个性能优化点让你的应用提速50%!
前端·人工智能·后端
2501_916007473 小时前
Transporter App 使用全流程详解:iOS 应用 ipa 上传工具、 uni-app 应用发布指南
android·ios·小程序·https·uni-app·iphone·webview