美文网首页
vue对接vaptcha验证码组件

vue对接vaptcha验证码组件

作者: 泛酸的桂花酒 | 来源:发表于2020-07-03 09:36 被阅读0次

修改自网上的demo,填入对应的vid即可,如需修改配置,传入组件对应的props即可。

<template>
  <div ref="vaptcha" style="width:300px;height:36px">
    <div class="vaptcha-init-main">
      <div class="vaptcha-init-loading">
        <a href="https://www.vaptcha.com/" target="_blank"><img src="https://cdn.vaptcha.com/vaptcha-loading.gif"/></a>
        <span class="vaptcha-text">VAPTCHA启动中...</span>
      </div>
    </div>
  </div>
</template>

<script>
  const extend = function(to, _from) {
    for (const key in _from) {
      to[key] = _from[key]
    }
    return to
  }

  export default {
    props: {
      type: {
        type: String,
        default: 'click'
      },
      scene: {
        type: [String,Number],
        default: 0
      },
      vpStyle: {
        type: String,
        default: 'dark'
      },
      color: {
        type: String,
        color: '#3C8AFF'
      },
      lang: {
        type: String,
        default:'auto'
      },
    },
    mounted() {
      var config = extend({
        vid: '5efc030de988dd2fc1b8e91b',
        container: this.$refs.vaptcha,
        style: this.vpStyle
      }, this.$props)
      this.loadV2Script().then(() => {
        window.vaptcha(config).then(obj => {
          this.$emit('input', obj)
          obj.render()
        })
      })
    },
    methods: {
      loadV2Script() {
        if (typeof window.vaptcha === 'function') { //如果已经加载就直接放回
          return Promise.resolve()
        } else {
          return new Promise(resolve => {
            var script = document.createElement('script')
            script.src = 'https://v.vaptcha.com/v3.js'
            script.async = true
            script.onload = script.onreadystatechange = function() {
              if (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') {
                resolve()
                script.onload = script.onreadystatechange = null
              }
            }
            document.getElementsByTagName("head")[0].appendChild(script)
          })
        }
      }
    }
  }
</script>

<style>
  .vaptcha-init-main {
    display: table;
    width: 100%;
    height: 100%;
    background-color: #EEEEEE;
  }

  .vaptcha-init-loading {
    display: table-cell;
    vertical-align: middle;
    text-align: center
  }

  .vaptcha-init-loading>a {
    display: inline-block;
    width: 18px;
    height: 18px;
  }

  .vaptcha-init-loading>a img {
    vertical-align: middle
  }

  .vaptcha-init-loading .vaptcha-text {
    font-family: sans-serif;
    font-size: 12px;
    color: #CCCCCC;
    vertical-align: middle
  }
</style>

相关文章

网友评论

      本文标题:vue对接vaptcha验证码组件

      本文链接:https://www.haomeiwen.com/subject/ixvjqktx.html