svg-captcha :可以很轻松的生成日常使用的 svg 格式的图片验证码,灵活的参数配置能够满足项目特别的需求。
GitHub文档地址:https://github.com/produck/svg-captcha/blob/HEAD/README_CN.md
Node环境
1、安装方法
npm install svg-captcha
2、使用方法(express)
const svgCaptcha = require('svg-captcha'); // 引入模块
// 生成验证码的接口
exports.svgCode = (req, res) => {
const captcha = svgCaptcha.create({
size: 4, //验证码长度
noise: 1, //干扰线条数目
width: 300, //宽度
height: 50, //高度
inverse: false, // 翻转颜色
fontSize: 65, // 字体大小
color: true, // 验证码字符颜色(需设置背景色)
background: '#ccc', // 背景
});
code = captcha.text; // 存储验证码数值(这里使用变量来存储生成的验证码,其实可以使用session来进行存储)
res.type('svg'); // 响应的类型
res.send(captcha.data);
};
Vue中使用
// src="http://localhost:8000/svgCode" 为接口路径
<img
src="http://localhost:8000/svgCode"
alt="看不清?点击刷新"
@click="getSvgCode"
ref="captcha"
class="vercode"
/>
getSvgCode() {
// 每次指定的src要不一样,img才会重新请求,可以使用 Date.now() 或者随机数 Math.random()
this.$refs.captcha.src =
'http://localhost:8000/svgCode?time=' + Date.now();
}
使用效果