<template>
  <div class="homePage">
    <!-- 加载页 -->

    <!-- 首页 -->
    <Index @indexPage="indexPage" />
  </div>
</template>

<script setup>
import { onBeforeMount, onMounted, defineEmits, defineProps, reactive, ref, toRefs, getCurrentInstance, computed } from 'vue'
import gsap from "gsap";
import axios from 'axios'
import { Toast, Dialog } from 'vant';
import { useStore } from "vuex";
// import { get_authorize, get_jssdk } from '@/api/authorize-api'
import service from "@/api/httpServe"
import qs from 'qs'
import h5plugin from '@/utils/plugin'
import Index from "@/components/Index"


// 初始化
const emit = defineEmits(["indexPage"]); // 声明触发事件，对应父组件上面的方法
const props = defineProps({ sendMessage: Object }); // 获取props
const store = useStore()  //初始化vuex
const { proxy } = getCurrentInstance();  //初始化全局方法：plugin工具箱



// 变量定义
const authData = ref('')
const authCode = ref('')


// 页面未挂载
onBeforeMount(() => {
  console.log("link：",window.location.href);

  // 微信环境授权
  if (h5plugin.isWX()) {
    console.log('code:', h5plugin.getQueryString('code'));

    if (h5plugin.getQueryString('code')) {
      // 存在code的话去换取openid
      authCode.value = h5plugin.getQueryString('code')
      getOpenId(authCode.value)
    } else {
      getCode()
    }
  }


  // let token = h5plugin.getQueryString('token')
  // 更新openid
  store.commit({ type: "updateToken", token: h5plugin.getQueryString('token') });
  store.commit({ type: "updateCardId", cardId: h5plugin.getQueryString('cardId') });
  store.commit({ type: "updateNkh", cardId: h5plugin.getQueryString('nkh') });
  console.log("用户活动信息：", store.state.userAccount);


});


// 页面挂载
onMounted(() => {

 });


// 来自首页的事件
const indexPage = (actions) => {
  console.log("来自index组件：", actions);
}


// 授权获取code
const getCode = () => {
  let redirect_uri = window.location.href;
  service.post(process.env.VUE_APP_API + '/zsApi/xfhdAuthorize', qs.stringify({
    redirect_uri: redirect_uri

  })).then((res) => {
    let code = res.data.data
    authCode.value = res.data.data
    window.location.href = res.data.data
  })
}

// 授权获取openid
const getOpenId = (code) => {

  service.post(process.env.VUE_APP_API + '/zsApi/oauthInfo', qs.stringify({
    code: code
  })).then((res) => {

    if (res.data.code == 0) {
      console.log("后端返回的openid：", res.data);
      // 更新openid
      store.commit({ type: 'updateXGLOpenid', openid: res.data.data })
      console.log("用户活动信息2：", store.state.userAccount);


    }

  })
}


</script>

<style lang="scss" scoped>
* {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}


div {
  color: #fff;
}

.homePage {
  font-size: 24px;
  width: 750px;
  max-width: 750px;
  height: 100%;
  overflow: hidden;
  position: absolute;
  top: 0;
  left: 0;
  background: rgb(255, 255, 255);
}
</style>