<script setup>
import { onMounted } from "vue";
import utils from './utils/lib'
import {getToken, removeToken} from './utils/auth'
import { showNotify } from "vant";
import { useRouter } from "vue-router";

const router = useRouter()

onMounted(() => {
  utils.ConnectSocket()
  utils.SockEventMap.__Error_Event__ = msg => {
    showNotify({
      message: msg,
      type: 'danger',
      duration: 2000
    })
  }
  utils.SockEventMap.secrets = data => {
    utils.EncryptHandler.setPublicKey(data.public)
    utils.EncryptHandler.setPrivateKey(data.private)
  }
  utils.SockEventMap.tokenExpired = () => {
    removeToken()
    this.$router.push(`/login?redirect=${this.$route.fullPath}`)
  }
  utils.SockEventMap.onePageOnly = () => {
    showNotify({
      message: '不支持页面多开',
      type: 'warning',
    })
  }
})

// router.beforeEach((to, from, next) => {
//   const hasToken = getToken()
//   if (hasToken) {
//     next()
//   }else {
//     next({ path: '/login', replace: true })
//   }
// })
</script>

<template>
    <router-view></router-view>
</template>

<style scoped>
</style>
