<template>
	<view>
		<!-- HTML -->
		<mumu-get-qrcode @success='qrcodeSucess' @error="qrcodeError" :scanWidth="300"
			:scanHeight="300"></mumu-get-qrcode>
	</view>
</template>

<script>
	// js
	import mumuGetQrcode from '@/uni_modules/mumu-getQrcode/components/mumu-getQrcode/mumu-getQrcode.vue'
	// 嫌路径长的话可以单独复制出来
	export default {
		components: {
			mumuGetQrcode
		},
		methods: {
			qrcodeSucess(data) {
				console.log("二维码数据", data)
				this.handleScanResult(data);
			},
			qrcodeError(err) {
				console.log(err)
				let that = this;
				uni.showModal({
					title: '摄像头授权失败',
					content: '摄像头授权失败，请检测当前浏览器是否有摄像头权限。',
					success: () => {
						that.redirectToWorkbench(); // 请求错误跳回工作台
					}
				})
			},
			handleScanResult(result) {
				let that = this;
				getInspectionManage(result)
					.then(res => {
						if (res.data.inspectionStatus === "1") {
							let inspectionPoint = res.data.inspectionPoint;
							let inspectionRequirements = res.data.inspectionRequirements;
							let inspectionPointId = res.data.id
							uni.reLaunch({
								url: `/pages/work/inspection/scanSign/index?inspectionPoint=${inspectionPoint}&inspectionRequirements=${inspectionRequirements}&inspectionPointId=${inspectionPointId}`
							});
						} else {
							uni.showToast({
								title: '验证码已失效！',
								icon: 'none'
							});
							that.redirectToWorkbench(); // 验证码失效跳回工作台
						}
					})
					.catch(err => {
						console.log("请求错误", err);
						uni.showToast({
							title: '服务器错误',
							icon: 'none'
						});
						that.redirectToWorkbench(); // 请求错误跳回工作台
					});
			},
			// 新增方法：跳转回工作台
			redirectToWorkbench() {
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/work/index'
					});
				}, 1500); // 1.5秒后跳转，让用户看到提示信息
			},
		}
	}
</script>

<style>

</style>