Vue验证码模块

一个普通图形验证码的模块,使用方式如下:

 

导入模块

import random from '@/util/VerificationCode'

使用方式

this.randomObj = random({
  id: 'random',
  api: {
      url: '/api/getCode',
      headers: {
          'Content-Type': 'application/json'
      }
  }
})

源码

!(function (window, document) {
    const charArr = 'abcdefghjkmnpqrstuvwxyz123456789ABCDEFGHJKMNPQRSTUVWXYZ'.split('');

    function GVerify(options) {
        this.options = {
            id: options.id,
            api: options.api,
            length: options.length ? options.length : 4,
            canvasId: 'VerifyCanvas_' + options.id,
            type: 'blend'
        };

        this._init();
        this.getCode();
    }

    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min) + min)
    }

    function randomColor(min, max) {
        let r = randomNum(min, max);
        let g = randomNum(min, max);
        let b = randomNum(min, max);
        return 'rgb(' + r + ',' + g + ',' + b + ')'
    }

    function generateCode(len) {
        let code = '';
        for (let i=0; i<len; i++) {
            code += charArr[randomNum(0, charArr.length)];
        }
        console.log(code);
        return code;
    }

    GVerify.prototype = {
        version: '1.0.0',
        _init() {
            let con = document.getElementById(this.options.id);
            let canvas = document.createElement('canvas');
            this.options.width = con.offsetWidth > 0 ? con.offsetWidth : '100';
            this.options.height = con.offsetHeight > 0 ? con.offsetHeight : '30';
            canvas.id = this.options.canvasId;
            canvas.width = this.options.width;
            canvas.height = this.options.height;
            canvas.style.cursor = 'pointer';
            canvas.innerHTML = '您的浏览器版本不支持canvas';
            con.appendChild(canvas);
            canvas.addEventListener('click', () => {
                this.getCode()
            })
        },
        // 刷新
        refresh() {
            let canvas = document.getElementById(this.options.canvasId);
            if (!canvas.getContext) return;
            let ctx = canvas.getContext('2d');
            ctx.textBaseline = 'middle';
            ctx.fillStyle = randomColor(180, 240);
            ctx.fillRect(0, 0, this.options.width, this.options.height);
            for (let i = 0; i < this.options.code.length; i++) {
                ctx.font = randomNum(this.options.height / 2, this.options.height) + 'px SimHei';
                ctx.fillStyle = randomColor(50, 160);
                ctx.shadowOffsetX = randomNum(-3, 3);
                ctx.shadowOffsetY = randomNum(-3, 3);
                ctx.shadowBlur = randomNum(-3, 3);
                ctx.shadowColor = 'rgba(0, 0, 0, 0.3)';
                let x = this.options.width / this.options.code.length * i;
                x = x + (this.options.width / this.options.code.length / 2) - 10; // 居中
                let y = this.options.height / 2 - 1;
                let deg = randomNum(-30, 30);
                ctx.translate(x, y);
                ctx.rotate(deg * Math.PI / 180);
                ctx.fillText(this.options.code.substring(i, i + 1), 0, 0);
                ctx.rotate(-deg * Math.PI / 180);
                ctx.translate(-x, -y);
            }
            for (let i = 0; i < 4; i++) {
                ctx.strokeStyle = randomColor(40, 180);
                ctx.beginPath();
                ctx.moveTo(randomNum(0, this.options.width), randomNum(0, this.options.height));
                ctx.lineTo(randomNum(0, this.options.width), randomNum(0, this.options.height));
                ctx.stroke();
            }
            for (let i = 0; i < this.options.width / 4; i++) {
                ctx.fillStyle = randomColor(0, 255);
                ctx.beginPath();
                ctx.arc(randomNum(0, this.options.width), randomNum(0, this.options.height), 1, 0, 2 * Math.PI);
                ctx.fill();
            }
            this.ctx = canvas;
        },
        // 重载大小
        reload() {},
        // 获取验证码
        getCode() {
            if (this.options.api && this.options.api.url) { // 从api获取
                let xhr = new XMLHttpRequest();
                xhr.onreadystatechange = () => {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        this.options.code = xhr.responseText;
                        this.refresh();
                    }
                };
                xhr.open(this.options.api.method ? this.options.api.method : 'POST', this.options.api.url);
                for (const key in this.options.api.headers) {
                    xhr.setRequestHeader(key, this.options.api.headers[key]);
                }
                xhr.send();
            } else { // 本地生成
                let code = generateCode(this.options.length);
                this.options.code = code;
                this.refresh();
            }
        },
        // 校验输入
        validate(code) {
            code = code.toLowerCase();
            let v_code = this.options.code.toLowerCase();
            return code == v_code;
        }
    };

    window.GVerify = GVerify;
})(window, document);

export default conf => {
    if (!conf.id) return false;
    return new GVerify(conf);
}

 

posted @ 2023-10-21 17:10  散人长情  阅读(8)  评论(0编辑  收藏  举报