广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >vue项目中扫码支付的实现示例(附demo)
  • 335
分享到

vue项目中扫码支付的实现示例(附demo)

2024-04-02 19:04:59 335人浏览 安东尼
摘要

目录需求背景思路分析UI展示开始使用一 编写支付组件模板二 支付组件的js相关代码和说明附:组件JS完整的源码需求背景 市场报告列表展示的报告有两种类型,一种是免费报告,另一种是付

需求背景

市场报告列表展示的报告有两种类型,一种是免费报告,另一种是付费报告。免费报告用户可以直接查看,付费报告需要用户购买之后才能查看。

思路分析

  • 点击查看为付费报告,弹出支付二维码。
  • 创建订单,二维码进行倒计时,其展示5秒后开始监听支付回调结果,频次为五秒一次。
  • 倒计时第一次倒数到0秒,提醒二维码过期让用户点击刷新二维码。
  • 继续倒计时并开始监听支付回调结果。
  • 刷新之后倒数到0秒还没有监听到结果则关闭支付弹窗,让用户重新发起支付。

UI展示

支付弹窗未过期长这样子喔

支付弹窗过期时长这样子喔

开始使用

支付功能作为项目的公共功能,所以我们单独封装一个组件,这样其他模块使用的时候就以子组件的方式引入。

一 编写支付组件模板

下面是模板具体的源码,由于样式不是我们考虑的重点,所以就不展示样式的代码了,根据需要自行添加哈。


<template>
  <div>
    <el-dialog
      class="dialog-pay"
      title=""
      :visible.sync="dialogVisible"
      :show-close="false"
      @close="handleClosePay"
    >
      <div class="content">
        <p class="tip">{{ pay.title }}</p>
        <p class="tip">
          支付金额:<span class="small">¥</span
          ><span class="large">{{ pay.money }}</span>
        </p>
        <img
          class="pic"
          :style="{ opacity: btnDisabled ? 1 : 0.3 }"
          :src="pay.url"
        />
        <el-button
          class="btn"
          :class="btnDisabled ? 'disabled' : ''"
          type="primary"
          :disabled="btnDisabled"
          @click="handleRefreshCode"
          >{{ btnText }}</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

二 支付组件的JS相关代码和说明

1. 监听支付弹窗是否显示
子组件通过props属性,在子组件中接收父组件传过来的值。用watch监听pay.show,只有为true的时候显示支付弹窗,并且在显示5秒后开始执行监听支付结果的方法。


watch: {
    'pay.show': {
      handler(val) {
        if (val) {
          this.dialogVisible = this.pay.show
          setTimeout(this.handleReportPayNotify(), 5000)
        }
      },
      immediate: true
    }
},

2. 二维码开始倒计时
二维码开始进行60秒的倒计时,到0秒提示点击刷新重新获取二维码,继续开始倒计时,此时如果到0秒则关闭支付弹窗,提示用户等待时间过长,请重新发起支付。


handleCountDown() {
  if (this.second == 1) {
    if (this.refresh) {
      this.second = 60
      this.btnDisabled = false
      this.btnText = '点击刷新重新获取二维码'
      if (this.timer) {
        clearInterval(this.timer)
      }
    } else {
      this.$emit('closePay', { type: 'fail' })
      clearInterval(this.timer)
      this.$message.warning('等待时间过长,请重新发起支付')
    }
  } else {
    this.second--
    this.btnDisabled = true
    this.btnText = `距离二维码过期剩余${this.second}秒`
    this.downTimer = setTimeout(() => {
      this.handleCountDown()
    }, 1000)
  }
},

3. 监听支付弹窗关闭


handleClosePay() {
  if (this.timer) {
    clearInterval(this.timer)
  }
  if (this.downTimer) {
    clearTimeout(this.downTimer)
  }
  this.$emit('closePay', { type: 'fail' })
  this.$message.warning('您已取消支付')
},

4. 监听支付回调结果
回调结果有两种,如果是正常范围内监听成功,则执行父组件传过来的fn,并清除定时器;如果监听到次数为12的时候还没有得到相应的结果,则关闭支付弹窗,提示用户等待时间过长,请重新发起支付,并清除定时器。


handleReportPayNotify() {
      let num = 0
      this.timer = setInterval(() => {
        num++
        this.pay.fn().then(res => {
          if (res.status == 111111) {
            this.$emit('closePay', { type: 'success' })
            clearInterval(this.timer)
          }
        })
        if (num == 12) {
          this.$emit('closePay', { type: 'fail' })
          clearInterval(this.timer)
          this.$message.warning('等待时间过长,请重新发起支付')
        }
      }, 5000)
    }

5. 支付组件销毁时清除定时器
这一步是容易忽略但是也是需要做的,当组件销毁时将定时器及时的清除掉。


  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer)
    }
    if (this.downTimer) {
      clearTimeout(this.downTimer)
    }
  }
}

附:组件JS完整的源码


<script>
export default {
  name: 'WechatPay',
  props: {
    pay: Object
  },
  data() {
    return {
      dialogVisible: false,
      btnDisabled: true,
      btnText: '',
      second: 60,
      timer: null,
      refresh: true
    }
  },
  watch: {
    'pay.show': {
      handler(val) {
        if (val) {
          this.dialogVisible = this.pay.show
          setTimeout(this.handleReportPayNotify(), 5000)
        }
      },
      immediate: true
    }
  },
  mounted() {
    this.handleCountDown()
  },
  methods: {
    
    handleRefreshCode() {
      this.$bus.$emit('refreshCode')
      this.handleCountDown()
      this.handleReportPayNotify()
      this.refresh = false
    },
    
    handleCountDown() {
      if (this.second == 1) {
        if (this.refresh) {
          this.second = 60
          this.btnDisabled = false
          this.btnText = '点击刷新重新获取二维码'
          if (this.timer) {
            clearInterval(this.timer)
          }
        } else {
          this.$emit('closePay', { type: 'fail' })
          clearInterval(this.timer)
          this.$message.warning('等待时间过长,请重新发起支付')
        }
      } else {
        this.second--
        this.btnDisabled = true
        this.btnText = `距离二维码过期剩余${this.second}秒`
        this.downTimer = setTimeout(() => {
          this.handleCountDown()
        }, 1000)
      }
    },
    
    handleClosePay() {
      if (this.timer) {
        clearInterval(this.timer)
      }
      if (this.downTimer) {
        clearTimeout(this.downTimer)
      }
      this.$emit('closePay', { type: 'fail' })
      this.$message.warning('您已取消支付')
    },
    
    handleReportPayNotify() {
      let num = 0
      this.timer = setInterval(() => {
        num++
        this.pay.fn().then(res => {
          if (res.status == 111111) {
            this.$emit('closePay', { type: 'success' })
            clearInterval(this.timer)
          }
        })
        if (num == 12) {
          this.$emit('closePay', { type: 'fail' })
          clearInterval(this.timer)
          this.$message.warning('等待时间过长,请重新发起支付')
        }
      }, 5000)
    }
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer)
    }
    if (this.downTimer) {
      clearTimeout(this.downTimer)
    }
  }
}
</script>

到此这篇关于Vue项目中扫码支付的实现示例(附demo)的文章就介绍到这了,更多相关vue 扫码支付内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: vue项目中扫码支付的实现示例(附demo)

本文链接: https://www.lsjlt.com/news/134259.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • vue项目中扫码支付的实现示例(附demo)
    目录需求背景思路分析UI展示开始使用一 编写支付组件模板二 支付组件的JS相关代码和说明附:组件JS完整的源码需求背景 市场报告列表展示的报告有两种类型,一种是免费报告,另一种是付...
    99+
    2022-11-12
  • vue项目中的支付功能实现(微信支付和支付宝支付)
    目录项目中常见的支付方式    支付宝支付微信支付项目中常见的支付方式     支付宝支付  &nbs...
    99+
    2022-11-12
  • Vue+SpringBoot实现支付宝沙箱支付的示例代码
    首先去下载支付宝沙箱的一系列东西,具体的配置什么的我就不说了,有很多博客都讲了,还有蚂蚁金服官方也说的很详细,我就直接说怎么样把后端的支付页面显示到Vue前端来: 在你配置好Ali...
    99+
    2022-11-12
  • 如何进行vue项目中的支付功能实现(微信支付和支付宝支付)
    如何进行vue项目中的支付功能实现(微信支付和支付宝支付),针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。项目中常见的支付方式    支付宝支付...
    99+
    2023-06-22
  • Vue项目实现html5图片上传的示例代码
    目录图例1.选择图片2.预览图片2.1添加图片预览代码 两种方法的对比3.裁剪图片4.上传选择图片 -> 预览图片 -> 裁剪图片 -> 上传图片 我会以...
    99+
    2022-11-12
  • vue项目中使用rem替换px的实现示例
    目录工具安装插件在项目根目录下添加.postcssrc.js文件index.html关于移动端页面适配,rem和vw适配方案 基础点:rem相对根节点字体的大小。所以不用px; 根字...
    99+
    2022-11-12
  • vue移动端项目中如何实现页面缓存的示例代码
    背景 在移动端中,页面跳转之间的缓存是必备的一个需求。 例如:首页=>列表页=>详情页。 从首页进入列表页,列表页需要刷新,而从详情页返回列表页,列表页则需要保持页面缓...
    99+
    2022-11-12
  • vue项目中vue-i18n和element-ui国际化开发实现的示例分析
    这篇文章主要介绍vue项目中vue-i18n和element-ui国际化开发实现的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!在vue构建的项目中,我们常用element-...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作