15.koa-图形验证码

455 阅读1分钟

安装

npm i trek-captcha -S

使用

  • html
<img src="/user/captcha" id="captcha" ref='captcha'>
<button @click='changeCaptcha'>换一张</button>

const vm = new Vue({
   el: '#app',
   data() {
       return {
       }
   },

   methods: {
       changeCaptcha() {
           this.$refs.captcha.src = '/user/captcha?r=' + Date.now();
       },
   }
})
  • user.js
//图形验证码
const captcha = require('trek-captcha');
router.get('/captcha', async (ctx, next) => {
    const {
        token,
        buffer
    } = await captcha({
        size: 4 //4位
    });
    //token的作用 前端输入完验证码与此时的token做对比
    ctx.body = buffer;
})