nuxt3模拟手机验证码

2024-09-01 02:44

本文主要是介绍nuxt3模拟手机验证码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

      • 前言
      • 前端
      • 后端
      • 前面代码会出现的问题
      • 约束button的小插件
      • 连接mongodb来写
      • 登陆
      • 项目开源链接

前言

真实应该要连接短信验证码服务,但是众所周知所有的服务和IT都是靠服务来挣钱的,所以我们目前只能模拟手机验证码登陆

考虑到账号的唯一值就想到了手机和验证码(基本上的网站都会使用)但是输入手机号后有"获取验证码"的操作,这个操作就是需要供应商支持(交钱)但是身无分文的我又没有像别人一样有收入来源所以我这里模拟的手机注册登陆的效果共大家做参考。

里面还有nuxt使用qrcode的方法,大家可以看看

前端

需要安装

yarn add nuxt-mongodb
yarn add --dev @types/mongodb
#生成二维码的依赖
yarn add qcode
<!--app.vue-->
<script setup lang="ts">
import Qrcode from 'qrcode.vue';
import ysn_kt from "./public/images/ysn_kt.png"
import type { Reactive } from 'vue';const qrcodeRef = ref(null)interface phoneInter {phone:string,//手机号can:string,//验证码
}const phoneReactive:Reactive<phoneInter> = reactive({phone:"",can:""
})//是否显示登陆页
const isLoginShow = ref(false)
//扫码配置
const qrcodeValue = ref("hello world") //扫码后里面的信息
const qrcodeSize = ref(200) //二维码的大小
const qrcodeColor = ref("#09fff7") //二维码的颜色
const qrcodeBack = ref("#ffffff")//二维码的背景颜色
const logoMargin = 5 // Logo 边距
const logoScale = 0.2 // Logo 缩放比例//打开登陆
function goToLogin(){isLoginShow.value = true// new Qrcode('')
}
//关闭登陆
function closeLogin(){isLoginShow.value = false
}//发送验证码
async function postSubCan(){fetch('/api/getPhone',{method: 'POST',body: JSON.stringify(phoneReactive),headers:{'Content-Type': 'application/json'}}).then(async res =>{return res.json()}).then(async(data:any) => {console.log(data)//弹出验证码alert(`你的验证码是:${data.message.can}`)}).catch(err=>console.log(err))
}//注册用户
function postRegisterUser(){fetch('/api/registerUser',{method:'POST',body:JSON.stringify(phoneReactive),headers:{'Context-Type':"application/json"}}).then(async res =>{return res.json()}).then(async(data:any) => {console.log(data)}).catch(err=>console.log(err))
}
</script><template><!-- 登陆页面 --><div ref="loginRef" v-if="isLoginShow" class="loginClass"><div class="masterBox"><div @click="closeLogin()" class="closeBtn">关闭</div><div class="scanNameTitle">扫码登陆</div><div class="qrcodeClass"><qrcode :value="qrcodeValue" :foreground="qrcodeColor":margin="1":logo-src="ysn_kt" :logo-scale="logoScale":logo-margin="logoMargin":size="qrcodeSize" class="qrcodeMaster" ref="qrcodeRef"/><div class="phoneRegNameTitle">手机注册</div><div class="phoneRegMaster"><input type="text" v-model="phoneReactive.phone" placeholder="手机号" /><input type="text" v-model="phoneReactive.can" placeholder="验证码" /><button @click="postSubCan">发送验证码</button><br /><button @click="postRegisterUser">注册</button></div></div></div></div><!-- 主页 --><div class=""><nuxt-link to="/" class="">首页</nuxt-link><nuxt-link to="/">测试</nuxt-link><span @click="goToLogin()" class="loginBtn">登录</span></div><div><nuxt-page></nuxt-page></div>
</template><style scoped>
/*样式,可以自定义*/
.loginClass{position: absolute;left: 0;top: 0;width: calc(100%);height: calc(100%);background: rgba(0, 0, 0, .3);z-index: 999;display: flex;align-items: center;justify-content: center;
}
.loginBtn{user-select: none;cursor: pointer;
}
.masterBox{width: 70%;height: 70%;background: rgba(255, 255, 255, 1);border-radius: 10px;
}
.closeBtn{position: absolute;right: calc(20%);top: calc(20%);user-select: none;cursor: pointer;
}
.qrcodeClass{position: relative;left: 100px;top: 200px;width: 200px;height: 200px;/* background: rgba(0, 255, 200, 1); */
}
.qrcodeMaster{border: 2px solid #09fff7; /* 添加边框 */padding: 10px; /* 添加内边距 */border-radius: 15px;
}
.scanNameTitle{position: absolute;left: 26%;top: 30%;font-size: calc(20px);
}
/* 注册样式 */
.phoneRegNameTitle{position: absolute;left: 190%;top: -40%;width: 40%;font-size: 20px;
}
.phoneRegMaster{position: absolute;left: 150%;top: -5%;width: 150%;height: 100%;/* background: rgba(255, 100, 100, 1); */display: flex;align-items: center;justify-content: center;
}
</style>

后端

/server/api/getPhone.ts

// server/api/getPhone.ts (模拟获取验证码服务)
//接口
interface phoneInter {phone:string,can:string,//验证码
}//随机浮点数
function getRandomFloatInRange(min: number, max: number): number {return Math.random() * (max - min) + min;
}
//随机四位整数
function getRandomFourDigitInt(): number {return Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000;
}//导出can信息
export let canMes:number = 0//只有注册账号才能使用(模拟手机短信发送(因为真正的短信发送要钱))
export default defineEventHandler(async(event:any)=><

这篇关于nuxt3模拟手机验证码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1125717

相关文章

springboot security验证码的登录实例

《springbootsecurity验证码的登录实例》:本文主要介绍springbootsecurity验证码的登录实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录前言代码示例引入依赖定义验证码生成器定义获取验证码及认证接口测试获取验证码登录总结前言在spring

Python实现自动化接收与处理手机验证码

《Python实现自动化接收与处理手机验证码》在移动互联网时代,短信验证码已成为身份验证、账号注册等环节的重要安全手段,本文将介绍如何利用Python实现验证码的自动接收,识别与转发,需要的可以参考下... 目录引言一、准备工作1.1 硬件与软件需求1.2 环境配置二、核心功能实现2.1 短信监听与获取2.

Flask 验证码自动生成的实现示例

《Flask验证码自动生成的实现示例》本文主要介绍了Flask验证码自动生成的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习... 目录生成图片以及结果处理验证码蓝图html页面展示想必验证码大家都有所了解,但是可以自己定义图片验证码

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

Python自动化处理手机验证码

《Python自动化处理手机验证码》手机验证码是一种常见的身份验证手段,广泛应用于用户注册、登录、交易确认等场景,下面我们来看看如何使用Python自动化处理手机验证码吧... 目录一、获取手机验证码1.1 通过短信接收验证码1.2 使用第三方短信接收服务1.3 使用ADB读取手机短信1.4 通过API获取

Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)

《Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)》本文介绍了如何使用Python和Selenium结合ddddocr库实现图片验证码的识别和点击功能,感兴趣的朋友一起看... 目录1.获取图片2.目标识别3.背景坐标识别3.1 ddddocr3.2 打码平台4.坐标点击5.图

SpringBoot整合kaptcha验证码过程(复制粘贴即可用)

《SpringBoot整合kaptcha验证码过程(复制粘贴即可用)》本文介绍了如何在SpringBoot项目中整合Kaptcha验证码实现,通过配置和编写相应的Controller、工具类以及前端页... 目录SpringBoot整合kaptcha验证码程序目录参考有两种方式在springboot中使用k

SpringBoot如何集成Kaptcha验证码

《SpringBoot如何集成Kaptcha验证码》本文介绍了如何在Java开发中使用Kaptcha生成验证码的功能,包括在pom.xml中配置依赖、在系统公共配置类中添加配置、在控制器中添加生成验证... 目录SpringBoot集成Kaptcha验证码简介实现步骤1. 在 pom.XML 配置文件中2.

使用 Python 和 LabelMe 实现图片验证码的自动标注功能

《使用Python和LabelMe实现图片验证码的自动标注功能》文章介绍了如何使用Python和LabelMe自动标注图片验证码,主要步骤包括图像预处理、OCR识别和生成标注文件,通过结合Pa... 目录使用 python 和 LabelMe 实现图片验证码的自动标注环境准备必备工具安装依赖实现自动标注核心

Spring 验证码(kaptcha)

首先引入需要的jar包: <dependency><groupId>com.github.axet</groupId><artifactId>kaptcha</artifactId><version>0.0.9</version></dependency> 配置验证码相关设置: <bean id="captchaProducer" class="com.