qr 二维码
基于 qrious 生成二维码。
默认二维码的操作并不是刚需的原因,因此采用一种延迟加载脚本的形式,可以通过全局配置配置来改变默认 CDN 路径(或使用本地路径),默认情况下使用 https://cdn.bootcdn.net/ajax/libs/qrious/4.0.2/qrious.min.js
。或安装 npm i --save qrious
依赖包并在 angular.json
的 scripts
引用 "node_modules/qrious/dist/qrious.min.js"
。
import { QRModule } from '@delon/abc/qr';
代码演示
基础样例
最简单的用法。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'components-qr-basic',
template: `
<h3>{{ type }}</h3>
<qr [value]="value"></qr>
<div>
<button *ngFor="let t of types" nz-button (click)="change(t)">{{ t }}</button>
</div>
`,
})
export class ComponentsQrBasicComponent implements OnInit {
types = ['url', 'email', 'tel', 'cn', 'vcard'];
value = '';
type = '';
change(type: string) {
this.type = type;
switch (type) {
case 'url':
this.value = 'https://ng-alain.com/';
break;
case 'email':
this.value = 'mailto:cipchk@qq.com';
break;
case 'tel':
this.value = 'tel:15900000000';
break;
case 'cn':
this.value = '你好🇨🇳';
break;
case 'vcard':
this.value = `BEGIN:VCARD
VERSION:4.0
N:色;卡;;Mr.;
FN:卡色
ORG:NG-ALAIN
TITLE:NG-ALAIN
PHOTO;MEDIATYPE=image/svg:https://ng-alain.com/assets/img/logo-color.svg
TEL;TYPE=work,voice;VALUE=uri:tel:15900000000
ADR;TYPE=WORK;PREF=1;LABEL="中国上海":;;上海;中国
EMAIL:cipchk@qq.com
x-qq:94458893
END:VCARD`;
break;
}
}
ngOnInit(): void {
this.change('url');
}
}
设计器
利用 change
可以回调二维码 dataURL 值。
import { Component } from '@angular/core';
@Component({
selector: 'components-qr-design',
template: `
<div nz-row [nzGutter]="24">
<div nz-col [nzSpan]="8" class="text-center">
<qr
[value]="value"
[background]="background"
[backgroundAlpha]="backgroundAlpha"
[foreground]="foreground"
[foregroundAlpha]="foregroundAlpha"
[level]="level"
[mime]="mime"
[padding]="padding"
[size]="size"
(change)="change($event)"
style="border:1px solid #999"
></qr>
</div>
<div nz-col [nzSpan]="16">
<se-container col="1">
<se label="背景">
<nz-input-group>
<div nz-row nzGutter="8">
<div nz-col nzSpan="12">
<input nz-input type="color" [(ngModel)]="background" [ngModelOptions]="{ standalone: true }" />
</div>
<div nz-col nzSpan="12">
<nz-input-number
[(ngModel)]="backgroundAlpha"
[nzMin]="0"
[nzMax]="1"
[nzStep]="0.1"
[ngModelOptions]="{ standalone: true }"
></nz-input-number>
</div>
</div>
</nz-input-group>
</se>
<se label="前景">
<nz-input-group>
<div nz-row nzGutter="8">
<div nz-col nzSpan="12">
<input nz-input type="color" [(ngModel)]="foreground" [ngModelOptions]="{ standalone: true }" />
</div>
<div nz-col nzSpan="12">
<nz-input-number
[(ngModel)]="foregroundAlpha"
[nzMin]="0"
[nzMax]="1"
[nzStep]="0.1"
[ngModelOptions]="{ standalone: true }"
></nz-input-number>
</div>
</div>
</nz-input-group>
</se>
<se label="误差">
<nz-select [(ngModel)]="level" [ngModelOptions]="{ standalone: true }">
<nz-option nzValue="L" nzLabel="L"></nz-option>
<nz-option nzValue="M" nzLabel="M"></nz-option>
<nz-option nzValue="Q" nzLabel="Q"></nz-option>
<nz-option nzValue="H" nzLabel="H"></nz-option>
</nz-select>
</se>
<se label="Mime">
<nz-select [(ngModel)]="mime" [ngModelOptions]="{ standalone: true }">
<nz-option nzValue="image/png" nzLabel="image/png"></nz-option>
<nz-option nzValue="image/jpeg" nzLabel="image/jpeg"></nz-option>
<nz-option nzValue="image/gif" nzLabel="image/gif"></nz-option>
</nz-select>
</se>
<se label="内边距">
<nz-input-number [(ngModel)]="padding" [ngModelOptions]="{ standalone: true }" [nzMin]="0" [nzMax]="100"></nz-input-number> px
</se>
<se label="大小">
<nz-input-number
[(ngModel)]="size"
[ngModelOptions]="{ standalone: true }"
[nzMin]="100"
[nzMax]="1000"
[nzStep]="padding"
></nz-input-number>
px
</se>
</se-container>
</div>
</div>
`,
})
export class ComponentsQrDesignComponent {
value = 'https://ng-alain.com/';
background = '#ffffff';
backgroundAlpha = 1.0;
foreground = '#000000';
foregroundAlpha = 1.0;
level = 'L';
mime = 'image/png';
padding = 10;
size = 220;
change(dataURL: string) {
console.log(dataURL);
}
}
API
qr
成员 | 说明 | 类型 | 默认值 | 全局配置 |
---|---|---|---|---|
[value] | 值 | string | - | |
[background] | 背景 | string | white | ✅ |
[backgroundAlpha] | 背景透明级别,范围:0-1 之间 | number | 1 | ✅ |
[foreground] | 前景 | string | white | ✅ |
[foregroundAlpha] | 前景透明级别,范围:0-1 之间 | number | 1 | ✅ |
[level] | 误差校正级别 | ‘L’,’M’,’Q’,’H’ | ‘L’ | ✅ |
[mime] | 二维码输出图片MIME类型 | string | image/png | ✅ |
[padding] | 内边距(单位:px) | number | 10 | ✅ |
[size] | 大小(单位:px) | number | 220 | ✅ |
[delay] | 延迟渲染,单位:毫秒 | number | 0 | ✅ |
(change) | 变更时回调,返回二维码dataURL值 | EventEmitter<string> | - |
常见问题
自定义LOGO
参考 #100 的写法。