uniapp 请求封装

 1.创建request.js文件

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中引入

import api from './request.js'

Vue.prototype.$http = api

3.页面请求接口

async queryName() {
				const data = {
					name:''
				}
				const res = await this.$http.post("接口地址", data)
				console.log(res, 'res');
			},

相关推荐

  1. uniapp 接口请求封装

    2024-07-09 21:24:03       22 阅读
  2. uniapp 请求封装

    2024-07-09 21:24:03       23 阅读
  3. uniapp+axios请求封装

    2024-07-09 21:24:03       27 阅读
  4. 全栈的自我修养 ———— uniapp封装api请求

    2024-07-09 21:24:03       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 21:24:03       49 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 21:24:03       53 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 21:24:03       42 阅读
  4. Python语言-面向对象

    2024-07-09 21:24:03       53 阅读

热门阅读

  1. Python人生重开器

    2024-07-09 21:24:03       19 阅读
  2. 【3】迁移学习模型

    2024-07-09 21:24:03       19 阅读
  3. Transformer 入门案例教程(大语言模型)

    2024-07-09 21:24:03       25 阅读
  4. 白骑士的C语言教学高级篇 3.4 C语言中的算法

    2024-07-09 21:24:03       21 阅读
  5. flask-apscheduler 定时任务被执行两次

    2024-07-09 21:24:03       19 阅读
  6. 部署Gunicorn + Flask应用到Docker

    2024-07-09 21:24:03       21 阅读
  7. VB 爬虫技术

    2024-07-09 21:24:03       21 阅读
  8. Self-Instruct构造Prompt的例子

    2024-07-09 21:24:03       20 阅读
  9. Oracle-查询表空间使用率很慢

    2024-07-09 21:24:03       21 阅读
  10. git reset HEAD^1

    2024-07-09 21:24:03       15 阅读
  11. 数据的统计探针:SKlearn中的统计分析方法

    2024-07-09 21:24:03       18 阅读
  12. 数据的完美贴合:SKlearn中的数据拟合方法全解

    2024-07-09 21:24:03       19 阅读