<template>
	<view class="container">
		<uv-qrcode ref="qrcodeRef" size="400rpx" :value="QRCodeUrl" :options="options"></uv-qrcode>

		<view class="tips">不要把二维码给他人使用，否则冻结账号</view>
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad, onUnload } from '@dcloudio/uni-app'

// 二维码信息内容
const QRCodeUrl = ref('')

// 屏幕亮度
const brightness = ref(0)

// 二维码自定义样式
const options = {
	typeNumber: 4,
	foregroundImageBorderRadius: 5,
	foregroundImageSrc: '/static/images/school-logo.jpg'
}

onLoad((data) => {
	// 监听用户截屏事件
	uni.onUserCaptureScreen(showTips)
	// 获取二维码信息内容
	QRCodeUrl.value = data.value
	// 获取屏幕亮度
	getBrightness()
	// 设置屏幕亮度
	setBrightness(1)
})

onUnload(() => {
	// 取消监听用户截屏事件
	uni.offUserCaptureScreen()
	// 恢复屏幕亮度
	setBrightness(brightness.value)
})

// 获取屏幕亮度
const getBrightness = () => {
	uni.getScreenBrightness({
		success: (res) => {
			brightness.value = res.value
		}
	})
}

// 设置屏幕亮度
const setBrightness = (data) => {
	uni.setScreenBrightness({
		value: data
	})
}

// 提示信息函数
const showTips = () => {
	uni.showToast({
		title: '不要把二维码给他人使用，否则将冻结账号',
		icon: 'none',
		duration: 3500
	})
}
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	justify-content: space-evenly;
	align-items: center;
	min-height: 100vh;
	background-color: #f1f6fe;
}
</style>
