123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <template>
- <canvas @click="onClick()" :style="{ width: `${size}px`, height: `${size}px` }" :canvas-id="id" :id="id" type="2d" :hidpi="false"></canvas>
- </template>
- <script>
- /**
- * Qrcode 显示二维码
- * @description 二维码生成的组件
- * @property {String} id 多个时需设置不同id
- * @property {String} text 二维码内容
- * @property {Number} size 二维码大小
- * @property {String} background 背景颜色
- * @property {String} foreground 二维码颜色
- * @property {String} logo 中间logo资源
- * @property {String} logoSize logo大小
- * @property {Boolean} logoRound logo圆角
- *
- * @event {Function} click 点击 Qrcode 触发事件
- * @example <QiyanQrcode text="321"></QiyanQrcode>
- */
- import drawQrcode from "./utils.js";
- let canvasObj = {};
- export default {
- name: 'QiyanQrcode',
- emits: ['click'],
- props: {
- id: {
- type: String,
- default: "qrCanvas",
- },
- text: {
- type: String,
- default: "",
- },
- size: {
- type: Number,
- default: 200,
- },
- background: {
- type: String,
- default: "#ffffff",
- },
- foreground: {
- type: String,
- default: "#000000",
- },
- logo: {
- type: String,
- default: "",
- },
- logoSize: {
- type: Number,
- default: 60,
- },
- logoRound: {
- type: Boolean,
- default: false,
- },
- },
- watch: {
- text(nVal) {
- if (nVal) {
- this.draw();
- } else {
- console.error('qrcode text is null');
- }
- }
- },
- mounted() {
- this.$nextTick(() => {
- let _this = this;
- // #ifdef H5
- const canvas = document.getElementById(this.id).childNodes[0]
- canvasObj[this.id] = canvas;
- _this.draw();
- // #endif
- // #ifndef H5
- const query = uni.createSelectorQuery().in(this);
- query.select("#" + this.id).fields({
- node: true,
- size: true,
- },
- (data) => {
- canvasObj[this.id] = data.node;
- _this.draw();
- }
- )
- .exec();
- // #endif
- })
- },
- beforeUnmount() {
- delete canvasObj[this.id];
- },
- methods: {
- draw() {
- if (this.text && canvasObj[this.id]) {
- let canvas = canvasObj[this.id];
- const drawText = (imageResource) => {
- let image = {};
- if (imageResource) {
- image = {
- imageResource,
- width: this.logoSize,
- height: this.logoSize,
- round: this.logoRound
- }
- }
- drawQrcode({
- canvas,
- canvasId: this.id,
- width: this.size,
- height: this.size,
- padding: 0,
- background: this.background,
- foreground: this.foreground,
- text: this.text,
- image
- });
- }
- setTimeout(() => {
- if (this.logo) {
- let img;
- // #ifdef H5
- img = new Image();
- // #endif
- // #ifndef H5
- img = canvas.createImage();
- // #endif
- img.src = this.logo;
- img.onload = () => {
- drawText(img);
- }
- } else {
- drawText();
- }
- }, 100);
- }
- },
- onClick() {
- this.$emit('click');
- },
- }
- }
- </script>
|