[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
在vue项目中,支付功能并不复杂,具体需求需要配合后端一起实现,本文章将带你简单了解支付功能实现的流程
支付界面如下所示:
当点击立即支付时,触发支付事件:
支付流程:
调用支付接口→拿到form表单→调起支付页面→查询支付结果
支付详解:
1、支付接口的参数并不是固定的,而是由后端决定,在接口发送成功后,我们会的得到响应数据,该数据并非JSON格式,而是一个表单数据!
2、接下来如何将得到的数据调起支付界面呢?我们需要在页面中有一个可以提供渲染的元素,该元素可以在网页的任何位置
<div ref="alipayWap" v-html="alipay" />
3、在接口响应成功后,将数据渲染到元素内,通过触发表单的提交事件,就可以调起支付接口了,是不是非常简单
// 调用支付接口 payTwoHundred({}).then((res) => { // 渲染支付页面 this.alipay = res.data;
// 防抖避免重复支付 this.$nextTick(() => { // 提交支付表单
this.$refs.alipayWap.children[0].submit(); setTimeout(() => { // this.toPayFlag
= false; }, 500); }); }
4、成功后调起的支付界面
其实以上,支付功能已经实现了,但是在支付之后,如何验证是否支付成功呢???
我们需要获取到该支付的订单ID,然后向后端发送请求以验证,但是支付页面跳转之后,是获取不到当前订单ID的,接下来我们需要:
1、在付款成功后,让后端将页面重定向到原来的页面,将需要到参数回调到地址栏
2、回到页面中我们发现,地址栏多了很多参数,截取地址栏的参数,就可以获得订单ID了,接下来只需要发请求验证既可
参考代码:
// 查询是否支付成功 queryPay() { // 获取地址栏 const href = window.location.href; //
截取地址栏参数 const orderId = href.split('?')[1].split('&')[1].split('=')[1]; const
param = { orderId: orderId, type: '支付宝支付' }; getOrderPayType(param).then((res)
=> { if (res.code === '0000') { this.$message.success('支付成功'); } else {
this.$message.error('支付失败'); } }); },