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

目录
  • 需求背景
  • 思路分析
  • 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: {
    /**
     * @descripttion: 刷新二维码
     */
    handleRefreshCode() {
      this.$bus.$emit('refreshCode')
      this.handleCountDown()
      this.handleReportPayNotify()
      this.refresh = false
    },
    /**
     * @descripttion: 二维码倒计时
     */
    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)
      }
    },
    /**
     * @descripttion: 监听支付弹窗关闭
     */
    handleClosePay() {
      if (this.timer) {
        clearInterval(this.timer)
      }
      if (this.downTimer) {
        clearTimeout(this.downTimer)
      }
      this.$emit('closePay', { type: 'fail' })
      this.$message.warning('您已取消支付')
    },
    /**
     * @descripttion: 监测支付回调结果
     */
    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 扫码支付内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • vue 微信扫码登录(自定义样式)

    使用插件 https://www.npmjs.com/package/vue-wxlogin // 安装 npm install vue-wxlogin --save-dev // 组件中引入 import wxlogin from 'vue-wxlogin' ... components: { wxlogin } ... html中使用 <wxlogin :appid="$store.getters.wechat_app_id" :scope="'snsapi_log

  • Vue+abp微信扫码登录的实现代码示例

    最近系统中要使用微信扫码登录,根据微信官方文档和网络搜索相关文献实现了.分享给需要的人,也作为自己的一个笔记.后端系统是基于ABP的,所以部分代码直接使用了abp的接口,直接拷贝代码编译不通过. 注册微信开放平台账号# 在微信开放平台注册,注意是开放平台不是公众平台,这里需要300元,然后申请网站应用.审核通过后获取到AppID和AppSecret以及登记的网站url.只有此url下的地址微信扫码后才能回调. 具体申请条件见官方文档. 生成登录二维码# 在vue登录页面嵌入登录二维码,根据官方文

  • vue实现二维码扫码功能(带样式)

    需求: 利用vue实现二维码扫描: 插件: QRCodeReader: 插件下载 npm install --save vue-qrcode-reader 注意: 需要在https协议下才可以调用相机,实现扫码. 可以通过配置vue.config.js中的devServer:{https:true} 或参照前文章 前端使用Nuxt框架,配置本地https访问 配置本地证书 <template> <div> <p class="error">{{ er

  • vue实现扫码功能

    最近在项目中碰见一个打开摄像头扫码的功能,项目最后打包成app,用的是hBuilder打的包,刚好hBuilder打包集成H5+sdk,就可以直接用人家的sdk了. demo地址:vue-scan-demo 代码实现: <template> <div class="scan"> <div id="bcid"> <div style="height:40%"></div> <p cl

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

    目录 需求背景 思路分析 UI展示 开始使用 一 编写支付组件模板 二 支付组件的JS相关代码和说明 附:组件JS完整的源码 需求背景 市场报告列表展示的报告有两种类型,一种是免费报告,另一种是付费报告.免费报告用户可以直接查看,付费报告需要用户购买之后才能查看. 思路分析 点击查看为付费报告,弹出支付二维码. 创建订单,二维码进行倒计时,其展示5秒后开始监听支付回调结果,频次为五秒一次. 倒计时第一次倒数到0秒,提醒二维码过期让用户点击刷新二维码. 继续倒计时并开始监听支付回调结果. 刷新之后

  • SpringBoot整合Vue实现微信扫码支付以及微信退款功能详解

    直接上代码,在order模块添加依赖 <dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>0.0.3</version> </dependency> 在配置类添加申请的商家号信息 #关联的公众号appid weixin.pay.appid=wxXXXXXXX #商户号 weixi

  • SpringBoot微信扫码支付的实现示例

    一.首先导入生成二维码和微信支付环境 <!-- 生成二维码工具 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>com.google.zx

  • vue项目中使用bpmn-自定义platter的示例代码

    内容概述 本系列"vue项目中使用bpmn-xxxx"分为七篇,均为自己使用过程中用到的实例,手工原创,目前陆续更新中.主要包括vue项目中bpmn使用实例.应用技巧.基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下.如果转载或通过爬虫直接爬的,格式特别丑,请来原创看:我是作者原文 前情提要 经过前四篇的学习,我们能够实现bpmn基本绘图.预览.为节点加事件加颜色等效果,这一篇我们来说,如何自定义左侧工具栏(platter),首先看一下自定义前后效果图对比: 我

  • vue项目实现记住密码到cookie功能示例(附源码)

    本文介绍了vue项目实现记住密码到cookie功能示例,分享给大家,具体如下: 登陆页面 实现功能: 1.记住密码勾选,点登陆时,将账号和密码保存到cookie,下次登陆自动显示到表单内 2.不勾选,点登陆时候则清空之前保存到cookie的值,下次登陆需要手动输入 大体思路就是通过存/取/删cookie实现的:每次进入登录页,先去读取cookie,如果浏览器的cookie中有账号信息,就自动填充到登录框中,存cookie是在登录成功之后,判断当前用户是否勾选了记住密码,如果勾选了,则把账号信息存

  • vue项目中实现的微信分享功能示例

    本文实例讲述了vue项目中实现的微信分享功能.分享给大家供大家参考,具体如下: /* 微信分享 */ Vue.prototype.wechatShare = (shareData) => { let resource = { title: '随我心愿!', desc: '体验优质服务', link: 'https://www.abc.cn/', img: 'https://www.abc.cn/images/share_logo.jpg' } let obj = Object.assign({}

  • vue项目中axios请求网络接口封装的示例代码

    每个项目网络请求接口封装都是很重要的一块,第一次做Vue项目,我们的封装方法如下: (1).新建一个js文件,取名api.js (2).引入 axios ,mint-UI ,如下图: import axios from 'axios' import {MessageBox, Toast} from 'mint-ui' axios.defaults.timeout = 50000//默认请求超时时间 axios.defaults.headers = '请求头' (2).封装get方法 export

  • vue项目中导入swiper插件的方法

    版本选择 swiper是个常用的插件,现在已经迭代到了第四代:swiper4. 常用的版本是swiper3和swiper4,我选择的是swiper3. 安装 安装swiper3的最新版本3.4.2: npm i swiper@3.4.2 -S 这里一个小知识,查看node包的所有版本号的方法: npm view 包名 versions 组件编写 swiper官方的使用方法分为4个流程: 加载插件 HTML内容 给Swiper定义一个大小 初始化Swiper 我也按照这个流程编写组件: 加载插件

  • Java中Spring Boot支付宝扫码支付及支付回调的实现代码

    前言:最近开发支付宝支付功能,总结一下做个分享 官方文档:https://opendocs.alipay.com/apis 支付宝沙箱地址: https://openhome.alipay.com/platform/appDaily.htm?tab=info 支付宝支付流程: 准备工作:获取支付宝沙箱数据(APPID,支付宝网关,RSA2秘,沙箱支付账号等) 集成SpringBoot,使用Java代码发起支付请求 支付宝收到支付请求后,返回HTML代码片段,用于前端展示二维码 扫码支付成功后,支

  • vue项目中的支付功能实现(微信支付和支付宝支付)

    目录 项目中常见的支付方式 支付宝支付 微信支付 项目中常见的支付方式 支付宝支付 微信支付 余额支付(也需要支付宝或微信充值) 注意:本文仅从前端角度展开讲解 支付宝支付 项目难点: 页面是h5网页,用支付宝支付必须得到支付宝授权,调用支付宝的api. (如何授权请参照:调用支付宝api) 支付宝支付的一般过程是: 调用订单接口,获得订单号,支付金额等.    传递订单号,金额 至预支付接口    后台会返回来一个form,然后提交form自动跳转到支付宝支付页面 支付过程: 下图为为接口文档

随机推荐