当前位置:   article > 正文

async,await使用例子_const res = await this.httputil.request(`192.168.1

const res = await this.httputil.request(`192.168.137.1/readers/login`, { met
async yes() {
	const res = await this.$myrequest({
	url: '/mcenter/member/withdrawal',
	method: 'POST',
	data: {
		identity: this.zhanghao,
		name: this.name,
		money: this.qian,
		},
		header: {
		'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
			},
		})
		if (res.data.code == 1) {
			uni.showToast({
				title: res.data.msg,
				icon: "success",
				duration: 2000
				     })
				location.reload()
				} else if (res.data.code == -1) {
					uni.showToast({
						title: "即将跳转登录",
						icon: "error",
						duration: 2000
					})
					let url = encodeURIComponent(
						"https://www.zhilongyihuo.com/html/qiye/#/pages/contact/contact?shop_id=" + this.shopid)
					window.location.href =
						"https://www.zhilongyihuo.com/index.php?s=/mobile/passport/login.html&redirecturl=" + url
				} else {
					return uni.showToast({
						title: res.data.msg,
						icon: "error",
						duration: 2000
					})
					window.location.href = "https://www.zhilongyihuo.com/html/qiye/#/pages/cart/cart"
				}
			}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

this.$myrequest为封装的文件内容为

const base_url = "https://www.zhilongyihuo.com/index.php"
export const myrequest = (options) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: base_url + options.url,
			method: options.method || 'GET',
			data: options.data || {},
			header: options.header || {},
			success: (res) => {
				// console.log(res)
				// if (res.data.code == -1) {

				// 	uni.showToast({
				// 		title: "即将跳转登录",
				// 		icon: "error",
				// 		duration: 2000
				// 	})
				// 	window.location.href =
				// 		"https://www.zhilongyihuo.com/index.php?s=/mobile/passport/login.html"


				// }
				resolve(res)
			},
			fail: (err) => {
				uni.showToast({
					title: "请求接口失败",
					icon: "error"
				})
				reject(err)
			}
		})
	})
}
// myrequest({
// 	url:'/api/getinfo',
// 	method:'POST',
// 	data:{}
// })
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

在main.js里引入

import {myrequest} from './until/api.js'
Vue.prototype.$myrequest = myrequest
  • 1
  • 2

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/294601
推荐阅读
相关标签
  

闽ICP备14008679号