vue实现滑块验证,文字点选验证 兼容pc,h5

758 阅读1分钟

1、文字点选验证

<template>
  <div class="point">
    <div class="point-move">
      <span class="point-move-span"
        ><span style="color: #f56a00">依次</span>点击:</span
      >
      <!--      需点击的文字图片-->
      <img :src="imgSrc" class="tip-img" />
      <!--      刷新-->
      <div class="point-bottom" @click="refresh">
        <van-icon name="replay" size="20" />
      </div>
    </div>
    <!--    点击区域 背景图-->
    <div class="point-content" ref="content" @click="handleClick">
      <div class="point-bg-img-div" ref="pointBgImg">
        <img :src="pointBgImg" alt />
      </div>
      <!--      点击次数及顺序-->
      <div class="bg-click-div" v-html="html"></div>
    </div>
  </div>
</template>

<script>
import { reqCheck, reqGetPicture } from "@/service/api";
import { formatDate } from "@/utils/common";

export default {
  data() {
    return {
      start: 0,
      startY: 0,
      startSlidingTime: "",
      entSlidingTime: "",
      trackArr: [], // 坐标信息
      clickCount: 0, // 已点击次数
      imgSrc: "", // 需点击的文字图片
      pointBgImg: "", // 背景图片
      html: "", // 生成已点击的次数顺序
      type: "" // 验证类型
    };
  },
  mounted() {
    // 获取验证信息
    this.refreshCaptcha();
  },
  methods: {
    // 开始点击
    move(event) {
      if (event instanceof TouchEvent) {
        event = event.touches[0];
      }
      // 存储坐标信息
      this.trackArr.push({
        x: event.offsetX,
        y: event.offsetY,
        t: 2,
        type: "move"
      });
    },
    // 验证
    valid() {
      let captchaTrack = {
        bgImageWidth: this.$refs.pointBgImg.offsetWidth,
        bgImageHeight: this.$refs.content.offsetHeight,
        sliderImageWidth: -1, // 后端接收 但文字点选没有 所以传-1
        sliderImageHeight: -1,
        startSlidingTime: formatDate(this.startSlidingTime),
        entSlidingTime: formatDate(this.entSlidingTime),
        trackList: this.trackArr
      };
      reqCheck({
        captchaTrack,
        type: this.type,
        id: this.id
      }).then(res => {
        this.refreshCaptcha();
        if (res.data.code == "00") {
          this.$toast({
            message: "验证成功!"
          });
          this.$emit("success");
          this.reset();
        }
      });
    },
    // 重置
    reset() {
      this.clickCount = 0;
      this.html = "";
      this.start = 0;
      this.startSlidingTime = null;
      this.entSlidingTime = null;
      this.trackArr.length = 0;
      this.clickCount = 0;
      this.startY = 0;
      window.removeEventListener("mousemove", this.move);
    },
    // 刷新
    refresh() {
      this.$emit("success");
    },
    // 获取验证信息
    refreshCaptcha() {
      this.reset();
      reqGetPicture({
        type: "WORD_IMAGE_CLICK"
      }).then(res => {
        this.doMoveX = 0;
        this.position = 0;
        if (res.data.code == "00") {
          this.pointBgImg = res.data.result.captcha.backgroundImage;
          this.imgSrc = res.data.result.captcha.sliderImage;
          this.id = res.data.result.id;
          this.type = res.data.result.captcha.data;
        }
      });
    },
    // 开始点击
    handleClick(event) {
      // 点击次数递增
      this.clickCount++;
      // 如果是第一次点击 存储开始时间 坐标信息
      if (this.clickCount === 1) {
        this.startSlidingTime = new Date();
        window.addEventListener("mousemove", this.move);
      }
      this.trackArr.push({
        x: event.offsetX,
        y: event.offsetY,
        type: "click",
        t: 2
      });
      // 点击的位置(相对于背景图)
      let left = event.offsetX - 5;
      let top = event.offsetY - 5;
      // 每次点击在当前点击的位置显示次数
      this.html +=
        "<span class='click-span' style='left:" +
        left +
        "px;top: " +
        top +
        "px'>" +
        this.clickCount +
        "</span>";
      // 如果点击4次 存储结束时间 坐标信息
      if (this.clickCount === 4) {
        this.entSlidingTime = new Date();
        window.removeEventListener("mousemove", this.move);
        this.valid();
      }
    }
  }
};
</script>
<style lang="scss">
.point {
  position: relative;
  user-select: none;
  background-color: #fff;
  width: 570px;
  height: 430px;
  z-index: 999;
  box-sizing: border-box;
  padding: 18px;
  border-radius: 12px;
  box-shadow: 0 0 22px 0 #999999;
  margin: 50px auto;
  .point-content {
    width: 100%;
    height: 310px;
    position: relative;
    .point-bg-img-div {
      width: 100%;
      position: absolute;
      transform: translate(0px, 0px);
      z-index: 0;
      img {
        width: 100%;
      }
    }
    .bg-click-div {
      pointer-events: none;
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      .click-span {
        position: absolute;
        left: 0;
        top: 0;
        border-radius: 50px;
        background-color: #409eff;
        width: 20px;
        height: 20px;
        text-align: center;
        line-height: 20px;
        font-size: 20px;
        color: #fff;
        border: 2px solid #fff;
      }
    }
  }
  .point-move {
    height: 70px;
    width: 100%;
    position: relative;
    display: flex;
    .point-move-span {
      font-size: 26px;
      display: inline-block;
      line-height: 70px;
      margin-left: 10px;
    }
    .tip-img {
      width: 200px;
      position: absolute;
      right: 60px;
      bottom: 10px;
    }
  }
  .point-bottom {
    position: absolute;
    bottom: 10px;
    right: 0;
    z-index: 2;
  }
}
</style>

2.滑块验证

<template>
  <div class="point">
    <div class="point-move">
      <span class="point-move-span"
        ><span style="color: #f56a00">依次</span>点击:</span
      >
      <!--      需点击的文字图片-->
      <img :src="imgSrc" class="tip-img" />
      <!--      刷新-->
      <div class="point-bottom" @click="refresh">
        <van-icon name="replay" size="20" />
      </div>
    </div>
    <!--    点击区域 背景图-->
    <div class="point-content" ref="content" @click="handleClick">
      <div class="point-bg-img-div" ref="pointBgImg">
        <img :src="pointBgImg" alt />
      </div>
      <!--      点击次数及顺序-->
      <div class="bg-click-div" v-html="html"></div>
    </div>
  </div>
</template>

<script>
import { reqCheck, reqGetPicture } from "@/service/api";
import { formatDate } from "@/utils/common";

export default {
  data() {
    return {
      start: 0,
      startY: 0,
      startSlidingTime: "",
      entSlidingTime: "",
      trackArr: [], // 坐标信息
      clickCount: 0, // 已点击次数
      imgSrc: "", // 需点击的文字图片
      pointBgImg: "", // 背景图片
      html: "", // 生成已点击的次数顺序
      type: "" // 验证类型
    };
  },
  mounted() {
    // 获取验证信息
    this.refreshCaptcha();
  },
  methods: {
    // 开始点击
    move(event) {
      if (event instanceof TouchEvent) {
        event = event.touches[0];
      }
      // 存储坐标信息
      this.trackArr.push({
        x: event.offsetX,
        y: event.offsetY,
        t: 2,
        type: "move"
      });
    },
    // 验证
    valid() {
      let captchaTrack = {
        bgImageWidth: this.$refs.pointBgImg.offsetWidth,
        bgImageHeight: this.$refs.content.offsetHeight,
        sliderImageWidth: -1, // 后端接收 但文字点选没有 所以传-1
        sliderImageHeight: -1,
        startSlidingTime: formatDate(this.startSlidingTime),
        entSlidingTime: formatDate(this.entSlidingTime),
        trackList: this.trackArr
      };
      reqCheck({
        captchaTrack,
        type: this.type,
        id: this.id
      }).then(res => {
        this.refreshCaptcha();
        if (res.data.code == "00") {
          this.$toast({
            message: "验证成功!"
          });
          this.$emit("success");
          this.reset();
        }
      });
    },
    // 重置
    reset() {
      this.clickCount = 0;
      this.html = "";
      this.start = 0;
      this.startSlidingTime = null;
      this.entSlidingTime = null;
      this.trackArr.length = 0;
      this.clickCount = 0;
      this.startY = 0;
      window.removeEventListener("mousemove", this.move);
    },
    // 刷新
    refresh() {
      this.$emit("success");
    },
    // 获取验证信息
    refreshCaptcha() {
      this.reset();
      reqGetPicture({
        type: "WORD_IMAGE_CLICK"
      }).then(res => {
        this.doMoveX = 0;
        this.position = 0;
        if (res.data.code == "00") {
          this.pointBgImg = res.data.result.captcha.backgroundImage;
          this.imgSrc = res.data.result.captcha.sliderImage;
          this.id = res.data.result.id;
          this.type = res.data.result.captcha.data;
        }
      });
    },
    // 开始点击
    handleClick(event) {
      // 点击次数递增
      this.clickCount++;
      // 如果是第一次点击 存储开始时间 坐标信息
      if (this.clickCount === 1) {
        this.startSlidingTime = new Date();
        window.addEventListener("mousemove", this.move);
      }
      this.trackArr.push({
        x: event.offsetX,
        y: event.offsetY,
        type: "click",
        t: 2
      });
      // 点击的位置(相对于背景图)
      let left = event.offsetX - 5;
      let top = event.offsetY - 5;
      // 每次点击在当前点击的位置显示次数
      this.html +=
        "<span class='click-span' style='left:" +
        left +
        "px;top: " +
        top +
        "px'>" +
        this.clickCount +
        "</span>";
      // 如果点击4次 存储结束时间 坐标信息
      if (this.clickCount === 4) {
        this.entSlidingTime = new Date();
        window.removeEventListener("mousemove", this.move);
        this.valid();
      }
    }
  }
};
</script>
<style lang="scss">
.point {
  position: relative;
  user-select: none;
  background-color: #fff;
  width: 558px;
  height: 430px;
  z-index: 999;
  box-sizing: border-box;
  padding: 18px;
  border-radius: 12px;
  box-shadow: 0 0 22px 0 #999999;
  margin: 50px auto;
  .point-content {
    width: 100%;
    height: 310px;
    position: relative;
    .point-bg-img-div {
      width: 100%;
      position: absolute;
      transform: translate(0px, 0px);
      z-index: 0;
      img {
        width: 100%;
      }
    }
    .bg-click-div {
      pointer-events: none;
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      .click-span {
        position: absolute;
        left: 0;
        top: 0;
        border-radius: 50px;
        background-color: #409eff;
        width: 20px;
        height: 20px;
        text-align: center;
        line-height: 20px;
        font-size: 20px;
        color: #fff;
        border: 2px solid #fff;
      }
    }
  }
  .point-move {
    height: 70px;
    width: 100%;
    position: relative;
    display: flex;
    .point-move-span {
      font-size: 26px;
      display: inline-block;
      line-height: 70px;
      margin-left: 10px;
    }
    .tip-img {
      width: 200px;
      position: absolute;
      right: 60px;
      bottom: 10px;
    }
  }
  .point-bottom {
    position: absolute;
    bottom: 10px;
    right: 0;
    z-index: 2;
  }
}
</style>