Crunchy
2024-11-07 673d58023f4c0fe8633a107238ec8b577496717a
添加登录验证码
已修改1个文件
已添加1个文件
180 ■■■■■ 文件已修改
src/page/login/LoginCode.vue 131 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/page/login/userlogin.vue 49 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/page/login/LoginCode.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,131 @@
<template>
  <div class="s-canvas" @click="$emit('refresh')">
    <canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas>
  </div>
</template>
<script>
export default {
  name: 'LoginCode',
  props: {
    identifyCode: {
      // é»˜è®¤æ³¨å†Œç 
      type: String,
      default: '1234',
    },
    fontSizeMin: {
      // å­—体最小值
      type: Number,
      default: 25,
    },
    fontSizeMax: {
      // å­—体最大值
      type: Number,
      default: 35,
    },
    backgroundColorMin: {
      // éªŒè¯ç å›¾ç‰‡èƒŒæ™¯è‰²æœ€å°å€¼
      type: Number,
      default: 200,
    },
    backgroundColorMax: {
      // éªŒè¯ç å›¾ç‰‡èƒŒæ™¯è‰²æœ€å¤§å€¼
      type: Number,
      default: 220,
    },
    dotColorMin: {
      // èƒŒæ™¯å¹²æ‰°ç‚¹æœ€å°å€¼
      type: Number,
      default: 60,
    },
    dotColorMax: {
      // èƒŒæ™¯å¹²æ‰°ç‚¹æœ€å¤§å€¼
      type: Number,
      default: 120,
    },
    contentWidth: {
      // å®¹å™¨å®½åº¦
      type: Number,
      default: 90,
    },
    contentHeight: {
      // å®¹å™¨é«˜åº¦
      type: Number,
      default: 38,
    },
  },
  methods: {
    // ç”Ÿæˆä¸€ä¸ªéšæœºæ•°
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    },
    // ç”Ÿæˆä¸€ä¸ªéšæœºçš„颜色
    randomColor(min, max) {
      const r = this.randomNum(min, max)
      const g = this.randomNum(min, max)
      const b = this.randomNum(min, max)
      return 'rgb(' + r + ',' + g + ',' + b + ')'
    },
    // ç”»å›¾
    drawPic() {
      const canvas = document.getElementById('s-canvas')
      // åˆ›å»ºä¸€ä¸ª2D对象作为上下文。
      const ctx = canvas.getContext('2d')
      ctx.textBaseline = 'bottom'
      // ç»˜åˆ¶èƒŒæ™¯
      ctx.fillStyle = '#e6ecfd'
      ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)
      // ç»˜åˆ¶æ–‡å­—
      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(50, 160) // éšæœºç”Ÿæˆå­—体颜色
      ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei' // éšæœºç”Ÿæˆå­—体大小
      const x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))
      const y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
      var deg = this.randomNum(-30, 30)
      // ä¿®æ”¹åæ ‡åŽŸç‚¹å’Œæ—‹è½¬è§’åº¦
      ctx.translate(x, y)
      ctx.rotate((deg * Math.PI) / 180)
      ctx.fillText(txt, 0, 0)
      // æ¢å¤åæ ‡åŽŸç‚¹å’Œæ—‹è½¬è§’åº¦
      ctx.rotate((-deg * Math.PI) / 180)
      ctx.translate(-x, -y)
    },
    // ç»˜åˆ¶å¹²æ‰°çº¿
    drawLine(ctx) {
      for (let i = 0; i < 4; i++) {
        ctx.strokeStyle = this.randomColor(100, 200)
        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) {
      for (let i = 0; i < 30; 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>
src/page/login/userlogin.vue
@@ -35,6 +35,25 @@
        <i slot="prefix" class="iconfont icon-mima"></i>
      </el-input>
    </el-form-item>
    <el-form-item prop="loginCode">
      <el-col :span="16">
        <el-input
          v-model="loginCode"
          size="small"
          auto-complete="off"
          placeholder="请输入验证码"
          @keyup.enter.native="handleLogin"
        >
        </el-input>
      </el-col>
      <el-col :span="6" :offset="2">
        <login-code
          :key="refreshKey"
          :identifyCode="loginCodeValue"
          @refresh="refreshLoginValueCode"
        />
      </el-col>
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
@@ -51,16 +70,14 @@
</template>
<script>
import { randomLenNum } from '@/util/util'
import { mapGetters } from 'vuex'
import { getCode } from '@/api/code'
import Verify from '@/components/verifition/Verify'
import { getSsoAuthUrl } from '@/api/login'
import LoginCode from '@/page/login/LoginCode.vue'
export default {
  name: 'Userlogin',
  components: {
    Verify
    LoginCode
  },
  data() {
    return {
@@ -89,7 +106,12 @@
        ]
      },
      passwordType: 'password',
      SSO: window.location.hash
      SSO: window.location.hash,
      loginCode: null,
      loginCodeValue: Math.random()
        .toString(36)
        .substr(2, 4),
      refreshKey: 0
    }
  },
  created() {
@@ -112,11 +134,21 @@
        : (this.passwordType = '')
    },
    handleLogin() {
      if (!this.loginCode) {
        this.$message.error('请输入验证码')
        return
      }
      if (this.loginCode.toLowerCase() !== this.loginCodeValue) {
        this.$message.error('验证码不正确')
        return
      }
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.$router.push({ path: this.tagWel.value })
          })
        } else {
          this.refreshLoginValueCode()
        }
      })
    },
@@ -127,6 +159,13 @@
          this.$router.push({ path: this.tagWel.value })
        })
      }
    },
    // åˆ·æ–°éªŒè¯ç 
    refreshLoginValueCode() {
      this.loginCodeValue = Math.random()
        .toString(36)
        .substr(2, 4)
      this.refreshKey++
    }
  }
}