• 引入
  • 代码演示
    • 自定义
    • 内联
  • API
    • Captcha Props
    • Captcha Methods
      • countdown()
      • resetcount()
      • setError(message)
    • Captcha Events
      • @show()
      • @hide()
      • @send(countdown)
      • @submit(code)

    Captcha 验证码

    Scan me!

    验证码校验窗口

    引入

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

    代码演示

    自定义

    Captcha 验证码窗口 - 图2

    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. setTimeout(() => {
    92. if (!this.limit || max < 0 || val.length === max) {
    93. if (val !== '1234') {
    94. this.$refs.captcha.setError('验证码错误,请重新输入')
    95. } else {
    96. this.show = false
    97. Toast({
    98. content: `你输入了${val}`,
    99. })
    100. }
    101. }
    102. }, 300)
    103. },
    104. onSend() {
    105. console.log('click resend button.')
    106. },
    107. onShow() {},
    108. onHide() {},
    109. },
    110. }
    111. </script>
    112. <style lang="stylus">
    113. .md-example-child-captcha
    114. padding 20px
    115. .md-field
    116. margin-bottom 40px
    117. </style>
    118.  

    内联

    Captcha 验证码窗口 - 图3

            <template>
      <div class="md-example-child md-example-child-captcha-1">
        <md-captcha
          :maxlength="4"
          :isView="true"
          brief="最新验证码依然有效,请勿重发"
        >
          验证码已发送至186****5407
        </md-captcha>
      </div>
    </template>
    
    <script>
    import {Captcha} from 'mand-mobile'
    
    export default {
      name: 'captcha-demo',
      components: {
        [Captcha.name]: Captcha,
      },
    }
    
    </script>
    
    <style lang="stylus">
    .md-example-child-captcha-1
      height 650px
      padding 30px 0
      .md-number-keyboard
        margin-top 30px
    </style>
    
          

    API

    Captcha Props

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

    Captcha Methods

    countdown()

    开始倒计时

    resetcount()

    重置倒计时

    setError(message)

    设置报错信息并显示

    Captcha Events

    @show()

    验证码组件显示事件

    @hide()

    验证码组件隐藏事件

    @send(countdown)

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

    @submit(code)

    用户提交输入内容事件