<template>
</template>

<script>
	import request from '@/network/request.js'
	export default {
			data() {
				return {
					on: true
				}
			},
			methods: {
			},
			onLoad() {
			},
			onShow() {
				// tab除了第一次，后面只触发show，增加on，防止重复触发
				var that = this
				if (that.on) {
					uni.scanCode({
						onlyFromCamera: true,
						scanType: ['qrCode'],
					    success: function (res) {
								console.log("获取二维码数据",res)
							if(res.result){
								let [type,prodKey,devKey] = res.result.split(':')
								if(prodKey&&devKey){
									let opt = {
										url: '/prod-api/iot/device/get/device',
										method: "GET",
									}
									let params={
										devKey,
										pd:prodKey
									}
									// this.$request调用为空，应该是onLaunch时$request还未挂载，直接引入request使用
									request.TokenRequest(opt,params).then(res => {
										console.log('res',res)
										if(res.code==200){
											if(res.data){
												let deviceId=res.data.deviceId;
												uni.navigateTo({
													url:'/pages/device/detail?deviceId='+deviceId,
													success: (res) => {
														uni.showToast({
														    title: '跳转成功',
															icon: 'none'
														});
													},
													fail: (err) => {
														uni.showToast({
														    title: '跳转失败',
															icon: 'none'
														});
													}
												})
											}else{
												uni.showToast({
												    title: '未查询到设备id',
													icon: 'none'
												});
											}
										}else{
											uni.showToast({
											    title: res.msg,
												icon: 'none'
											});
										}
									}, error => {
										uni.showToast({
										    title: '服务器开小差了呢，请您稍后再试',
											icon: 'none'
										});
									})
								}
								
							}
							 
					    }
					});
				}
			},
			onHide: function() {
				console.log("扫一扫onHide")
				this.on = !this.on;
				uni.switchTab({
					url:'./home'
				})
			},
		}
</script>

<style>
</style>
