<!--扫描二维码页面-->
<template>
	<view id="saoCode">
		<web-view v-if="webViewType" :src="baseUrl+'code='+code+'&type='+type"></web-view>
	</view>
</template>

<script>
	import {
		config
	} from '@/api/request/config.js'
	import {
		chemicalAioVerifyScanLogin,
		chemicalAioVerifyAppletLogin,
		laboratoryAppBoardScanCodeVerification,
		laboratoryAppletGetSubDetailInfo
	} from '@/pages/api/index.js'
	export default {
		name: "saoCode",
		data() {
			return {
				webViewType: false,
				baseUrl: config.saoCodeTypeUrl,
				code: "",
				type: "",
			}
		},
		onLoad(option) {
			// console.log('option',option)
			let self = this;
			if (option.q) {
				let text = decodeURIComponent(option.q)
				let list = text.split("?")[1].split("&");
				let codeData = {};
				list.forEach((item) => {
					codeData[item.split("=")[0]] = item.split("=")[1];
				})
				if (!uni.getStorageSync('token')) {
					uni.setStorageSync('codeData', codeData);
					uni.reLaunch({
						url: '/pages/views/login/login',
					});
				} else {
					//二维码功能识别
					this.codeRecognize(codeData);
				}
			} else {
				let codeData = uni.getStorageSync('codeData');
				uni.removeStorageSync('codeData');
				//二维码功能识别
				this.codeRecognize(codeData);
			}
		},
		mounted() {

		},

		methods: {
			//二维码功能识别
			codeRecognize(codeData) {
				if (!codeData.type) {
					//非功能二维码提示
					uni.showToast({
						mask: true,
						icon: "none",
						position: "center",
						title: '请扫描正确的小程序二维码',
						duration: 2000
					});
					setTimeout(function() {
						uni.reLaunch({
							url: '/pages/views/home/home',
						});
					}, 2000);
				} else {
					if (codeData.type == 11) {
						//化学品终端-扫码登录
						this.chemicalAioVerifyScanLogin(codeData.code, codeData.subId, codeData.macId);
					} else if (codeData.type == 12) {
						//化学品终端-双人认证
						this.chemicalAioVerifyAppletLogin(codeData.doorId, codeData.subId, codeData.macId, codeData.code);
					} else if (codeData.type == 13) {
						//电子信息牌二维码-小程序扫码验证
						this.laboratoryAppBoardScanCodeVerification(codeData.macId, codeData.subId, codeData.code);
					} else if (codeData.type == 14) {
						//化学品-操作教程
						uni.reLaunch({
							url: '/pages/views/saoCode/chemicalsInstructionsVideo'
						});
					} else if (codeData.type == 15) {
						//安科院设备信息
						uni.reLaunch({
							url: '/pages_equipmentUtilization/views/equipmentInformation/index?id=' + codeData.id,
						});
					} else if (codeData.type == 7) {
						//培训课程
						uni.reLaunch({
							url: '/pages/views/pages_patrolInspector/courseQRcode?code=' + codeData.code,
						});
					} else if (codeData.type == 8) {
						//化学品柜
						uni.reLaunch({
							url: '/pages/views/pages_patrolInspector/chemicalCabinetQRcode?code=' + codeData.code,
						});
					} else if (codeData.type == 9) {
						//化学品
						uni.reLaunch({
							url: '/pages/views/pages_patrolInspector/chemicalDetail?code=' + codeData.code,
						});
					} else if (codeData.type == 1 || codeData.type == 2 || codeData.type == 3) {
						//1.MSDS说明书			2.安全制度			3.危险源详情
						this.$set(this, 'code', codeData.code);
						this.$set(this, 'type', codeData.type);
						this.$set(this, 'webViewType', true);
					} else if (codeData.type == 5) {
						//实验室详情
						this.laboratoryAppletGetSubDetailInfo(codeData.code);
					} else if (codeData.type == 10) {
						uni.showToast({
							mask: true,
							icon: "none",
							position: "center",
							title: '专项检查功能请从安全检查进入',
							duration: 2000
						});
						setTimeout(function() {
							uni.reLaunch({
								url: '/pages/views/home/home',
							});
						}, 2000);
					} else {
						uni.showToast({
							mask: true,
							icon: "none",
							position: "center",
							title: '二维码异常,请联系管理员',
							duration: 2000
						});
						setTimeout(function() {
							uni.reLaunch({
								url: '/pages/views/home/home',
							});
						}, 2000);
					}
				}
			},
			//化学品终端-扫码登录
			async chemicalAioVerifyScanLogin(code, subId, macId) {
				const {
					data
				} = await chemicalAioVerifyScanLogin({
					code: code,
					subId: subId,
					userId: uni.getStorageSync('userId'),
					macId: macId
				});
				uni.showToast({
					mask: true,
					icon: "none",
					position: "center",
					title: data.message,
					duration: 2000
				});
				setTimeout(function() {
					uni.reLaunch({
						url: '/pages/views/mine/mine',
					});
				}, 2000);
			},
			//化学品终端-双人认证
			async chemicalAioVerifyAppletLogin(doorId, subId, macId, code) {
				const {
					data
				} = await chemicalAioVerifyAppletLogin({
					doorId: doorId,
					subId: subId,
					macId: macId,
					code: code,
					userId: uni.getStorageSync('userId'),
				});
				uni.showToast({
					mask: true,
					icon: "none",
					position: "center",
					title: data.message,
					duration: 2000
				});
				setTimeout(function() {
					uni.reLaunch({
						url: '/pages/views/mine/mine',
					});
				}, 2000);
			},
			//电子信息牌二维码-小程序扫码验证
			async laboratoryAppBoardScanCodeVerification(macId, subId, code) {
				const {
					data
				} = await laboratoryAppBoardScanCodeVerification({
					macId: macId,
					subId: subId,
					code: code,
					userId: uni.getStorageSync('userId'),
				});
				uni.showToast({
					mask: true,
					icon: "none",
					position: "center",
					title: data.message,
					duration: 2000
				});
				setTimeout(function() {
					uni.reLaunch({
						url: '/pages/views/mine/mine',
					});
				}, 2000);
			},
			//实验室详情-跳转
			async laboratoryAppletGetSubDetailInfo(infoId) {
				const {
					data
				} = await laboratoryAppletGetSubDetailInfo({
					infoId: infoId
				});
				if (data.code == 200) {
					uni.reLaunch({
						// url: '/pages_manage/views/laboratory/infoPage?infoData=' + encodeURIComponent(JSON.stringify(data.data))+'&saoCode=true'
						url: '/pages_manage/views/laboratory/safetyCardScan?infoData=' + encodeURIComponent(JSON.stringify(data.data))+'&saoCode=true'
					});
				}
			},
		},
	}
</script>

<style lang="stylus" scoped>
	#saoCode {
		overflow scroll
	}
</style>