uni-qrcode.vue 4.2 KB
Newer Older
芊里 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
<template>
	<view>
		<canvas :id="cid" :canvas-id="cid" :style="{width: `${size}px`, height: `${size}px`}" />
	</view>
</template>

<script>
	import uQRCode from '@/common/uqrcode.js'

	export default {
		props: {
			cid: {
				type: String,
				required: true
			},
			text: {
				type: String,
				required: true
			},
			size: {
				type: Number,
				default: uni.upx2px(590)
			},
			margin: {
				type: Number,
				default: 0
			},
			backgroundColor: {
				type: String,
				default: '#ffffff'
			},
			foregroundColor: {
				type: String,
				default: '#000000'
			},
			backgroundImage: {
				type: String
			},
			logo: {
				type: String
			},
			makeOnLoad: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {

			}
		},
		mounted() {
			if (this.makeOnLoad) {
				this.make()
			}
		},
		methods: {
			async make() {
				var options = {
					canvasId: this.cid,
					componentInstance: this,
					text: this.text,
					size: this.size,
					margin: this.margin,
					backgroundColor: this.backgroundImage ? 'rgba(255,255,255,0)' : this.backgroundColor,
					foregroundColor: this.foregroundColor
				}
				var filePath = await this.makeSync(options)

				if (this.backgroundImage) {
					filePath = await this.drawBackgroundImageSync(filePath)
				}

				if (this.logo) {
					filePath = await this.drawLogoSync(filePath)
				}

				this.makeComplete(filePath)
			},
			makeComplete(filePath) {
				this.$emit('makeComplete', filePath)
			},
			drawBackgroundImage(options) {
				var ctx = uni.createCanvasContext(this.cid, this)

				ctx.drawImage(this.backgroundImage, 0, 0, this.size, this.size)

				ctx.drawImage(options.filePath, 0, 0, this.size, this.size)

				ctx.draw(false, () => {
					uni.canvasToTempFilePath({
						canvasId: this.cid,
						success: res => {
							options.success && options.success(res.tempFilePath)
						},
						fail: error => {
							options.fail && options.fail(error)
						}
					}, this)
				})
			},
			async drawBackgroundImageSync(filePath) {
				return new Promise((resolve, reject) => {
					this.drawBackgroundImage({
						filePath: filePath,
						success: res => {
							resolve(res)
						},
						fail: error => {
							reject(error)
						}
					})
				})
			},
			fillRoundRect(ctx, r, x, y, w, h) {
				ctx.save()
				ctx.translate(x, y)
				ctx.beginPath()
				ctx.arc(w - r, h - r, r, 0, Math.PI / 2)
				ctx.lineTo(r, h)
				ctx.arc(r, h - r, r, Math.PI / 2, Math.PI)
				ctx.lineTo(0, r)
				ctx.arc(r, r, r, Math.PI, Math.PI * 3 / 2)
				ctx.lineTo(w - r, 0)
				ctx.arc(w - r, r, r, Math.PI * 3 / 2, Math.PI * 2)
				ctx.lineTo(w, h - r)
				ctx.closePath()
				ctx.setFillStyle('#ffffff')
				ctx.fill()
				ctx.restore()
			},
			drawLogo(options) {
				var ctx = uni.createCanvasContext(this.cid, this)

				ctx.drawImage(options.filePath, 0, 0, this.size, this.size)

				var logoSize = this.size / 4
				var logoX = this.size / 2 - logoSize / 2
				var logoY = logoX

				var borderSize = logoSize + 10
				var borderX = this.size / 2 - borderSize / 2
				var borderY = borderX
				var borderRadius = 5

				this.fillRoundRect(ctx, borderRadius, borderX, borderY, borderSize, borderSize)

				ctx.drawImage(this.logo, logoX, logoY, logoSize, logoSize)
				
				ctx.draw(false, () => {
					uni.canvasToTempFilePath({
						canvasId: this.cid,
						success: res => {
							options.success && options.success(res.tempFilePath)
						},
						fail: error => {
							options.fail && options.fail(error)
						}
					}, this)
				})
			},
			async drawLogoSync(filePath) {
				return new Promise((resolve, reject) => {
					this.drawLogo({
						filePath: filePath,
						success: res => {
							resolve(res)
						},
						fail: error => {
							reject(error)
						}
					})
				})
			},
			async makeSync(options) {
				return new Promise((resolve, reject) => {
					uQRCode.make({
						canvasId: options.canvasId,
						componentInstance: options.componentInstance,
						text: options.text,
						size: options.size,
						margin: options.margin,
						backgroundColor: options.backgroundColor,
						foregroundColor: options.foregroundColor,
						success: res => {
							resolve(res)
						},
						fail: error => {
							reject(error)
						}
					})
				})
			}
		}
	}
</script>