vue实现弹窗输入验证码

实现思路:前端输入完账号和密码,点击登录按钮的时候,弹出一个输入验证码的窗口,后端把验证码图片通过base64的字符传给前端,前端把字符当成图片展示出来。输入完验证码,点击确认进行登录,把验证码传给后端,后端判断验证码是否准确。

前端获取验证码的接口

/component/GraphCodeDialog /index.vue 封装成组件

复制代码
<template>
    <el-dialog v-model="dialogVisible" title="获取验证码" width="500">
        <div class="row">
            <img :src="codeImg" style="height: 150px;width:380px" @click="getCodeImge()" />
        </div>
        <div class="row">
            <el-input type="text" placeholder="请输入验证码" v-model="captcode" @keyup.enter="handleAffirm" style="width: 300px;"
                maxlength="6">
            </el-input>
            <el-button type="primary" @click="handleAffirm">确认</el-button>
        </div>
    </el-dialog>
</template>
<script lang='ts'>
import { reactive, getCurrentInstance, toRefs, onMounted, nextTick, defineComponent } from 'vue';
export default defineComponent({
    name: '',
    props: {},
    setup(props: any, { emit }: any) {
        onMounted(() => {
            nextTick(() => { });
        });
        const { proxy } = getCurrentInstance() as any;
        const data = reactive({
            dialogVisible: false,
            codeImg: "",
            captcode: "",
        });

        const handleAffirm = () => {
            if (!data.captcode) return proxy.$message.warning("请输入验证码")
            data.dialogVisible = false
            emit("handleAffirm", data.captcode)
            data.captcode = ''
        }

        const getCodeImge = async () => {
            let res = await proxy.apis.loginCaptchacode()
            if (res.code == 200) {
                data.codeImg = res.data
                data.dialogVisible = true
            } else {
                proxy.$message.error(res.message)
            }
        }

        return { ...toRefs(data), handleAffirm, getCodeImge }
    }
})
</script>
<style scoped lang='scss'>
.row {
    text-align: center;
    margin-bottom: 20px;
}
</style>

页面文件

template代码

复制代码
<!-- 获取图片验证码 -->
<GraphCodeDialog ref="codeImgRef" @handleAffirm="getCodeAffirm"></GraphCodeDialog>

script代码

复制代码
<script setup lang="ts" name="loginIndex">
// 引入组件
const GraphCodeDialog = defineAsyncComponent(() => import('/@/component/GraphCodeDialog /index.vue'));


const getCodeAffirm = (captcode: string) => {
	console.log("验证码=",captcode);
}
</script>

效果图

相关推荐
豆苗学前端3 分钟前
彻底讲透浏览器渲染原理,吊打面试官
前端·javascript·面试
踩着两条虫11 分钟前
AI 驱动的 Vue3 应用开发平台 入门指南(五):创建 H5 移动应用
前端·vue.js·ai编程
ZengLiangYi12 分钟前
用 AudioContext.suspend()/resume() 作为流式音视频的同步门控
前端·音视频开发
进击的尘埃13 分钟前
可视化搭建引擎的撤销重做系统:Command 模式 + Immutable 快照实现操作历史树
javascript
踩着两条虫13 分钟前
AI 驱动的 Vue3 应用开发平台 入门指南(二):快速入门
前端·vue.js·ai编程
程序员阿耶15 分钟前
CSS滚动条样式从入门到实战:打造跨浏览器的自定义滚动条
前端
范小饭1 小时前
哼,要变天了:副业赚18块的自救实录
前端
天蓝色的鱼鱼2 小时前
从“死了么”到“我在”:用uniCloud开发一款温暖人心的App
前端·uni-app
小徐_23332 小时前
uni-app 组件库 Wot UI 的 AI 友好型编程指南
前端·uni-app
HelloReader2 小时前
Flutter Widget 基础手把手教你创建自定义组件(二)
前端