<template>
	<view style="width: 345px;height:336px;background-color: #ffffff;border-radius: 10rpx;">
		<canvas style="width: 100%;height: 100%;" canvas-id="canvas" id="canvas"></canvas>
		<view>
			<u-button @click="save" type="primary">保存海报</u-button>
		</view>
	</view>
</template>

<script>
	let self;
	export default {
		props: {
			qrcode: {
				type: String,
				default: ''
			},
		},
		data() {
			return {
				screenWidth: 345,
				screenHeight: 336
			}
		},
		created() {
			self = this;

			// uni.getSystemInfo({
			// 	success: (res) => {
			// 		self.screenWidth = res.windowWidth - 30; // 屏幕宽度，单位为px
			// 		self.screenHeight = res.windowHeight; // 屏幕高度，单位为px
			// 	},
			// });

			self.draw();
		},

		methods: {
			getImageInfo(src) {
				return new Promise((resolve, reject) => {
					uni.getImageInfo({
						src,
						success: res => {
							resolve(res)
						},
						fail: err => {
							reject(err)
						}
					})
				})
			},
			async draw() {

				const ctx = uni.createCanvasContext('canvas');

				// 绘制图片1
				const image1 = await this.getImageInfo('https://xingdong-app.oss-cn-beijing.aliyuncs.com/share.png')
				let x = image1.width / 2;
				let y = image1.height / 2;
				ctx.drawImage(image1.path, 0, 0, x, y);
				
				// 绘制图片2
				const image2 = await this.getImageInfo(
					self.$store.state.auth.user.qrcode
				);

				ctx.drawImage(image2.path, x / 2 - 96, 90, 190, 190);

				ctx.font = '16px Regular'; // 字体大小
				ctx.fillStyle = '#1449AE'; // 字体填充颜色
				let invite_code = '邀请码：' + self.$store.state.auth.user.invite_code;
				ctx.fillText(invite_code, x / 2 - 90, 36);

				ctx.font = '14px Regular'; // 字体大小
				ctx.fillStyle = '#999999'; // 字体填充颜色
				ctx.fillText('欢迎加入羿充电', x / 2 - 70, 60);

				ctx.font = '14px Regular'; // 字体大小
				ctx.fillStyle = '#999999'; // 字体填充颜色
				ctx.fillText('长按保存或转发', x / 2 - 48, y - 28);

				setTimeout(function() { // uni-app必须加上延迟，不然显示不出来， 亲测 
					ctx.stroke();
					ctx.draw() //必须加上  uniapp 没这儿玩意儿 显示不出来不比原生  不加可以显示
				}, 300);

			},
			save() {
				// uni.canvasToTempFilePath({
				// 	canvasId: 'canvas',
				// 	success: res => {
				// 		uni.saveImageToPhotosAlbum({
				// 			filePath: res.tempFilePath,
				// 			success(res) {

				// 			}
				// 		})
				// 	}
				// })
			}
		}
	}
</script>

<style lang="scss">

</style>