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');
			},
相关推荐
专注API从业者2 小时前
Python + 淘宝 API 开发:自动化采集商品数据的完整流程
大数据·运维·前端·数据挖掘·自动化
烛阴2 小时前
TypeScript高手密技:解密类型断言、非空断言与 `const` 断言
前端·javascript·typescript
样子20183 小时前
Uniapp 之renderjs解决swiper+多个video卡顿问题
前端·javascript·css·uni-app·html
Nicholas683 小时前
flutterAppBar之SystemUiOverlayStyle源码解析(一)
前端
黑客飓风4 小时前
JavaScript 性能优化实战大纲
前端·javascript·性能优化
emojiwoo5 小时前
【前端基础知识系列六】React 项目基本框架及常见文件夹作用总结(图文版)
前端·react.js·前端框架
fakaifa5 小时前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
张人玉5 小时前
XML 序列化与操作详解笔记
xml·前端·笔记
杨荧6 小时前
基于Python的宠物服务管理系统 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python·信息可视化
YeeWang6 小时前
🎉 Eficy 让你的 Cherry Studio 直接生成可预览的 React 页面
前端·javascript