1.0.0 • Published 3 years ago

hsswebyzm v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

==================

下载 svg-captcha


在egg的app文件夹下创建service文件夹  下再创建VerifyTools.js文件
'use strict';
const Service = require('egg').Service;
const svgCaptcha = require('svg-captcha');
// 验证码配置信息
const options = {
    size: 4, // 验证码长度(显示几个字符)
    fontSize: 50, // 验证码的字体大小
    width: 100, // 验证码的宽度
    height: 50, // 验证码的高度
    background: '#cc9966', // 验证码的背景颜色
};
class VerifyToolsService extends Service {
    async captcha() {
        // 第三方插件,实现验证码功能
        const captcha = svgCaptcha.create(options);
        // 将验证码text文本保存到全局session中, 观察者/发布者订阅模式
        this.ctx.session.code = captcha.text;
        return captcha;
    }
}
module.exports = VerifyToolsService;



在home.js 文件下


  async verify() {
        const { ctx } = this;
        // 服务里面的方法
        let captcha = await this.service.verifyTools.captcha();
        // 返回的类型
        ctx.response.type = 'image/svg+xml';
        // {data: '<svg.../svg>', text: 'abcd'}
        // this.ctx.session.code  生成的yzm这在后端的存储位置
        ctx.body = captcha.data;
    }


在router.js文件下注册


 router.post('/verify', controller.home.verify);

==================