<script setup>
import { onMounted, ref } from 'vue'
import { RouterLink, RouterView, useRoute } from 'vue-router'
import Header from './views/header.vue';
import axios from 'axios';
import qs from 'qs';
import { userInfoStore,userCurrentRole } from './stores/counter'

const route = useRoute()
const store = userInfoStore()
const CurrentRole = userCurrentRole()
onMounted(() => {
  console.log(11, window.parent.location.href)
  // 通过 grant_code 获取登录信息
  let isGrantCode = getUrlParam(window.parent.location.href, 'grant_code')
  axios.defaults.headers = { 'content-type': 'application/json;charset=utf-8' }
  if (isGrantCode) {
    let params = {
      grantCode: isGrantCode,
      redirectUri: "https://cloud.cocorobo.cn/testapi"
    }
    axios.post('//cloud.cocorobo.cn/api/getAccessToken', JSON.stringify(params)).then(res => {
      console.log(res)
      let accessTokenParams = {
        accessToken: res.data.data.accessToken
      }
      axios.post('//cloud.cocorobo.cn/api/getUserInfo', JSON.stringify(accessTokenParams)).then(r => {
        console.log(r, r.data.code, r.data.data)
        if (r.data.code == 0) {
          store.user = r.data.data
          userInfo(r.data.data)
        } else {
        }
      })
    })
  }
})

const linkLogin = () => {
  window.location.href = `http://szedutest.changyan.cn/thirdauth/oauth2/authorize?service=initService&scope=all&response_type=code&app_id=94c720f0634b4bad890dc9223de01166&redirect_uri=https://cloud.cocorobo.cn/testapi`
}

// 同步登录信息
const userInfo = (data) => {
  let userIdentity = 2
  if (data.currentRole == "areaAdministrator" || data.currentRole == "userAdministrator" || data.currentRole == "securityAuditor" || data.currentRole == "schoolAdministrator" || data.currentRole == "schoolSecurityAuditor" || data.currentRole == "teacher") {
    CurrentRole.currentRole == data.currentRole
    userIdentity = 1
  }
  let userGrand = ""
  if (data.childList && data.childList.length > 0) {
    userGrand = data.childList[0].gradeName + data.childList[0].className
  }
  let params = {
    openid: "fb1ca3e74fb4236ae95d0c670f65487114e0e3878759bc46d907d8ef61b568d09f5543fc67f61aa7",
    edu: true
  }
  axios.defaults.headers = { 'content-type': 'application/x-www-form-urlencoded' }
  axios.defaults.withCredentials = true;
  axios.post(`https://beta.api.cocorobo.cn/api/user`, qs.stringify(params)).then(res => {
    console.log(res, '222222222222')

    if (res.data && res.data[0][0].active == 1) {
    } else {
      top.U.A.Request("https://pbl.cocorobo.cn/api/pbl/batchRegistrationSzdjg", [data.userName, data.telephoneNumber + '@szdjg.com', '16ace517-b5c7-4168-a9bb-a9e0035df840', data.schoolName, data.telephoneNumber, userGrand, userIdentity, data.userId], function (r) {
        console.log('rrrrr', r)
        axios.post(`https://beta.api.cocorobo.cn/api/user`, qs.stringify(params)).then(d=>{
          console.log(d, 'ddddddd')
        })
      }, [], { "type": "POST", "withCredentials": true });
    }
  })
}

const getUrlParam = (urlStr, urlKey) => {
  const url = new URL(urlStr) // 字符串转换成url格式
  const paramsStr = url.search.slice(1) // 获取'?'后面的参数字符串
  const paramsArr = paramsStr.split('&') // 分割'&'字符 获得参数数组
  for (let i = 0; i < paramsArr.length; i++) {
    const tempArr = paramsArr[i].split('=')
    if (tempArr[0] === urlKey) {
      return tempArr[1]
    }
  }
}


</script>

<template>
  <!-- <keep-alive> -->
  <router-view></router-view>
  <!-- </keep-alive> -->
</template>

<style scoped></style>
