首页 > 其他分享 >验证码组件开发,本地缓存倒计时

验证码组件开发,本地缓存倒计时

时间:2023-12-18 17:46:50浏览次数:27  
标签:el 缓存 index 验证码 else 倒计时 key error input

vue组件

<template>
    <div class="verifyCode mt-24">
        <div class="header">
            <span class="logo inline-block mr-6 w-[26px] h-[26px] bg-black-0"></span>
            <span class="title text-[22px] font-semibold"> {{ title }}</span>
        </div>
        <div class="desc text-sm mt-6">
            {{ desc }}
        </div>
        <div class="main flex justify-between items-center">
            <div class="number">
                <div class="input-box flex-1">
                    <div class="input-content" @keydown="keydown" @keyup="keyup" @paste="paste" @mousewheel="mousewheel"
                        @input="inputEvent">
                        <input max="9" min="0" maxlength="1" data-index="0" v-model.trim.number="input[0]" type="number"
                            :class="isError || isExpired ? 'error' : 'default'" />
                        <input max="9" min="0" maxlength="1" data-index="1" v-model.trim.number="input[1]" type="number"
                            :class="isError || isExpired ? 'error' : 'default'" />
                        <input max="9" min="0" maxlength="1" data-index="2" v-model.trim.number="input[2]" type="number"
                            :class="isError || isExpired ? 'error' : 'default'" />
                        <input max="9" min="0" maxlength="1" data-index="3" v-model.trim.number="input[3]" type="number"
                            :class="isError || isExpired ? 'error' : 'default'" />
                        <input max="9" min="0" maxlength="1" data-index="4" v-model.trim.number="input[4]" type="number"
                            :class="isError || isExpired ? 'error' : 'default'" />
                        <input max="9" min="0" maxlength="1" data-index="5" v-model.trim.number="input[5]" type="number"
                            :class="isError || isExpired ? 'error' : 'default'" />
                    </div>
                </div>
            </div>
            <div v-show="isCodeShow">
                <div v-show="sendMsg.length >= 7" @click="handleReSend"
                    class="resend mt-4 w-[96px] h-9 cursor-pointer leading-9 text-center border-2 border-[#000]  text-sm font-semibold rounded-[16px]">
                    Get code
                </div>
                <div v-show="parseInt(sendMsg) >= 1"
                    class="resend mt-4 w-[96px] h-9  leading-9 text-center   text-sm font-semibold ">
                    {{ sendMsg }}s
                </div>
            </div>
        </div>
        <div class="mailCode mt-3 text-[13px] font-medium text-[#868D9A]">
            {{ mark }}
        </div>
        <!-- 错误 -->
        <div v-show="isError" class="errCode mt-3 text-[13px] font-medium text-[#F36343]">
            Verification code error, please try again
        </div>
        <!-- 过期 -->
        <div v-show="isExpired" class="errCode mt-3 text-[13px] font-medium text-[#F36343]">
            The verification code has expired, please obtain it again
        </div>
        <!-- 忘记密码 -->
        <div v-show="isResetPassword" class="text-[#868D9A] text-[13px] mt-3 cursor-pointer">
            Forget number
            <svg xmlns="http://www.w3.org/2000/svg" width="6" height="10" viewBox="0 0 6 10" fill="none">
                <rect width="6.01783" height="1.3373" rx="0.668648"
                    transform="matrix(-0.707108 0.707106 -0.707108 -0.707106 6 5.04492)" fill="#A8AAAE" />
                <rect width="6.01783" height="1.3373" rx="0.668648"
                    transform="matrix(-0.707108 -0.707106 0.707108 -0.707106 5.05469 5.83496)" fill="#A8AAAE" />
            </svg>
        </div>
    </div>
</template>

<script>
let TIME_COUNT = 60; // 设置一个全局的倒计时的时间
export default {
    props: {
        "isCodeShow": {
            type: Boolean,
            default: () => {
                return false;
            }
        },
        "isResetPassword": {
            type: Boolean,
            default: () => {
                return false;
            }
        },
        "title": {
            type: String,
            default: () => {
                return 'defalut title';
            }
        },
        "desc": {
            type: String,
            default: () => {
                return 'defalut desc';
            }
        },
        "mark": {
            type: String,
            default: () => {
                return '';
            }
        }
    },
    data() {
        return {
            input: [],
            sendMsg: "",
            timer: null,
            isError: false,
            isExpired: false,
        }
    },
    created() {
        this.input = new Array(6).fill('');
        if(this.getLocalStorage('TIME_COUNT')){
            this.handleReSend()
        } else {
            this.sendMsg = 'Get code'
        }
    },
    methods: {
        // 设置缓存
        setLocalStorage(key, value) {
            try {
                window.localStorage.setItem(key, JSON.stringify(value));
            } catch (error) {
                // 处理缓存设置失败的情况
                console.error('设置缓存失败:', error);
            }
        },
        // 获取缓存
        getLocalStorage(key) {
            try {
                const value = window.localStorage.getItem(key);
                return value ? JSON.parse(value) : null;
            } catch (error) {
                // 处理缓存获取失败的情况
                console.error('获取缓存失败:', error);
                return null;
            }
        },
        removeLocalStorage(key) {
            try {
                window.localStorage.removeItem(key);
            } catch (error) {
                // 处理缓存删除失败的情况
                console.error('删除缓存失败:', error);
            }
        },
        // 解决一个输入框输入多个字符
        inputEvent(e) {
            var index = e.target.dataset.index * 1
            var el = e.target
            el.value = el.value
                .replace(/Digit|Numpad/i, '')
                .replace(/1/g, '')
                .slice(0, 1)
            this.input[index] = el.value
        },
        keydown(e) {
            var index = e.target.dataset.index * 1
            var el = e.target
            if (e.key === 'Backspace') {
                if (this.input[index] && this.input[index].length > 0) {
                    this.input[index] = ''
                } else {
                    if (el.previousElementSibling) {
                        el.previousElementSibling.focus()
                        this.input[index - 1] = ''
                    }
                }
            } else if (e.key === 'Delete') {
                if (this.input[index].length > 0) {
                    this.input[index] = ''
                } else {
                    if (el.nextElementSibling) {
                        this.input[(index = 1)] = ''
                    }
                }
                if (el.nextElementSibling) {
                    el.nextElementSibling.focus()
                }
            } else if (e.key === 'Home') {
                el.parentElement.children[0] && el.parentElement.children[0].focus()
            } else if (e.key === 'End') {
                el.parentElement.children[this.input.length - 1] &&
                    el.parentElement.children[this.input.length - 1].focus()
            } else if (e.key === 'ArrowLeft') {
                if (el.previousElementSibling) {
                    el.previousElementSibling.focus()
                }
            } else if (e.key === 'ArrowRight') {
                if (el.nextElementSibling) {
                    el.nextElementSibling.focus()
                }
            } else if (e.key === 'ArrowUp') {
                if (this.input[index] * 1 < 9) {
                    this.input[index] = (this.input[index] * 1 + 1).toString()
                }
            } else if (e.key === 'ArrowDown') {
                if (this.input[index] * 1 > 0) {
                    this.input[index] = (this.input[index] * 1 - 1).toString()
                }
            }
        },
        keyup(e) {
            var index = e.target.dataset.index * 1
            var el = e.target
            // 解决输入e的问题
            el.value = el.value
                .replace(/Digit|Numpad/i, '')
                .replace(/1/g, '')
                .slice(0, 1)
            if (/Digit|Numpad/i.test(e.code)) {
                // 必须在这里符直,否则输入框会是空值
                this.input[index] = e.code.replace(/Digit|Numpad/i, '')
                el.nextElementSibling && el.nextElementSibling.focus()
                if (index === 5) {
                    if (this.input.join('').length === 6) {
                        document.activeElement.blur()
                    }
                }
            } else {
                if (this.input[index] === '') {
                    this.input[index] = ''
                }
            }
            this.$emit('complete', this.input.slice());
        },
        mousewheel(e) {
            var index = e.target.dataset.index
            if (e.wheelDelta > 0) {
                if (this.input[index] * 1 < 9) {
                    this.input[index] = (this.input[index] * 1 + 1).toString()
                }
            } else if (e.wheelDelta < 0) {
                if (this.input[index] * 1 > 0) {
                    this.input[index] = (this.input[index] * 1 - 1).toString()
                }
            } else if (e.key === 'Enter') {
                if (this.input.join('').length === 6) {
                    document.activeElement.blur()
                    this.$emit('complete', this.input.slice())
                }
            }
        },
        paste(e) {
            // 当进行粘贴时
            e.clipboardData.items[0].getAsString((str) => {
                if (str.toString().length === 6) {
                    document.activeElement.blur();
                    this.input = str.split('');
                    this.$emit('complete', this.input.slice())
                } else {
                    // 如果粘贴内容不合规,清除所有内容
                    this.input[0] = new Array(6)
                }
            })
        },
        handleReSend() {
            // 模拟生成code
            this.sendMsg = this.getLocalStorage('TIME_COUNT') || TIME_COUNT;
            this.timer = setInterval(() => {
                if (this.sendMsg <= 1) {
                    clearInterval(this.timer)
                    this.removeLocalStorage('TIME_COUNT')
                    this.sendMsg = "Get code"
                    return
                }
                this.sendMsg -= 1;
                this.setLocalStorage('TIME_COUNT',this.sendMsg)
            }, 1000)
        },
        getCode() {
            let resultArray = this.input.filter(item => {
                return item != '' && item >= 0
            })
            if (resultArray.length == 6) {
                let originStr = resultArray.toString().replace(/,/g, '')
                return originStr;
            } else {
                this.$message.error('请检查输入的数据!')
            }

        }
    }
}
</script>
<style>
.input-box .input-content {
    margin-top: 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.input-box .input-content input {
    color: inherit;
    font-family: inherit;
    border: 0;
    outline: 0;
    height: 42px;
    width: 42px;
    line-height: 42px;
    border-radius: 12px;
    font-size: 22px;
    text-align: center;
    margin-right: 14px;
}

.error {
    color: #F36343 !important;
    background: #f7e6e5 !important;
}

.default {
    color: #000;
    background: #F2F4F5;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    appearance: none;
    margin: 0;
}
</style>

父组件使用

 <ValidCode ref="ValidCodeMail" v-bind="ValidCodeMailProps" />

data(){
          ValidCodeMailProps: {
                isCodeShow: true,
                title: "mailbox verification",
                desc: "Please enter your email verification code",
                mark: "[email protected]",
            },
}

// 获取code
const ValidCodeGoogleCode = this.$refs.ValidCodeGoogle.getCode();

标签:el,缓存,index,验证码,else,倒计时,key,error,input
From: https://www.cnblogs.com/brujie/p/17911779.html

相关文章

  • Spring Boot学习随笔- JSP小项目-员工管理系统(验证码生成、增删改查)
    学习视频:【编程不良人】2021年SpringBoot最新最全教程第十章、项目开发实现一个登录注册,增删改查功能的系统10.1项目开发流程需求分析分析用户主要需求提取项目核心功能,根据核心功能构建页面原型库表设计:分析系统有哪些表分析表之间关联关系确定字段详细设......
  • 【UniApp】-uni-app-数据缓存
    前言好,经过上个章节的介绍完毕之后,给大家补充了一下uni-app-数据传递的内容那么补充了uni-app-数据传递的内容之后,这篇文章来给大家介绍一下uni-app-数据缓存搭建项目首先我们还是要先搭建一个项目,这里我就不多说了,大家可以参考上一篇文章搭建好项目之后,我们就可以开......
  • 14.adb 命令清缓存
    adbshell应用1查看目录结构:adbshellls查看系统当前日期:adbshelldate查看系统CPU使用情况:adbshellcat/proc/cpuinfo查看系统内存使用情况:adbshellcat/proc/meminfoadbshell应用2-查看应用列表显示所有应用:adbshellpmlistpackages显示系统自带应......
  • 记录--js小练习(弹幕、 电梯导航、 倒计时、 随机点名、 购物放大镜)
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助DOM小练习弹幕电梯导航倒计时随机点名购物放大镜1.弹幕效果预览功能:输入弹幕内容,按下回车显示一条弹幕(弹幕颜色、字体随机生成)思路:设置按钮抬起事件,在事件中判断如果按下的是回车键则将输入框中替换掉......
  • SpringBoot中项目启动及定时任务缓存数据库常用数据至内存变量并转换后高频调用
    场景定时任务中需要获取数据库中数据进行数据转换成需要的格式并进行后续的业务处理。数据库中的数据更新频率不高。可将数据库中数据在项目启动后读取一遍数据,然后再通过定时任务定时查询数据库更新数据。实现数据库缓存的方式有多种,比如以下:SpringBoot中通过自定义缓存注解......
  • Asp.net core Net6.0 Webapi 项目如何优雅地使用内存缓存
    前言缓存是提升程序性能必不可少的方法,Asp.netcore支持多级缓存配置,主要有客户端缓存、服务器端缓存,内存缓存和分布式缓存等。其中客户端缓和服务器端缓存在使用上都有比较大的限制,而内存缓和分布式缓存则比较灵活。内存缓存就是一种把缓存数据放到应用程序内存中的机制。本......
  • web网站有验证码,appscan如何配置才能成功扫描
    1、点击完全扫描配置 2.登录方法选择‘无’  3、添加token  4、进行手动探测,并排除登录页面手动探测,从登录后开始 ......
  • springboot+vue小白升级之路14-实现系统公告首页公告展示、springboot+vue小白升级之
    还是接着之前的内容,我把新增的功能代码贴出来,给大家参考学习。数据库droptableifexistsan_user;createtablean_user( idintnotnullauto_incrementprimarykeycomment'主键id', namevarchar(255)notnulluniquecomment'姓名', `password`varchar(255)notnu......
  • 快速入门:使用 .NET Aspire 组件实现缓存
    前言云原生应用程序通常需要各种类型的可扩展缓存解决方案来提高性能。.NETAspire组件简化了连接到流行的缓存服务(例如Redis)的过程,今天小编就为大家简单介绍一下如何使用.NETAspire组件实现缓存。本文的内容概要:创建一个设置为使用.NETAspire的基本ASP.NETCore应......
  • Redis缓存问题分析与解决方案
    在分布式系统中,Redis作为一种高效的缓存解决方案,但在面对大规模并发、高负载情境下,可能出现雪崩、击穿和穿透等问题,需要我们采取相应的解决方案。1.Redis雪崩问题描述:Redis雪崩是指缓存中大量的键在同一时刻过期,导致大量请求直接落到数据库上,引发数据库压力骤增。解决方案:随机设......