<template>
	<view v-if="showCode">
		<view class="code-view">
			<tki-qrcode
			ref="qrcode" 
			:val="val" 
			:size="100" 
			unit="upx" 
			:onval="true" 
			:loadMake="true" 
			:showLoading="false"
			:usingComponents="true"
			/>
		</view>
		<text class="code-text">请使用微信扫码验证</text>
	</view>
</template>

<script>
	import tkiQrcode  from 'tki-qrcode/components/tki-qrcode/tki-qrcode.vue'
	import {createUUID} from '../../utils/uuid.js'
	import * as api from '@/api/index/index.js'
	import webSocket from '@/utils/weSocket.js'
	export default {
		components:{tkiQrcode},
		data() {
			return {
				val:'https://u.wechat.com/MGCFEPwjKx0ZXPaED6LSwB4',
				showCode:false,
				socketMsgQueue:'',
			}
		},
		onLoad() {
			this.checkLogin()
		},
		onShow() {
			//页面每次显示判断是否已经登录（防止websocket断掉），往后端发送请求，看请求是否能通过
			// this.checkLogin();
			// uni.setStorageSync("wxObj", this)
		},
		methods: {
			checkLogin(){
				api.checkLogin().then(res=>{
					if(res.message == 'success'){//已经登录过
						//跳转页面
						uni.redirectTo({
							url:"/pages/store-commodity/store-commodity"
						})
					}else{//未登录，显示二维码，并建立websocket连接
						this.showCode=true;
						this.getWebSocketRes()
					}
				})
			},
			VXCode(val){
				this.showCode = true
				this.val = val
				uni.setStorageSync('id', val)
			},
			loginPage(val){
				uni.setStorageSync('openId', val.openId)
				//登录成功
				uni.redirectTo({
					url:'/pages/menu/menu?id=' + val.menuId
				})
			},
			getWebSocketRes(){
				let socket = webSocket.getWebSocket(this)
				uni.setStorageSync('socket', socket)
			}
		}
	}
</script>

<style>
	
	page{
		background: #f5f5f5;
	}
	
	.code-view{
		position: absolute;
		left: calc(50% - 50upx);
		top: calc(50% - 60upx);
		width: 100upx;
		height: 100upx;
	}
	
	.code-text{
		position: absolute;
		width: 100upx;
		text-align: center;
		font-size:10.41upx;
		left: calc(50% - 50upx);
		top: calc(50% + 60upx);
		display: block;
	}
	
</style>
