前端使用封装好的验证码,复制即用(一)

前端使用封装好的验证码,复制即用(一)


前言

前端使用封装好的验证码,复制即用


提示:以下是本篇文章正文内容,下面案例可供参考

一、使用步骤

1.创建文件

在src下创建components文件夹 创建ValidCode.vue

<template>
    <div class="ValidCode disabled-select" style="width: 100%; height: 100%" @click="refreshCode">
      <span v-for="(item, index) in codeList" :key="index" :style="getStyle(item)">{{item.code}}</span>
    </div>
  </template>
  
  <script>
  export default {
    name: 'validCode',
    data () {
      return {
        length: 4,
        codeList: []
      }
    },
    mounted () {
      this.createdCode()
    },
    methods: {
      refreshCode () {
        this.createdCode()
      },
      createdCode () {
        let len = this.length,
            codeList = [],
            chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789',
            charsLen = chars.length
        // 生成
        for (let i = 0; i < len; i++) {
          let rgb = [Math.round(Math.random() * 220), Math.round(Math.random() * 240), Math.round(Math.random() * 200)]
          codeList.push({
            code: chars.charAt(Math.floor(Math.random() * charsLen)),
            color: `rgb(${rgb})`,
            padding: `${[Math.floor(Math.random() * 10)]}px`,
            transform: `rotate(${Math.floor(Math.random() * 90) - Math.floor(Math.random() * 90)}deg)`
          })
        }
        // 指向
        this.codeList = codeList
        // 将当前数据派发出去
        this.$emit('update:value', codeList.map(item => item.code).join(''))
      },
      getStyle (data) {
        return `color: ${data.color}; font-size: ${data.fontSize}; padding: ${data.padding}; transform: ${data.transform}`
      }
    }
  }
  </script>
  
  <style>
  .ValidCode{
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
  }
  .ValidCode span {
    display: inline-block;
    font-size: 18px;
  }
  </style>

2.主页中使用

代码如下(示例):

<script>
import ValidCode from '@/components/ValidCode.vue'   //here
export default {
    name:'Login',
    components:{
        ValidCode   //here

    },
    data(){
        return{

...........
<el-form-item prop="valiCode">  <!--验证码  前台校验-->
	<div style="display: flex;">   <!--平均分为左右2部分-->
		<el-input prefix-icon="el-icon-edit" style="flex:1"></el-input>
			<div style="flex: 1;">
				<valid-code></valid-code>
             </div>
    </div>
</el-form-item>

3.使用效果

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您提供一些思路和代码示例。下面是一个简单的获取验证码组件的示例: ```html <template> <div class="verification-code"> <input type="text" v-model="phoneNumber" placeholder="请输入手机号" /> <button :disabled="countdown > 0" @click="getCode">{{ buttonText }}</button> </div> </template> <script> export default { data() { return { phoneNumber: "", countdown: 0, buttonText: "获取验证码", timer: null, }; }, methods: { getCode() { if (!this.phoneNumber) { alert("请输入手机号!"); return; } // 发送验证码接口 // ... // 模拟倒计时 this.countdown = 60; this.buttonText = `${this.countdown}秒后重新获取`; this.timer = setInterval(() => { this.countdown--; if (this.countdown <= 0) { clearInterval(this.timer); this.timer = null; this.buttonText = "获取验证码"; } else { this.buttonText = `${this.countdown}秒后重新获取`; } }, 1000); }, }, beforeDestroy() { if (this.timer) { clearInterval(this.timer); this.timer = null; } }, }; </script> <style scoped> .verification-code { display: flex; flex-direction: row; justify-content: center; align-items: center; } input { width: 80%; height: 40px; font-size: 16px; padding: 5px; margin-right: 10px; border-radius: 4px; } button { width: 20%; height: 40px; background-color: #409eff; border: none; color: #fff; font-size: 16px; border-radius: 4px; cursor: pointer; } button:disabled { background-color: #b5b5b5; cursor: not-allowed; } </style> ``` 这个组件包含一个输入框和一个按钮,当用户点击按钮时,会触发 `getCode` 方法,该方法会先检查用户输入的手机号,然后向后端发送验证码请求,并开启一个模拟的倒计时,防止用户重复获取验证码。在倒计时过程中,按钮会显示剩余时间,倒计时结束后,按钮会重新变为可用状态。同时,为了防止组件销毁时倒计时仍然在进行,我们需要在组件销毁前清除计时器。 以上示例代码基于 Vue.js 和 uni-app 框架,您可以根据您的实际需求进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值