qr 二维码

基于 qrious 生成二维码。

默认二维码的操作并不是刚需的原因,因此采用一种延迟加载脚本的形式,可以通过全局配置配置来改变默认 CDN 路径(或使用本地路径),默认情况下使用 https://cdn.bootcdn.net/ajax/libs/qrious/4.0.2/qrious.min.js。或安装 npm i --save qrious 依赖包并在 angular.jsonscripts 引用 "node_modules/qrious/dist/qrious.min.js"

  1. import { QRModule } from '@delon/abc/qr';

代码演示

qr 二维码 - 图1

基础样例

最简单的用法。

  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'components-qr-basic',
  4. template: `
  5. <h3>{{ type }}</h3>
  6. <qr [value]="value"></qr>
  7. <div>
  8. <button *ngFor="let t of types" nz-button (click)="change(t)">{{ t }}</button>
  9. </div>
  10. `,
  11. })
  12. export class ComponentsQrBasicComponent implements OnInit {
  13. types = ['url', 'email', 'tel', 'cn', 'vcard'];
  14. value = '';
  15. type = '';
  16. change(type: string) {
  17. this.type = type;
  18. switch (type) {
  19. case 'url':
  20. this.value = 'https://ng-alain.com/';
  21. break;
  22. case 'email':
  23. this.value = 'mailto:cipchk@qq.com';
  24. break;
  25. case 'tel':
  26. this.value = 'tel:15900000000';
  27. break;
  28. case 'cn':
  29. this.value = '你好🇨🇳';
  30. break;
  31. case 'vcard':
  32. this.value = `BEGIN:VCARD
  33. VERSION:4.0
  34. N:色;卡;;Mr.;
  35. FN:卡色
  36. ORG:NG-ALAIN
  37. TITLE:NG-ALAIN
  38. PHOTO;MEDIATYPE=image/svg:https://ng-alain.com/assets/img/logo-color.svg
  39. TEL;TYPE=work,voice;VALUE=uri:tel:15900000000
  40. ADR;TYPE=WORK;PREF=1;LABEL="中国上海":;;上海;中国
  41. EMAIL:cipchk@qq.com
  42. x-qq:94458893
  43. END:VCARD`;
  44. break;
  45. }
  46. }
  47. ngOnInit(): void {
  48. this.change('url');
  49. }
  50. }

qr 二维码 - 图2

设计器

利用 change 可以回调二维码 dataURL 值。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'components-qr-design',
  4. template: `
  5. <div nz-row [nzGutter]="24">
  6. <div nz-col [nzSpan]="8" class="text-center">
  7. <qr
  8. [value]="value"
  9. [background]="background"
  10. [backgroundAlpha]="backgroundAlpha"
  11. [foreground]="foreground"
  12. [foregroundAlpha]="foregroundAlpha"
  13. [level]="level"
  14. [mime]="mime"
  15. [padding]="padding"
  16. [size]="size"
  17. (change)="change($event)"
  18. style="border:1px solid #999"
  19. ></qr>
  20. </div>
  21. <div nz-col [nzSpan]="16">
  22. <se-container col="1">
  23. <se label="背景">
  24. <nz-input-group>
  25. <div nz-row nzGutter="8">
  26. <div nz-col nzSpan="12">
  27. <input nz-input type="color" [(ngModel)]="background" [ngModelOptions]="{ standalone: true }" />
  28. </div>
  29. <div nz-col nzSpan="12">
  30. <nz-input-number
  31. [(ngModel)]="backgroundAlpha"
  32. [nzMin]="0"
  33. [nzMax]="1"
  34. [nzStep]="0.1"
  35. [ngModelOptions]="{ standalone: true }"
  36. ></nz-input-number>
  37. </div>
  38. </div>
  39. </nz-input-group>
  40. </se>
  41. <se label="前景">
  42. <nz-input-group>
  43. <div nz-row nzGutter="8">
  44. <div nz-col nzSpan="12">
  45. <input nz-input type="color" [(ngModel)]="foreground" [ngModelOptions]="{ standalone: true }" />
  46. </div>
  47. <div nz-col nzSpan="12">
  48. <nz-input-number
  49. [(ngModel)]="foregroundAlpha"
  50. [nzMin]="0"
  51. [nzMax]="1"
  52. [nzStep]="0.1"
  53. [ngModelOptions]="{ standalone: true }"
  54. ></nz-input-number>
  55. </div>
  56. </div>
  57. </nz-input-group>
  58. </se>
  59. <se label="误差">
  60. <nz-select [(ngModel)]="level" [ngModelOptions]="{ standalone: true }">
  61. <nz-option nzValue="L" nzLabel="L"></nz-option>
  62. <nz-option nzValue="M" nzLabel="M"></nz-option>
  63. <nz-option nzValue="Q" nzLabel="Q"></nz-option>
  64. <nz-option nzValue="H" nzLabel="H"></nz-option>
  65. </nz-select>
  66. </se>
  67. <se label="Mime">
  68. <nz-select [(ngModel)]="mime" [ngModelOptions]="{ standalone: true }">
  69. <nz-option nzValue="image/png" nzLabel="image/png"></nz-option>
  70. <nz-option nzValue="image/jpeg" nzLabel="image/jpeg"></nz-option>
  71. <nz-option nzValue="image/gif" nzLabel="image/gif"></nz-option>
  72. </nz-select>
  73. </se>
  74. <se label="内边距">
  75. <nz-input-number [(ngModel)]="padding" [ngModelOptions]="{ standalone: true }" [nzMin]="0" [nzMax]="100"></nz-input-number> px
  76. </se>
  77. <se label="大小">
  78. <nz-input-number
  79. [(ngModel)]="size"
  80. [ngModelOptions]="{ standalone: true }"
  81. [nzMin]="100"
  82. [nzMax]="1000"
  83. [nzStep]="padding"
  84. ></nz-input-number>
  85. px
  86. </se>
  87. </se-container>
  88. </div>
  89. </div>
  90. `,
  91. })
  92. export class ComponentsQrDesignComponent {
  93. value = 'https://ng-alain.com/';
  94. background = '#ffffff';
  95. backgroundAlpha = 1.0;
  96. foreground = '#000000';
  97. foregroundAlpha = 1.0;
  98. level = 'L';
  99. mime = 'image/png';
  100. padding = 10;
  101. size = 220;
  102. change(dataURL: string) {
  103. console.log(dataURL);
  104. }
  105. }

API

qr

成员说明类型默认值全局配置
[value]string-
[background]背景stringwhite
[backgroundAlpha]背景透明级别,范围:0-1 之间number1
[foreground]前景stringwhite
[foregroundAlpha]前景透明级别,范围:0-1 之间number1
[level]误差校正级别‘L’,’M’,’Q’,’H’‘L’
[mime]二维码输出图片MIME类型stringimage/png
[padding]内边距(单位:px)number10
[size]大小(单位:px)number220
[delay]延迟渲染,单位:毫秒number0
(change)变更时回调,返回二维码dataURL值EventEmitter<string>-

常见问题

自定义LOGO

参考 #100 的写法。