async,await使用例子

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"
				}
			}

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:{}
// })

在main.js里引入

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值