<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, onShow } from '@dcloudio/uni-app'
import { myRequest } from '@/utils/api.js'
// 引入解密函数
import { decryptDes } from '@/utils/des.js'

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

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

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

const timer = ref(null)

const timerCode = ref(null)

onLoad((data) => {
	// 获取二维码信息内容
	QRCodeUrl.value = data.value

	// 监听用户截屏事件
	uni.onUserCaptureScreen(showTips)

	// 获取屏幕亮度
	getBrightness()
	// 设置屏幕亮度
	setBrightness(1)

	//开启定时器 监听ios用户是否录屏
	timer.value = setInterval(() => {
		uni.getScreenRecordingState({
			success: (res) => {
				if (res.state == 'on') {
					uni.showModal({
						title: '提示',
						content: '此页面不允许录屏',
						showCancel: false,
						success: () => {
							uni.redirectTo({
								url: '/pages/home/home'
							})
						}
					})
				}
			}
		})
	}, 1000)

	// 开启安卓用户禁止截屏
	if (wx.setVisualEffectOnCapture) {
		wx.setVisualEffectOnCapture({
			visualEffect: 'hidden',
			complete: function (res) {
				console.log('开启', res)
			}
		})
	}
})

onShow(() => {
	if (timerCode.value) {
		clearInterval(timerCode.value)
	}
	timerCode.value = setInterval(() => {
		getQrcode()
	}, 55000)
})

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

	// 清除定时器
	clearInterval(timer.value)

	// 关闭安卓用户禁止截屏
	if (wx.setVisualEffectOnCapture) {
		wx.setVisualEffectOnCapture({
			visualEffect: 'none',
			complete: function (res) {
				console.log('解除', res)
			}
		})
	}

	clearInterval(timerCode.value)
})

// 获取身份码信息
const getQrcode = async () => {
	const res = await myRequest({
		url: '/wanzai/api/smartQrcode/generateQrcode',
		data: {
			userId: uni.getStorageSync('userInfo').id
		}
	})
	// console.log(res)
	const result = JSON.parse(decryptDes(res.data))
	// console.log(result)
	QRCodeUrl.value = result.qrcode
}

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

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

// 提示信息函数
const showTips = async () => {
	setTimeout(() => {
		uni.showToast({
			title: '不要把二维码给他人使用，否则将冻结账号',
			icon: 'none',
			duration: 3500
		})
	}, 100)
	const res = await myRequest({
		url: '/wanzai/api/smartScreenshotRecord/userScreenshotRecord',
		data: {
			userId: uni.getStorageSync('userInfo').id
		}
	})
	// console.log(res)
}
</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>
