1.首先安装
npm install --save svg-captcha
2. 在app/controller下面新建util.js
'use strict';
const svgCaptcha = require('svg-captcha');
const Controller = require('egg').Controller;
class UtilController extends Controller {
async captcha() {
const captcha = svgCaptcha.create({
size: 4,
fontSize: 50,
ignoreChars: 'Ooli',
width: 100,
height: 40,
noise: 3,
color: true,
background: '#cc9966',
});
this.ctx.session.captcha = captcha.text;
this.ctx.response.type = 'image/svg+xml';
this.ctx.body = captcha.data;
}
}
module.exports = UtilController;
3.在router.js里面新增
router.get('/captcha', controller.util.captcha);
4.打开浏览器运行http://127.0.0.1:7001/captcha就可以看到对应的图形验证码了