qr-image 在 Egg.js 中的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liqianglai/article/details/79065748

  • 安装
yarn add qr-image
  • 使用
<img src="/qrcode?text=hello Egg.js" alt="二维码"/>
'use strict';

const qr = require('qr-image');
const { Controller } = require('egg');

class UtilsController extends Controller {
  async qrcode() {
    const { ctx, logger } = this;
    const { text, size, margin } = ctx.query;
    try {
      // 大小默认5,二维码周围间距默认1
      var img = qr.image(text || '', { type: 'png', size: size || 5, margin: margin || 1 });
      ctx.status = 200;
      ctx.type = 'image/png';
      ctx.body = img;
    } catch (e) {
      ctx.status = 414;
      ctx.set('Content-Type', 'text/html');
      ctx.body = '<h1>414 Request-URI Too Large</h1>';
    }
  }
}

module.exports = UtilsController;


  • 详情参看

https://github.com/alexeyten/qr-image

猜你喜欢

转载自blog.csdn.net/liqianglai/article/details/79065748