UNI-APP封装请求方法

使用说明

将以下复制创建一个命名为index.js的js文件

export default {
	config: {
		baseUrl: "",
		headers: {},
		dataType: "json",
		responseType: "text"
	},
	interceptor: {
		request: null,
		response: null
	},
	request(options) {
		return new Promise((resolve, reject) => {
			let _config = null
			options.url = this.config.baseUrl + options.url
			options.complete = (response) => {
				let statusCode = response.statusCode
				response.config = _config

				if (process.env.NODE_ENV === 'development') {
					if (statusCode === 200) {
					}
				}

				if (this.interceptor.response) {
					let newResponse = this.interceptor.response(response)
					if (newResponse) {
						response = newResponse
					}
				}

				if (statusCode === 200) { //成功
					resolve(response);
				} else {
					reject(response) 
				}
			}

			_config = Object.assign({}, this.config, options)
			_config.requestId = new Date().getTime()

			if (this.interceptor.request) {
				this.interceptor.request(_config)
			}

			if (process.env.NODE_ENV === 'development') {
				if (_config.data) {
				}
			}

			uni.request(_config);
		});
	},
	get(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	post(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	put(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'PUT'
		return this.request(options)
	},
	delete(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	}
}

把 index.js 文件夹 copy 到项目 common/js/http/ 目录下
在main.js添加以下代码

import http from '@/common/js/http/'
	//设置baseUrl
	http.config.baseUrl = "http://localhost:8080/api/"
	//设置请求前拦截器
	http.interceptor.request = (config) => {
		//添加通用参数
		config.header = {
			"token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
		}
	}
	//设置请求结束后拦截器
	http.interceptor.response = (response) => {
		//判断返回状态 执行相应操作
		return response;
	}

直接引用后使用

<script>
		import http from '@/common/js/http/'

		export default {
			data() {
				return {}
			},
			onLoad(option) {
				this.test();
				this.test1);
			},
			methods: {
				test () {
					http.get('user/list').then((res)=>{
						console.log(JSON.stringify(res))
					})

					http.get('user/list', {status: 1}).then((res)=>{
						console.log(JSON.stringify(res))
					})

					http.post('user', {id:1, status: 1}).then((res)=>{
						console.log(JSON.stringify(res))
					})

					http.put('user/1', {status: 2}).then((res)=>{
						console.log(JSON.stringify(res))
					})

					http.delete('user/1').then((res)=>{
						console.log(JSON.stringify(res))
					})
				},
				async test1() {
					let res = await this.http.get('user/list');
					console.log(JSON.stringify(res));
				}
			}
		}
	</script>

全局挂载后使用
在 main.js 添加以下代码

import http from '@/common/js/http/'
	Vue.prototype.$http = http

页面中使用

<script>
		export default {
			data() {
				return {}
			},
			onLoad(option) {
				this.test();
				this.test1);
			},
			methods: {
				test () {
					this.$http.get('user/list').then((res)=>{
						console.log(JSON.stringify(res))
					})

					this.$http.get('user/list', {status: 1}).then((res)=>{
						console.log(JSON.stringify(res))
					})

					this.$http.post('user', {id:1, status: 1}).then((res)=>{
						console.log(JSON.stringify(res))
					})

					this.$http.put('user/1', {status: 2}).then((res)=>{
						console.log(JSON.stringify(res))
					})

					this.$http.delete('user/1').then((res)=>{
						console.log(JSON.stringify(res))
					})
				},
				async test1() {
					let res = await this.$http.get('user/list');
					console.log(JSON.stringify(res));
				}
			}
		}
	</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无情的BUG生产机器

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值