Vue 邮箱登录界面

news/2024/10/5 10:47:20/

功能

模拟了纯前端的邮箱登录逻辑
还没有连接后端的发送邮件的服务
后续计划,再做一个邮箱、密码登录的界面
然后把这两个一块连接上后端

技术介绍

主要介绍绘制图形人机验证乃个
使用的是canvas,在源码里就有

界面控制主要就是用 表格、表单(其实表单都没怎么用到)、v-if、文本输入框和按钮。

效果图

在这里插入图片描述

在这里插入图片描述

代码

Auth.vue

<template><div><form @submit.prevent="handleSubmit"><table><!-- 邮箱输入框 --><tr v-show="!showEmailVerification"><td><label for="email">邮箱</label></td><td colspan="2"><input type="email" id="email" v-model="formData.email" placeholder="请输入邮箱" required></td></tr><!-- 人机校验 --><tr v-show="!showEmailVerification"><td><button type="button" @click="refreshCaptcha"><canvas ref="myCanvas" width="90" height="25"></canvas></button></td><td><input type="text" id="captcha" v-model="userInputCaptchaText" placeholder="请输入验证码" required></td></tr><tr v-show="!showEmailVerification"><td></td><td><button type="button" @click="sendEmail">人机验证</button></td></tr><!-- 邮箱认证 --><tr v-show="showEmailVerification"><td><label for="emailVerificationCode">验证码</label></td><td colspan="2"><input type="text" id="emailVerificationCode" v-model="formData.emailVerificationCode" placeholder="请输入邮箱验证码" required></td></tr><tr v-show="showEmailVerification"><td></td><td colspan="2"><button type="button" @click="verifyEmailVerificationCode">提交验证码</button></td></tr><!-- 消息通知栏 --><tr><td colspan="2">{{ message }}</td></tr></table></form></div>
</template><script setup>javascript">
import { onMounted } from 'vue';
import useFormValidation from './formValidation';const {formData,userInputCaptchaText,showEmailVerification,message,refreshCaptcha,sendEmail,verifyEmailVerificationCode,
} = useFormValidation();// Initialize captcha on component mount
onMounted(refreshCaptcha);function handleSubmit() {// 可以在这里添加表单提交逻辑
}
</script><style scoped>
table, th, td {border: 1px solid black;border-collapse: collapse;
}
</style>

captcha.js

export function drawCaptcha(ctx, width, height) {// 填充背景色ctx.fillStyle = '#f2f2f2';ctx.fillRect(0, 0, width, height);// 设置字符集const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';const charCount = 4; // 验证码字符数let captchaText = '';// 随机生成验证码文本for (let i = 0; i < charCount; i++) {captchaText += chars.charAt(Math.floor(Math.random() * chars.length));}// 设置字体样式ctx.font = '24px Arial';ctx.fillStyle = '#333';ctx.textBaseline = 'middle';// 绘制字符for (let i = 0; i < captchaText.length; i++) {const x =  i * 24;const y = height / 2;const angle = (Math.random() - 0.5) * 0.6; // 随机旋转角度ctx.save();ctx.translate(x, y);ctx.rotate(angle);ctx.fillText(captchaText.charAt(i), 0, 0);ctx.restore();}// 添加干扰线条for (let i = 0; i < 10; i++) {ctx.strokeStyle = getRandomColor();ctx.beginPath();ctx.moveTo(Math.random() * width, Math.random() * height);ctx.lineTo(Math.random() * width, Math.random() * height);ctx.stroke();}// 添加噪点for (let i = 0; i < 50; i++) {ctx.fillStyle = getRandomColor();ctx.beginPath();ctx.arc(Math.random() * width, Math.random() * height, 1, 0, Math.PI * 2);ctx.fill();}return captchaText;
}function getRandomColor() {const r = Math.floor(Math.random() * 256);const g = Math.floor(Math.random() * 256);const b = Math.floor(Math.random() * 256);return `rgb(${r},${g},${b})`;
}

formValidation.js

import { ref } from 'vue';
import { drawCaptcha } from './captcha';
import axios from 'axios';export default function useFormValidation() {const formData = ref({email: '',emailVerificationCode: ''});const userInputCaptchaText = ref('');let captchaText = '';const isValid = ref(false);const showEmailVerification = ref(false);const loginSuccess = ref(false);const message = ref('');function refreshCaptcha() {const canvas = document.querySelector('canvas');const ctx = canvas.getContext('2d');captchaText = drawCaptcha(ctx, canvas.width, canvas.height);}function sendEmail() {if (!isValidEmail(formData.value.email)) {message.value = '请输入有效的邮箱地址';return;}if (isValidCaptcha(userInputCaptchaText.value, captchaText)) {isValid.value = true;showEmailVerification.value = true;message.value = '请查收邮箱验证码';// 发送邮件验证码的逻辑可以放在这里sendVerificationCode(formData.value.email);} else {message.value = '验证码错误,请重新输入';refreshCaptcha();userInputCaptchaText.value = '';isValid.value = false;showEmailVerification.value = false;}}const sendVerificationCode = async (email) => {try {const response = await axios.post('http://localhost:8080/api/register', { email }, {headers: {'Content-Type': 'application/json'}});console.log('Verification code sent successfully:', response.data);} catch (error) {console.error('Error sending verification code:', error);}};// 校验邮箱验证码function verifyEmailVerificationCode() {if (isValidEmailVerificationCode(formData.value.emailVerificationCode)) {loginSuccess.value = true;message.value = '邮箱验证码校验通过,登录成功!';} else {message.value = '邮箱验证码错误,请重新输入';}}const isValidEmailVerificationCode = async (code) => {console.log(code);try {const response = await axios.post('http://localhost:8080/api/checkEmail', { code }, {headers: {'Content-Type': 'application/json'}});console.log('Verification code check result:', response.data);return response.data;} catch (error) {console.error('Error checking verification code:', error);message.value = '校验邮箱验证码时发生错误';return false;}}return {formData,userInputCaptchaText,isValid,showEmailVerification,loginSuccess,message,refreshCaptcha,sendEmail,verifyEmailVerificationCode};
}function isValidEmail(email) {const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;return emailRegex.test(email);
}function isValidCaptcha(inputCaptcha, generatedCaptcha) {return inputCaptcha.toLowerCase() === generatedCaptcha.toLowerCase();
}

http://www.ppmy.cn/news/1473430.html

相关文章

Java学习 -Golang开发环境+目录结构+编译+部署

开发环境 环境变量设置 GOROOT 指定 golang sdk 的安装目录GOPATH golang 工作目录&#xff0c;项目的源码放在这个目录下PATH 将 GOROOT/bin 放在 Path 路径下&#xff0c;方便命令行能直接运行 golang的命令行工具项目目录结构 |--project // 位于G…

【力扣】快乐数

&#x1f525;博客主页&#xff1a; 我要成为C领域大神&#x1f3a5;系列专栏&#xff1a;【C核心编程】 【计算机网络】 【Linux编程】 【操作系统】 ❤️感谢大家点赞&#x1f44d;收藏⭐评论✍️ 本博客致力于知识分享&#xff0c;与更多的人进行学习交流 编写一个算法来判断…

CY3-Biotin在细胞成像中的应用

星戈瑞单品CY3-Biotin是一种结合了荧光染料CY3和生物素&#xff08;Biotin&#xff09;的荧光探针&#xff0c;它在细胞成像技术中扮演科研角色。以下是CY3-Biotin在细胞成像中的几个主要应用&#xff1a; 蛋白质标记与追踪 CY3-Biotin常被用来标记那些与生物素亲和性高的蛋白…

SQL Server查看所有的数据库、所有的表 以及表的描述

文章目录 -- 查看所有的数据库 select name from sys.databases order by name;-- 查看所有的表 use [你的数据库名]; -- select * from sys.objects order by type; -- select * from sys.objects where type u; -- select object_id,name from sys.objects where type u; s…

Build a Large Language Model (From Scratch)附录B(gpt-4o翻译版)

来源&#xff1a;https://github.com/rasbt/LLMs-from-scratch?tabreadme-ov-file https://www.manning.com/books/build-a-large-language-model-from-scratch

MySQL 8.0 架构 之 中继日志(Relay log)

文章目录 MySQL 8.0 架构 之 中继日志&#xff08;Relay log&#xff09;中继日志&#xff08;Relay log&#xff09;概述相关参数参考 【声明】文章仅供学习交流&#xff0c;观点代表个人&#xff0c;与任何公司无关。 来源|WaltSQL和数据库技术(ID:SQLplusDB) MySQL 8.0 OCP …

详细的讲解一下网络变压器应用POE ,AT BT AF BF的概念,做电路连接指导分析

网络变压器在应用POE&#xff08;Power over Ethernet&#xff09;技术时&#xff0c;承担着重要的角色。它不仅负责数据的传输&#xff0c;同时也为网络设备提供电力。在IEEE 802.3标准中&#xff0c;定义了几个与POE相关的标准&#xff0c;包括802.3af、802.3at、802.3bt等&a…

PLL和CDR的内部结构及其区别

比较PLL和CDR的内部结构及其区别&#xff1a; 基本结构&#xff1a; PLL&#xff08;相位锁定环&#xff09;&#xff1a; 相位检测器环路滤波器压控振荡器&#xff08;VCO&#xff09;分频器&#xff08;可选&#xff0c;用于频率合成&#xff09; CDR&#xff08;时钟数据恢复…