<template>
  <service-basekq :centerNo="centerNo" v-if="centerNo"></service-basekq>
</template>

<script>
import ServiceBasekq from './ServiceBaseKQ.vue'
const userData = require('@/views/answerRank/data.json')
export default {
  name: 'ServiceKQ',
  data() {
    return {
      centerNo: '',
    }
  },
  mounted() {
    console.log('xst mounted')
    this.checkAuth()
    //二次回退
    const sUserAgent = window.navigator.userAgent.toLowerCase()
    const bIsAlipayMini = sUserAgent.indexOf('miniprogram') > -1 && sUserAgent.indexOf('alipay') > -1
    if (bIsAlipayMini) {
      console.log('我来自支付宝,popstate')
      window.addEventListener('popstate', this.listenerPopstate, true)
    } else {
      console.log('我来自浙里办,popstate')
      window.addEventListener('popstate', this.listenerPopstate, true)
    }
  },
  //销毁监听
  destroyed() {
    console.log('销毁支付宝监听')
    window.removeEventListener('popstate', this.listenerPopstate, true)
  },
  methods: {
    //支付宝二次回退方法，addEventListener和removeEventListener一定要调用方法，不然无效
    listenerPopstate() {
      ZWJSBridge.close()
    },
    checkAuth() {
      let centerNo = localStorage.getItem('centerNo')
      let ticket = this.common.getUrlParam('ticket') //浙里办app访问
      let ticketId = this.common.getUrlParam('ticketId') //浙里办微信小程序访问
      if (centerNo) {
        // alert('有中台编号：' + centerNo)
        this.centerNo = centerNo
        localStorage.setItem('centerNo', centerNo)
        this.getUserInfo()
      } else if (ticket && ticket.indexOf('debug') == -1) {
        // ZWJSBridge.alert({
        //   message: '有ticket：' + ticket,
        // })
        this.getCenterByTicket(ticket)
      } else if (ticketId && ticketId.indexOf('debug') == -1) {
        // ZWJSBridge.alert({
        //   message: '有ticketId：' + ticketId,
        // })
        this.getCenterByTicketId(ticketId)
        localStorage.setItem('isWechat', 1)
      } else {
        const sUserAgent = window.navigator.userAgent.toLowerCase()
        const dtdreamweb = sUserAgent.indexOf('dtdreamweb') > -1
        const miniprogram = sUserAgent.indexOf('miniprogram') > -1 && sUserAgent.indexOf('alipay') > -1
        if (dtdreamweb) {
          // alert('浙里办')
          window.location.href = `https://puser.zjzwfw.gov.cn/sso/mobile.do?action=oauth&scope=1&servicecode=82a7a71edb794fc285895f9e33290ddc`
        } else if (miniprogram) {
          // alert('支付宝')
          window.location.href = `https://puser.zjzwfw.gov.cn/sso/alipay.do?action=ssoLogin&servicecode=82a7a71edb794fc285895f9e33290ddc`
        } else {
          alert('非浙里办渠道访问，显示测试用户数据')
          console.log('非浙里办渠道访问，显示测试用户数据', userData)
          this.centerNo = '2022031508525674030748540013076'
          localStorage.setItem('centerNo', '2022031508525674030748540013076')
          let userInfo = JSON.stringify(userData)
          localStorage.setItem('userInfo', userInfo)
          this.getUserInfo()
        }
      }
    },
    // 通过ticket获取centerNo
    getCenterByTicket(ticket) {
      this.yxAxios.get(`${this.proxyUrl}/api/third/getCenterNo?ticket=${ticket}`).then((res) => {
        if (res.data.code == 200) {
          console.log('通过ticket获取centerNo成功:', res.data)
          this.centerNo = res.data.message
          localStorage.setItem('centerNo', res.data.message)
          this.getUserInfo()
        } else {
          console.log('通过ticket获取centerNo失败:', res.data.code)
          this.reLoad()
        }
      })
    },
    // 通过ticketId获取centerNo
    getCenterByTicketId(ticketId) {
      // alert(ticketId)
      this.yxAxios.get(`${this.proxyUrl}/api/third/getCenterNoWx?ticket=${ticketId}`).then((res) => {
        if (res.data.code == 200) {
          console.log('通过ticketId获取centerNo成功:', res.data)
          this.centerNo = res.data.message
          localStorage.setItem('centerNo', res.data.message)
          this.getUserInfo()
        } else {
          console.log('通过ticketId获取centerNo失败:', res.data.code)
          this.reLoad()
        }
      })
    },
    reLoad() {
      console.log('reload')
      const sUserAgent = window.navigator.userAgent.toLowerCase()
      const dtdreamweb = sUserAgent.indexOf('dtdreamweb') > -1
      const miniprogram = sUserAgent.indexOf('miniprogram') > -1 && sUserAgent.indexOf('alipay') > -1
      // if (localStorage.getItem('reloadCount') == 2) {
      //   localStorage.setItem('reloadCount', 1)
      //   ZWJSBridge.confirm({
      //     title: '警告',
      //     buttonLabels: ['重新载入'],
      //     message: '用户身份验证失败，请重新加载',
      //   }).then((res) => {
      //     if (dtdreamweb) {
      //       // alert('浙里办')
      //       window.location.href = `https://puser.zjzwfw.gov.cn/sso/mobile.do?action=oauth&scope=1&servicecode=82a7a71edb794fc285895f9e33290ddc`
      //     } else if (miniprogram) {
      //       // alert('支付宝')
      //       window.location.href = `https://puser.zjzwfw.gov.cn/sso/alipay.do?action=ssoLogin&servicecode=82a7a71edb794fc285895f9e33290ddc`
      //     } else {
      //       // alert('浙里办')
      //       window.location.href = `https://puser.zjzwfw.gov.cn/sso/mobile.do?action=oauth&scope=1&servicecode=82a7a71edb794fc285895f9e33290ddc`
      //     }
      //   })
      // } else {
      //   localStorage.setItem('reloadCount', 2)
      if (miniprogram) {
        // alert('支付宝')
        window.location.href = `https://puser.zjzwfw.gov.cn/sso/alipay.do?action=ssoLogin&servicecode=82a7a71edb794fc285895f9e33290ddc`
      } else {
        // alert('浙里办')
        window.location.href = `https://puser.zjzwfw.gov.cn/sso/mobile.do?action=oauth&scope=1&servicecode=82a7a71edb794fc285895f9e33290ddc`
      }
      // }
    },
    // 获取用户信息
    getUserInfo() {
      this.centerNo = localStorage.getItem('centerNo')
      // alert('getUserInfo:' + this.centerNo)
      this.yxAxios.post(`${this.proxyUrl}/user/info/getPortalUserByNumV2?userNum=${localStorage.getItem('centerNo')}`).then((res) => {
        console.log('获取用户信息getPortalUserByNum:', res)
        if (res.data.code == 200) {
          let userInfo = res.data.data.userInfo
          localStorage.setItem('userInfo', JSON.stringify(userInfo))
          this.userInfo = userInfo
          localStorage.setItem('_user_nick', userInfo.travelerName)
          this.$zwlogPvGlobal({ loadTime: new Date() })
          this.$nextTick(() => {
            this.$zwlogPvGlobal({ responseTime: new Date() })
          })
        }
      })
    },
  },
  components: {
    ServiceBasekq,
  },
}
</script>
