当前位置: 首页 > news >正文

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)=><

相关文章:

  • Java算法之鸡尾酒排序(Cocktail Sort,或称为双向冒泡排序)
  • 进制数基础知识 与 位运算(基础版)
  • 在内核态使用 intel avx2 加速内存操作
  • 力扣2402.会议室 III
  • Spring八股文
  • Windows 下 RabbitMQ 服务无法重装(erlsrv 无法移除和禁用)问题
  • 基于my Batis优化图书管理系统(总)
  • 装饰器模式及应用【理论+代码】
  • 若依 Vue3的前后端分离系统管理 创建
  • 设计模式 16 迭代器模式
  • opencv/c++的一些简单的操作(入门)
  • 大数据处理从零开始————1.Hadoop介绍
  • splunk Enterprise 的HTTP收集器-windows
  • 【PLL】为什么 环路带宽是参考频率的1/10
  • MFC工控项目实例之十添加系统测试对话框
  • 《黑神话·悟空》是用什么编程语言开发的?
  • SQL-函数
  • HTML实现俄罗斯方块
  • 2024/8/30 英语每日一段
  • HTML <template> 标签的基本技巧
  • 山西太原一小区发生爆炸,造成1人遇难21人受伤2人失联
  • 奈雪的茶叫停“能喝奶茶就不要喝水”宣传,当地市监称不要误导消费者
  • “80后”蒋美华任辽宁阜新市副市长
  • 徐徕任上海浦东新区副区长
  • 广东省副省长刘红兵任湖南省委常委、宣传部部长
  • 船只深夜撞上海上风机后沉没1死1失踪,调查报告公布