captchas.js 542 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. import captchapng from 'captchapng';
  3. class Captchas {
  4. constructor(){
  5. }
  6. //验证码
  7. async getCaptchas(req, res, next){
  8. const cap = parseInt(Math.random()*9000+1000);
  9. const p = new captchapng(80,30, cap);
  10. p.color(0, 0, 0, 0);
  11. p.color(80, 80, 80, 255);
  12. const base64 = p.getBase64();
  13. res.cookie('cap', cap, { maxAge: 300000, httpOnly: true });
  14. res.send({
  15. status: 1,
  16. code: 'data:image/png;base64,' + base64
  17. });
  18. }
  19. }
  20. export default new Captchas()