<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()
  },
  activated() {
    console.log('KQ activated')
    this.checkAuth()
  },
  methods: {
    checkAuth() {
      let centerNo = localStorage.getItem('centerNo')
      let ticket = this.common.getUrlParam('ticket') //浙里办app访问
      let ticketId = this.common.getUrlParam('ticketId') //浙里办微信小程序访问
      // alert(location.href)
      // alert(ticketId)
      if (ticket) {
        this.getCenterByTicket(ticket)
      } else if (ticketId) {
        // alert('getCenterByTicketId')
        this.getCenterByTicketId(ticketId)
        localStorage.setItem('isWechat',1)
      } else if (centerNo) {
        // alert('有中台编号：' + centerNo)
        this.centerNo = centerNo
        localStorage.setItem('centerNo', centerNo)
        this.getUserInfo()
      } 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 = '2021111017103639979048000010279'
          localStorage.setItem('centerNo', '2021111017103639979048000010279')
          let userInfo = JSON.stringify(userData)
          localStorage.setItem('userInfo', userInfo)
          this.getUserInfo()
        }
      }
    },
    // 通过ticket获取centerNo
    getCenterByTicket(ticket) {
      this.mgop({
        api: 'mgop.sz.hswsy.getCenterNo', // 必须
        host: 'https://mapi.zjzwfw.gov.cn/',
        dataType: 'JSON',
        type: 'GET',
        appKey: 'fuxgnukl+2001895516+edccpx', // 必须
        headers: {
          //   'isTestUrl': '1'
        },
        data: {
          ticket: ticket,
        },
        onSuccess: (res) => {
          console.log('通过ticket获取centerNo成功:', res)
          // alert('通过ticket获取centerNo成功:' + res.data.message)
          if (res.data.code == 200) {
            this.centerNo = res.data.message
            localStorage.setItem('centerNo', res.data.message)
            this.getUserInfo()
          } else {
            this.reLoad()
          }
        },
        onFail: (err) => {
          // alert('通过ticket获取centerNo失败:' + JSON.stringify(err))
          console.log('通过ticket获取centerNo失败:', JSON.stringify(err))
          this.reLoad()
        },
      })
    },
    // 通过ticketId获取centerNo
    getCenterByTicketId(ticketId) {
      // alert(ticketId)
      this.mgop({
        api: 'mgop.sz.hswsy.getCenterNoWx', // 必须
        host: 'https://mapi.zjzwfw.gov.cn/',
        dataType: 'JSON',
        type: 'GET',
        appKey: 'fuxgnukl+2001895516+edccpx', // 必须
        headers: {
          //   'isTestUrl': '1'
        },
        data: {
          ticket: ticketId,
        },
        onSuccess: (res) => {
          // alert('通过ticketId获取centerNo Success:' + res.data.message)
          if (res.data.code == 200) {
            // this.centerNo = res.data.message
            localStorage.setItem('centerNo', res.data.message)
            this.getUserInfo()
          } else {
            this.getUserInfo()
          }
        },
        onFail: (err) => {
          // alert('通过ticket获取centerNo Fail:' + JSON.stringify(err))
          // this.reLoad()
        },
      })
    },
    reLoad() {
      console.log('reload')
      ZWJSBridge.confirm({
        title: '警告',
        buttonLabels: ['重新载入'],
        message: '用户身份验证失败，请重新加载',
      }).then((res) => {
        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=hswsy`
        } else if (miniprogram) {
          // alert('支付宝')
          window.location.href = `https://puser.zjzwfw.gov.cn/sso/alipay.do?action=ssoLogin&servicecode=hswsy`
        }
      })
    },
    // 获取用户信息
    getUserInfo: function() {
      this.centerNo = localStorage.getItem('centerNo')
      // alert('getUserInfo:' + this.centerNo)
      this.mgop({
        api: 'mgop.sz.hswsy.getPortalUserByNum', // 必须
        // host: 'https://mapi.zjzwfw.gov.cn/',
        // dataType: 'JSON',
        // type: 'GET',
        appKey: 'fuxgnukl+2001895516+edccpx', // 必须
        headers: {
          //   'isTestUrl': '1'
        },
        data: {
          userNum: localStorage.getItem('centerNo'),
        },
        onSuccess: (res) => {
          // alert('getUserInfo success:' + JSON.stringify(res))
          if (res.data.code == 200) {
            localStorage.setItem('userInfo', JSON.stringify(res.data.data.userInfo))
          }
        },
        onFail: (err) => {
          // alert('getUserInfo fail:' + JSON.stringify(err))
        },
      })
    },
  },
  components: {
    ServiceBasekq,
  },
}
</script>
