登录验证码

 1.验证码组件封装

<template>
    <div class="s-canvas">
        <canvas id="s-canvas" :width="props.contentWidth" :height="props.contentHeight"></canvas>
    </div>
</template>
 
<script setup>
import { onMounted, watch } from 'vue';
 
const props = defineProps({
    identifyCode: {
        type: String,
        default: '1234'
    },
    fontSizeMin: {
        type: Number,
        default: 35
    },
    fontSizeMax: {
        type: Number,
        default: 35
    },
    backgroundColorMin: {
        type: Number,
        default: 180
    },
    backgroundColorMax: {
        type: Number,
        default: 240
    },
    colorMin: {
        type: Number,
        default: 50
    },
    colorMax: {
        type: Number,
        default: 160
    },
    lineColorMin: {
        type: Number,
        default: 100
    },
    lineColorMax: {
        type: Number,
        default: 200
    },
    dotColorMin: {
        type: Number,
        default: 0
    },
    dotColorMax: {
        type: Number,
        default: 255
    },
    contentWidth: {
        type: Number,
        default: 120
    },
    contentHeight: {
        type: Number,
        default: 40
    }
})
 
// 生成一个随机数
const randomNum = (min, max) => {
    return Math.floor(Math.random() * (max - min) + min)
}
 
// 生成一个随机的颜色
const randomColor = (min, max) => {
    let r = randomNum(min, max)
    let g = randomNum(min, max)
    let b = randomNum(min, max)
    return 'rgb(' + r + ',' + g + ',' + b + ')'
}
 
// 绘制干扰线
const drawLine = (ctx) => {
    for (let i = 0; i < 3; i++) {
        ctx.strokeStyle = randomColor(props.lineColorMin, props.lineColorMax)
        ctx.beginPath()
        ctx.moveTo(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight))
        ctx.lineTo(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight))
        ctx.stroke()
    }
}
 
const drawText = (ctx, txt, i) => {
    ctx.fillStyle = randomColor(props.colorMin, props.colorMax)
    ctx.font = randomNum(props.fontSizeMin, props.fontSizeMax) + 'px SimHei'
    let x = (i + 1) * (props.contentWidth / (props.identifyCode.length + 1))
    let y = randomNum(props.fontSizeMax, props.contentHeight - 5)
    var deg = randomNum(-10, 10)
    // 修改坐标原点和旋转角度
    ctx.translate(x, y)
    ctx.rotate(deg * Math.PI / 100)
    ctx.fillText(txt, 0, 0)
    // 恢复坐标原点和旋转角度
    ctx.rotate(-deg * Math.PI / 100)
    ctx.translate(-x, -y)
}
 
const drawDot = (ctx) => {
    // 绘制干扰点
    for (let i = 0; i < 30; i++) {
        ctx.fillStyle = randomColor(0, 255)
        ctx.beginPath()
        ctx.arc(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight), 1, 0, 2 * Math.PI)
        ctx.fill()
    }
}
 
const drawPic = () => {
    let canvas = document.getElementById('s-canvas')
    let ctx = canvas.getContext('2d')
    ctx.textBaseline = 'bottom'
    // 绘制背景
    ctx.fillStyle = randomColor(props.backgroundColorMin, props.backgroundColorMax)
    ctx.fillRect(0, 0, props.contentWidth, props.contentHeight)
    // 绘制文字
    for (let i = 0; i < props.identifyCode.length; i++) {
        drawText(ctx, props.identifyCode[i], i)
    }
    drawLine(ctx)
    drawDot(ctx)
}
 
watch(() => props.identifyCode, (newValue, oldValue) => {
     drawPic()
})
 
onMounted(() => {
   drawPic()
})
</script>
<style scoped lang='css'>
.s-canvas {
    cursor: pointer;
}
</style>

2.在页面中使用的结构搭建

<template>
  <el-form
    ref="loginFormRef"
    :model="loginModel"
    :rules="loginFormRules"
    label-position="top"
  >
    <el-form-item prop="name" label="用户名">
      <el-input
        v-model="loginModel.name"
        style="height: 43px"
        @keyup.enter="login"
      >
      </el-input>
    </el-form-item>
    <el-form-item prop="password" label="密码">
      <el-input
        v-model="loginModel.password"
        :type="isShowPassword ? '' : 'password'"
        @keyup.enter="login"
      >
        <template #suffix>
          <svg-icon
            v-if="!isShowPassword"
            name="login-eye"
            @click="changeShowPassword"
          />
          <svg-icon v-else name="login-close-eye" @click="changeShowPassword" />
        </template>
      </el-input>
    </el-form-item>
    <el-form-item label="验证码" prop="validate" class="validatebox">
      <el-input
        class="aric-yanzheng"
        type="text"
        v-model="loginModel.validate"
        @keyup.enter="login"
      ></el-input>
      <div class="login-code" @click="refreshCode">
        <SIdentify
          :identifyCode="identifyCode"
          :contentHeight="38"
          :contentWidth="100"
        />
      </div>
    </el-form-item>
    <el-form-item prop="remember">
      <!-- <el-checkbox label="保持登陆"  v-model="loginModel.remember" /> -->
      <!-- <el-switch v-model="loginModel.remember" /> -->
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="login"> 登录 </el-button>
    </el-form-item>
  </el-form>
</template>

 3.在页面中使用的js代码

<script setup>
import { reactive, ref, onMounted } from "vue";
import SIdentify from "@/components/Sidentify.vue";
import { ElLoading, ElMessage, ElMessageBox } from "element-plus";
// import md5 from "js-md5";
// import { sha256 } from "js-sha256";
onMounted(() => {
  makeCode(identifyCodes, 4);
});
const loginFormRef = ref(null);
// 基础数据
let identifyCode = ref("");
// 图形验证码
let identifyCodes = "1234567890";

const randomNum = (min, max) => {
  return Math.floor(Math.random() * (max - min) + min);
};
// 从 code 中 随机生成 num 位验证码
const makeCode = (code, num) => {
  for (let i = 0; i < num; i++) {
    identifyCode.value += code[randomNum(0, code.length)];
  }
};
// 点击更换验证码
const refreshCode = () => {
  identifyCode.value = "";
  makeCode(identifyCodes, 4);
};
// 自定义验证码校验规则
const validateidCode = (rule, value, callback) => {
  if (!value) {
    callback(new Error("验证码不能为空"));
  } else if (value != identifyCode.value) {
    callback(new Error("验证码错误"));
  } else {
    callback();
  }
};
// 表单绑定的数据
const loginModel = reactive({
  name: "", //'test02',
  password: "", //'eqmpwd123.'
  validate: "",
});
const loginFormRules = reactive({
  name: [
    {
      required: true,
      trigger: "change",
      message: "请填写登录账户",
    },
  ],
  password: [
    {
      required: true,
      trigger: "change",
      message: "请填写密码",
    },
  ],
  validate: [
    {
      required: true,
      trigger: "change",
      validator: validateidCode,
    },
  ],
});

const login = () => {
  // 表单验证
  loginFormRef.value.validate((valid) => {
    if (valid) {
      console.log(loginModel);
      // loginModel.password = md5(sha256(md5(loginModel.password)));
    }
  });
};
// 是否显示明文密码
const isShowPassword = ref(false);
// 切换密码明暗文显示
const changeShowPassword = () => {
  isShowPassword.value = !isShowPassword.value;
};
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值