<template></template>
<script>
import user from '@/http/api/user'
import { setToken,getToken } from '@/utils/auth'
// import { getToken} from '@/utils/auth';
import { encrypt } from '@/utils/jsencrypt'
import base from '@/http/common/base'
export default {
  data() {
    return {}
  },
  mounted() {
    // if(getToken()){
    //   const directUrl = "/studentAnalysisReport/list";
    //   this.$router.push({ path: directUrl });
    //   return false
    // }else{

    // }
    // this.SubmitLogin();
    this.GetUserInfo()
  },
  methods: {
    //登录提交
    SubmitLogin() {
      const code = this.$route.query.code || ''
      const pw = this.$route.query.pw || ''
      const type = this.$route.query.type || '2' //1:单校 2:联校
      if (!code && !pw) {
        window.location.href = base.STUDENT_LOGIN //跳转到教师端登录页面
      }
      sessionStorage.setItem('schoolType', type) //1:单校 2:联校
      const params = {
        password: decodeURI(pw), //解密 encrypt(decodeURI(pw))
        username: code
      }
      user.loginEmailPass(params).then(res => {
        if (res.code === 200) {
          this.$store.dispatch('user/CLEAR_LOCAL_STORAGE') //清空本地存储
          setToken(res.data.tokenValue)

          this.$store.dispatch('user/SET_TOKEN', res.data.tokenValue)
          this.$store.dispatch('user/SET_SCHOOL_LOGO', res.data.schoolLogoUrl) //设置学校logo
          this.$store.dispatch('user/SET_SCHOOL_WEB_SITE_ID', res.data.cloudMonitorSiteId || '') //设置学校网站id
          this.$store.dispatch('user/getUserInfoByToken') //获取用户登录信息

          const directUrl = '/jointStudentAnalysisReport/list';//默认跳转到联校的地址
          this.$router.push({ path: directUrl })
        } else {
          this.$message({
            message: res.msg,
            type: 'warning',
            offset: 100
          })
        }
      })
    },

    //获取用户信息
    GetUserInfo() {
      const messageHandler = event => {
        console.log('打印父窗口传来的数据', event.data)
        console.log('打印父窗口的源', event.origin)
        console.log('打印当前窗口的源', window.location.origin)
        const schoolType = sessionStorage.getItem('schoolType');
        //安全检查 验证消息来源
        if (event.origin !== window.location.origin) {
          return
        }
        if (event.data.type === 'STU_TOKEN') {
          const userInfo = event.data.userInfo;
          const tokenValue = userInfo.tokenValue;
          const oldToken = getToken(); 
          if(oldToken && oldToken === tokenValue && schoolType){
            const currentPath = sessionStorage.getItem('student_currentPath');//获取当前进入的路由
            const directUrl = currentPath?currentPath:'/jointStudentAnalysisReport/list';
            this.$router.push({ path: directUrl })
            return
          }
          this.$store.dispatch('user/CLEAR_LOCAL_STORAGE') //清空本地存储
          setToken(tokenValue)
          sessionStorage.setItem('schoolType', schoolType || 2) //1:单校 2:联校// 暂时写死单校   线上还是单校的
          this.$store.dispatch('user/SET_TOKEN', userInfo.tokenValue)
          this.$store.dispatch('user/SET_SCHOOL_LOGO', userInfo.schoolLogoUrl) //设置学校logo
          this.$store.dispatch('user/SET_SCHOOL_WEB_SITE_ID', userInfo.cloudMonitorSiteId || '') //设置学校网站id

          this.$store.dispatch('user/getUserInfoByToken').then(user => {
            // console.log('获取用户信息成功', this.$store.state.user.userInfo)
            let userInfos = this.$store.state.user.userInfo
            // 移除事件监听器，避免重复处理
            window.removeEventListener('message', messageHandler)
            if (userInfos.phone) {
              const directUrl = '/jointStudentAnalysisReport/list'

              this.$router.push({ path: directUrl })
            } else {
              this.$router.push({ path: '/userInfo/personInfo' })
              // // 使用 this.$confirm 替代
              // this.$confirm('为了方便找回密码，我们建议您绑定手机号!', '', {
              //   confirmButtonText: '确定',
              //   cancelButtonText: '取消',
              //   customClass:"page_dialog",
              //   type: 'warning'
              // }).then(() => {
              //   // 用户点击立即绑定，跳转到个人资料页面

              // }).catch(() => {
              //   // 用户点击稍后绑定，直接进入首页
              //   const directUrl = "/studentAnalysisReport/list";
              //   this.$router.push({ path: directUrl });
              // });
            }
          }) //获取用户登录信息
        }
      }

      // 添加消息监听器
      window.addEventListener('message', messageHandler)
    }
  }
}
</script>

<style lang="scss">
@use '@/styles/login.scss';
</style>
