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

<script>
	import {
		config
	} from '@/api/request/config.js'
	import {
		chemicalAioVerifyScanLogin,
		chemicalAioVerifyAppletLogin
	} from '@/pages/api/index.js'

	export default {
		name: "saoCode",
		data() {
			return {
				webViewType: false,
				baseUrl: config.base_url,
				code: "",
				type: "",
			}
		},
		onLoad(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.redirectTo({
						url: '/pages/login/login',
					});
				} 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 == 7) {
						//培训课程
						uni.redirectTo({
							url: '/pages/pages_patrolInspector/courseQRcode?code=' + codeData.code,
						});
					} else if (codeData.type == 8) {
						//化学品柜
						uni.redirectTo({
							url: '/pages/pages_patrolInspector/chemicalCabinetQRcode?code=' + codeData.code,
						});
					} else if (codeData.type == 9) {
						//化学品
						uni.redirectTo({
							url: '/pages/pages_patrolInspector/chemicalDetail?code=' + codeData.code,
						});
					} else if(codeData.type == 1 || codeData.type == 2 || codeData.type == 3){
						//type == 1 / 2 / 3 / 4 / 5 / 6
						this.$set(this, 'code', codeData.code);
						this.$set(this, 'type', codeData.type);
						this.$set(this, 'webViewType', true);
					}
				}
			} else {
				let codeData = uni.getStorageSync('codeData');
				uni.removeStorageSync('codeData');
				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 == 7) {
					//培训课程
					uni.redirectTo({
						url: '/pages/views/pages_patrolInspector/courseQRcode?code=' + codeData.code,
					});
				} else if (codeData.type == 8) {
					//化学品柜
					uni.redirectTo({
						url: '/pages/views/pages_patrolInspector/chemicalCabinetQRcode?code=' + codeData.code,
					});
				} else if (codeData.type == 9) {
					//化学品
					uni.redirectTo({
						url: '/pages/views/pages_patrolInspector/chemicalDetail?code=' + codeData.code,
					});
				} else if(codeData.type == 1 || codeData.type == 2 || codeData.type == 3){
						//type == 1 / 2 / 3 / 4 / 5 / 6
						this.$set(this, 'code', codeData.code);
						this.$set(this, 'type', codeData.type);
						this.$set(this, 'webViewType', true);
					}
			}
		},
		mounted() {

		},

		methods: {
			//化学品终端-扫码登录
			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.redirectTo({
						url: '/pages/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.redirectTo({
						url: '/pages/mine/mine',
					});
				}, 2000);
			},
		},
	}
</script>

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