current position:Home>Antd Vue realizes login login page layout - with verification code verification function - Case

Antd Vue realizes login login page layout - with verification code verification function - Case

2022-05-15 04:45:54JackieDYH

effect

Login page

<!--
 * @Author: Jackie
 * @Date: 2022-05-07 14:34:06
 * @LastEditTime: 2022-05-09 15:57:44
 * @LastEditors: Jackie
 * @Description:  The login page   user name - password - Verification code login method 
 * @FilePath: /vue-admin-template/src/views/Login.vue
 * @version: 
-->
<template>
  <div class="Login">
    <h1 class="title">JackieDYH Management platform </h1>
    <a-form-model ref="ruleForm" :model="form" :rules="rules">
      <h5> Sign in </h5>
      <a-form-model-item prop="name">
        <a-input v-model="form.name" placeholder=" Please enter email address / cell-phone number " />
      </a-form-model-item>
      <a-form-model-item prop="password">
        <a-input type="password" v-model="form.password" placeholder=" Please input a password " />
      </a-form-model-item>
      <a-form-model-item prop="code" class="identify">
        <a-input v-model="form.code" placeholder=" Please enter the verification code " />
        <Identify :identifyCode="identifyCode" @click="refreshCode" />
      </a-form-model-item>
      <a-form-model-item>
        <a-button block type="primary" @click="onSubmit">  Submit  </a-button>
        <!-- <router-link
          :to="{ name: 'Password' }"
          style="float: right"
          class="pointer"
        >
           Forget the password 
        </router-link> -->
      </a-form-model-item>
    </a-form-model>
  </div>
</template>

<script>
import Identify from "@/components/Identify";
export default {
  name: "Login",
  components: { Identify },
  data() {
    let validateCode = (rule, value, callback) => {
      if (value === "") {
        callback(new Error(" The verification code is empty "));
        return false;
      } else if (value !== this.identifyCode) {
        callback(new Error(" The verification code is incorrect "));
        return false;
      } else {
        callback();
      }
    };
    return {
      identifyCodes: "1234567890",
      identifyCode: "",
      form: {
        name: "",
        password: "",
        code: "",
      },
      rules: {
        name: { required: true, message: " Please enter email address / cell-phone number ", trigger: "blur" },
        password: { required: true, message: " Please input a password ", trigger: "blur" },
        code: [{ validator: validateCode, trigger: "blur" }],
      },
    };
  },
  mounted() {
    //  initial 
    this.identifyCode = "";
    this.makeCode(this.identifyCodes, 4);
  },
  methods: {
    //  confirm 
    onSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.$router.push("/home");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //  Reset form 
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
    //  Verification code related 
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    //  Click refresh verification code 
    refreshCode() {
      this.identifyCode = "";
      this.makeCode(this.identifyCodes, 4);
    },
    //  Generate verification code 
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode +=
          this.identifyCodes[this.randomNum(0, this.identifyCodes.length)];
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.Login {
  width: 100%;
  height: 100%;
  background-image: url("[email protected]/assets/images/bg_repeat_white.png");
  overflow: auto;
  .title {
    font-size: 40px;
    font-weight: bold;
    padding: 60px 0 60px 40px;
  }
  .ant-form {
    width: 500px;
    border-radius: 5px;
    background: #fff;
    margin: auto;
    padding: 18px 40px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    .ant-dropdown-trigger {
      float: right;
      margin-right: -20px;
    }
    h5 {
      font-size: 26px;
      text-align: center;
      margin-bottom: 25px;
    }
  }
  @media screen and (min-width: 769px) and (max-width: 1000px) {
    .title {
      font-size: 50px;
      text-align: center;
    }
    .ant-form {
      width: 60%;
    }
  }
  @media screen and (max-width: 769px) {
    .title {
      font-size: 50px;
      text-align: center;
    }
    .ant-form {
      width: 90%;
    }
  }
}
</style>
<style lang="scss">
//  Verification code style 
.Login .identify .ant-form-item-children {
  display: flex;
  align-items: center;
  .ant-input {
    flex: 1;
    margin-right: 10px;
  }
}
</style>

 Identify Verification Code

<!--
 * @Author: Jackie
 * @Date: 2022-05-09 14:49:38
 * @LastEditTime: 2022-05-09 14:49:39
 * @LastEditors: Jackie
 * @Description:  Verification Code 
 * @FilePath: /vue-admin-template/src/components/Identify.vue
 * @version: 
-->
<template>
  <canvas
    id="s-canvas"
    :width="contentWidth"
    :height="contentHeight"
    v-on="$listeners"
  ></canvas>
</template>
<script>
export default {
  name: "SIdentify",
  props: {
    identifyCode: {
      type: String,
      default: "1234",
    },
    fontSizeMin: {
      type: Number,
      default: 16,
    },
    fontSizeMax: {
      type: Number,
      default: 40,
    },
    backgroundColorMin: {
      type: Number,
      default: 180,
    },
    backgroundColorMax: {
      type: Number,
      default: 240,
    },
    colorMin: {
      type: Number,
      default: 50,
    },
    colorMax: {
      type: Number,
      default: 160,
    },
    lineColorMin: {
      type: Number,
      default: 40,
    },
    lineColorMax: {
      type: Number,
      default: 180,
    },
    dotColorMin: {
      type: Number,
      default: 0,
    },
    dotColorMax: {
      type: Number,
      default: 255,
    },
    contentWidth: {
      type: Number,
      default: 112,
    },
    contentHeight: {
      type: Number,
      default: 38,
    },
  },
  methods: {
    //  Generate a random number 
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    //  I'm going to generate a random color 
    randomColor(min, max) {
      let r = this.randomNum(min, max);
      let g = this.randomNum(min, max);
      let b = this.randomNum(min, max);
      return "rgb(" + r + "," + g + "," + b + ")";
    },
    drawPic() {
      let canvas = document.getElementById("s-canvas");
      let ctx = canvas.getContext("2d");
      ctx.textBaseline = "bottom";
      //  Draw the background 
      ctx.fillStyle = this.randomColor(
        this.backgroundColorMin,
        this.backgroundColorMax
      );
      ctx.fillRect(0, 0, this.contentWidth, this.contentHeight);
      //  Draw text 
      for (let i = 0; i < this.identifyCode.length; i++) {
        this.drawText(ctx, this.identifyCode[i], i);
      }
      // this.drawLine(ctx)
      this.drawDot(ctx);
    },
    drawText(ctx, txt, i) {
      ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax);
      ctx.font =
        this.randomNum(this.fontSizeMin, this.fontSizeMax) + "px SimHei";
      let x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1));
      let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5);
      var deg = this.randomNum(-45, 45);
      //  Modify the origin and rotation Angle 
      ctx.translate(x, y);
      ctx.rotate((deg * Math.PI) / 180);
      ctx.fillText(txt, 0, 0);
      //  Restore the origin of coordinates and rotation Angle 
      ctx.rotate((-deg * Math.PI) / 180);
      ctx.translate(-x, -y);
    },
    drawLine(ctx) {
      //  Draw interference line 
      for (let i = 0; i < 8; i++) {
        ctx.strokeStyle = this.randomColor(
          this.lineColorMin,
          this.lineColorMax
        );
        ctx.beginPath();
        ctx.moveTo(
          this.randomNum(0, this.contentWidth),
          this.randomNum(0, this.contentHeight)
        );
        ctx.lineTo(
          this.randomNum(0, this.contentWidth),
          this.randomNum(0, this.contentHeight)
        );
        ctx.stroke();
      }
    },
    drawDot(ctx) {
      //  Plot interference point 
      for (let i = 0; i < 100; i++) {
        ctx.fillStyle = this.randomColor(0, 255);
        ctx.beginPath();
        ctx.arc(
          this.randomNum(0, this.contentWidth),
          this.randomNum(0, this.contentHeight),
          1,
          0,
          2 * Math.PI
        );
        ctx.fill();
      }
    },
  },
  watch: {
    identifyCode() {
      this.drawPic();
    },
  },
  mounted() {
    this.drawPic();
  },
};
</script>
<style lang="scss">
canvas {
  cursor: pointer;
}
</style>

copyright notice
author[JackieDYH],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/131/202205111359153278.html

Random recommended