https://github.com/produck/svg-captcha

  1. npm install --save svg-captcha

验证码细节

用redis存验证码值
前台点击二维码,要刷新;刷新页面验证码也要刷新
https://www.codeleading.com/article/29115579333/
https://www.1024sou.com/article/440263.html
https://www.jianshu.com/p/0865b692cc71
https://blog.csdn.net/xuelang532777032/article/details/118677831

Controller

/app/controller/captcha.js

  1. 'use strict';
  2. const svgCaptcha = require('svg-captcha');
  3. const {Controller} = require('egg');
  4. class CaptchaController extends Controller {
  5. async index() {
  6. const captcha = svgCaptcha.create({
  7. size: 4,
  8. fontSize: 50,
  9. ignoreChars: 'Ooli',
  10. width: 100,
  11. height: 40,
  12. noise: 3,
  13. color: true,
  14. background: '#cc9966',
  15. });
  16. this.ctx.session.captcha = captcha.text;
  17. this.ctx.response.type = 'image/svg+xml';
  18. this.ctx.body = captcha.data;
  19. }
  20. }
  21. module.exports = CaptchaController;

router.js 新增路由
浏览器运行http://127.0.0.1:7001/captcha 就可以看到对应的图形验证码

  1. router.get('/captcha', controller.captcha.index);