<template>
  <router-view />
</template>
<script setup lang="ts">
  // import 'vant/lib/index.css';
  import { onMounted, computed } from 'vue';
  import { useUserStore } from '/@/store/modules/user';
  import { useCookies } from '@vueuse/integrations/useCookies';
  const userStore = useUserStore();
  const wxOpenId = computed(() => {
    return userStore.getWxOpenId;
  });
  onMounted(async () => {
    let code = getUrlKey('code'); //获取url参数code
    if (wxOpenId.value) {
      //
      console.log('已登录', wxOpenId.value);
    } else if (code) {
      //存在code
      userStore.setWxOpenId(code);
    } else {
      //进行微信登录
      getCodeApi(123);
    }
    let clear = getUrlKey('clear'); //获取url参数code
    if(clear){
      useCookies().set('wxOpenId', '')
    }
  });
  function getUrlKey(name) {
    //获取url 参数
    return (
      decodeURIComponent(
        (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20'),
      ) || null
    );
  }
  function getCodeApi(state) {
    //获取code
    let urlNow = encodeURIComponent(window.location.href);
    let scope = 'snsapi_base'; //snsapi_userinfo   //静默授权 用户无感知
    let appid = 'wxac3d59ea82d9b82a';
    let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
    window.location.replace(url);
  }
</script>
<style>
  #app {
    font-family: PingFang SC-Regular, PingFang SC;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: #2c3e50;
    background-color: var(--color-bg-1);
  }

  .van-toast {
    background: rgba(0, 0, 0, 0.7) !important;
  }
</style>
