Captcha 验证码

Scan me!

验证码校验窗口

引入

  1. import { Captcha } from 'mand-mobile'
  2. Vue.component(Captcha.name, Captcha)

代码演示

自定义

  1. <template>
  2. <div class="md-example-child md-example-child-captcha">
  3. <md-field title="文案">
  4. <md-input-item
  5. title="标题"
  6. v-model="title"
  7. ></md-input-item>
  8. <md-input-item
  9. title="插槽内容"
  10. v-model="content"
  11. ></md-input-item>
  12. <md-input-item
  13. title="短信验证码"
  14. value="1234"
  15. readonly
  16. ></md-input-item>
  17. </md-field>
  18. <md-field title="配置">
  19. <md-field-item
  20. title="限制验证码长度"
  21. customized
  22. align="right">
  23. <md-switch v-model="limit"></md-switch>
  24. </md-field-item>
  25. <md-input-item
  26. title="验证码长度"
  27. type="tel"
  28. v-model="maxlength"
  29. ></md-input-item>
  30. <md-field-item
  31. title="采用掩码"
  32. customized
  33. align="right">
  34. <md-switch v-model="mask"></md-switch>
  35. </md-field-item>
  36. <md-field-item
  37. title="使用系统键盘"
  38. customized
  39. align="right">
  40. <md-switch v-model="system"></md-switch>
  41. </md-field-item>
  42. </md-field>
  43. <md-button @click="next">确定</md-button>
  44. <md-captcha
  45. ref="captcha"
  46. v-model="show"
  47. :title="title"
  48. :maxlength="limit ? parseFloat(maxlength) : -1"
  49. :system="system"
  50. :mask="mask"
  51. :appendTo="appendTo"
  52. @submit="submit"
  53. @show="onShow"
  54. @hide="onHide"
  55. @send="onSend"
  56. >
  57. {{content}}
  58. </md-captcha>
  59. </div>
  60. </template>
  61. <script>
  62. import {Button, Toast, Captcha, InputItem, Field, FieldItem, Switch} from 'mand-mobile'
  63. export default {
  64. name: 'captcha-demo',
  65. components: {
  66. [Button.name]: Button,
  67. [Captcha.name]: Captcha,
  68. [InputItem.name]: InputItem,
  69. [Field.name]: Field,
  70. [FieldItem.name]: FieldItem,
  71. [Switch.name]: Switch,
  72. },
  73. data() {
  74. return {
  75. show: false,
  76. appendTo: document.querySelector('.doc-demo-box-priview') || document.body,
  77. title: '输入验证码',
  78. content: '验证码已发送至 186****5407',
  79. limit: true,
  80. maxlength: '4',
  81. mask: false,
  82. system: false,
  83. }
  84. },
  85. methods: {
  86. next() {
  87. this.show = true
  88. },
  89. submit(val) {
  90. const max = parseFloat(this.maxlength)
  91. if (!this.limit || max < 0 || val.length === max) {
  92. if (val !== '1234') {
  93. this.$refs.captcha.setError('验证码错误,请重新输入')
  94. } else {
  95. this.show = false
  96. Toast({
  97. content: `你输入了${val}`,
  98. })
  99. }
  100. }
  101. },
  102. onSend() {
  103. console.log('click resend button.')
  104. },
  105. onShow() {},
  106. onHide() {},
  107. },
  108. }
  109. </script>
  110. <style lang="stylus">
  111. .md-example-child-captcha
  112. padding 20px
  113. .md-field
  114. margin-bottom 40px
  115. </style>
  116.  

内联

  1. <template>
  2. <div class="md-example-child md-example-child-captcha-1">
  3. <md-captcha
  4. :maxlength="4"
  5. :isView="true"
  6. >
  7. 验证码已发送至186****5407
  8. </md-captcha>
  9. </div>
  10. </template>
  11. <script>
  12. import {Captcha} from 'mand-mobile'
  13. export default {
  14. name: 'captcha-demo',
  15. components: {
  16. [Captcha.name]: Captcha,
  17. },
  18. }
  19. </script>
  20. <style lang="stylus">
  21. .md-example-child-captcha-1
  22. height 650px
  23. padding 30px 0
  24. .md-number-keyboard
  25. margin-top 30px
  26. </style>
  27.  

API

Captcha Props

属性说明类型默认值
v-model验证码窗口是否显示Booleanfalse
is-view是否内嵌在页面内展示,否则以弹层形式Booleanfalse
maxlength字符最大输入长度, 若为-1则不限制输入长度Number4
mask是否掩码Booleanfalse
system是否使用系统默认键盘Booleanfalse
auto-countdown1.3.0+是否自动开始倒计时,否则需手动调用countdownBooleantrue
title窗口标题String-
appendTo挂载节点HTMLElementdocument.body
count倒计时时长, 设置为0的时候不显示倒计时按钮Number60
countNormalText1.3.0+发送验证码正常状态文字String发送验证码
countActiveText1.3.0+发送验证码及倒计时按钮文案配置项String{$1}秒后重发

Captcha Methods

countdown()

手动重置倒计时

setError(message)

设置报错信息并显示

Captcha Events

@show()

验证码组件显示事件

@hide()

验证码组件隐藏事件

@send(countdown)

发送验证码事件, 第一次打开时或点击重发按钮后触发并开始倒计时,如果auto-countdownfalse需手动调用countdown开始倒计时

@submit(code)

用户提交输入内容事件