

<template>
  <a-config-provider :locale="locale">
    <a-style-provider hash-priority="high">
      <router-view />
    </a-style-provider>
  </a-config-provider>
</template>

<script setup>
import { RouterView } from 'vue-router';
import { onBeforeMount, ref } from 'vue';
import http from "@/utils/auth-api";

import zhCN from 'ant-design-vue/es/locale/zh_CN';
import dayjs from 'dayjs';
import { message } from 'ant-design-vue';
import { useUserStore } from '@/stores';
import { setLocalStorageWithExpiry, getLocalStorageWithExpiry } from '@/utils/store.js'

import 'dayjs/locale/zh-cn';
dayjs.locale('zh-cn');

const locale = ref(zhCN)

const store = useUserStore();
onBeforeMount(() => {
  document.title = window?.AppGlobalConfig?.system?.name || '';
})
const route = useRoute();
const router = useRouter();
const foundHandle = () => {
  router.push({
    name: '404'
  })
}
const ticketStr = ref()
const getUrlParamByToken = () => {
  if (!window?.AppGlobalConfig?.isAuth) return;
  const routePath = route.path
  if (routePath.indexOf('404') !== -1) { return }
  const isLogin = getLocalStorageWithExpiry("_isLogin")
  if(isLogin === '1') return
  var urlStr = window.location.href
  var arr = urlStr.split('?')
  if (arr.length === 1) {
    foundHandle()
    return
  }

  var paramStr = arr[1]
  var paramArr = paramStr.split('&')
  var paramMap = {}
  paramArr.forEach(item => {
    var arr = item.split('=')
    paramMap[arr[0]] = arr[1]
  })
  if (paramMap["debugger"] == "1") {
      return;
  }
  if (!paramMap.hasOwnProperty('sykjzlptv2_ticket')) {
    foundHandle()
    return
  }
  ticketStr.value = paramMap['sykjzlptv2_ticket']
  const storeTicketStr = localStorage.getItem('_sykjzlptv2_ticket')
  if (storeTicketStr) {
    if (storeTicketStr === paramMap['sykjzlptv2_ticket']) {
      return
    }
  }
  localStorage.removeItem('sykjzlptv2_ticket')
  getCommonToken()
}
const getCommonToken = async () => {
  if (!ticketStr.value) { return }
  const urlApi = `/auth/ticket`
  var p = { ticket: ticketStr.value };
  http.get(urlApi, p, this, false).then(async (resultData) => {
    if (!resultData || resultData['error_code']) {
      message.error('用户不合法，请核实用户信息!')
      foundHandle()
      return
    }
    if (resultData['error']) {
      message.error(resultData['error'])
      foundHandle()
      return
    }
    if(resultData['syUser']){
      const syUserObj = resultData["syUser"];
      store.updateUser({
        syUser: syUserObj
      })
      setLocalStorageWithExpiry("_isLogin", "1", 3600000);
      localStorage.setItem("_sykjzlptv2_ticket", ticketStr.value);
    }
  });
}
getUrlParamByToken()
</script>

<style lang="scss" scoped>

::v-deep(div.ant-popconfirm) {
  min-width: 200px;
}
</style>
