<template>
  <div>
    <div>企业微信-授权认证中</div>
  </div>
</template>

<script>
import req from '@/request.js'
import utils from '@/utils.js'
import {Notify} from 'vant'
import wx from 'weixin-js-sdk'
export default {
  components: {},
  data() {
    return {
      layout: {},
      gridList: [],
      // code: '',
      // response: '',
      // redirect_uri: '',
      // params: '',
    }
  },
  mounted() {
    if (sessionStorage.getItem('currentUser')) {
      var redirect_uri = decodeURIComponent(utils.getParameters('redirect_uri'))
      if (redirect_uri) {
        location.href = redirect_uri
      } else {
        this.$router.push({path: '/MobileHome/unfinished'})
      }
    }
    const loading = this.$loading({
      lock: true,
      text: '企业微信授权中',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)',
    })

    // 根据企业微信链接分离出需要的参数
    let code = utils.getParameters('code')
    if (!code) {
      loading.close()
      Notify({
        type: 'danger',
        message: '传入的参数错误了',
      })
      return
    } else {
      this.proxyLogin(code, loading)
    }

    // var code = utils.getParameters('code')
    // this.code = code
    // this.params = params

    // var redirect = 'laptop-kkottu8t.com:8080/fvue/qywxOauth'
    // this.redirect_uri = redirect

    // let url = `http://183.230.176.125:8896/sso/weixin?code=${code}`

    // req.get(url).then((response) => {
    //   this.response = response.data
    //   loading.close()

    //   if (this.response) {
    //     sessionStorage.setItem('currentUser', JSON.stringify(response.data))
    //     sessionStorage.setItem('token', response.data.token) // 存入一个值
    //     sessionStorage.setItem('username', response.data.username)
    //     sessionStorage.setItem('account', response.data.account)
    //     sessionStorage.setItem('userId', response.data.userId)
    //     this.$store.dispatch('login/qywxJump', response.data)

    //     this.$router.push({path: '/MobileHome/unfinished'})
    //   }
    // })
    // this.proxyLogin(code, redirect, loading, params)
  },
  methods: {
    proxyLogin(code, loading) {
      let url = window.context.uc + '/sso/weixin?code=' + code
      req.get(url).then((response) => {
        loading.close()
        if (response && response.data) {
          sessionStorage.setItem('currentUser', JSON.stringify(response.data))
          sessionStorage.setItem('token', response.data.token) // 存入一个值
          sessionStorage.setItem('username', response.data.username)
          sessionStorage.setItem('account', response.data.account)
          sessionStorage.setItem('userId', response.data.userId)
          this.$router.push({path: '/MobileHome/unfinished'})
        }
      })
    },
  },
}
</script>
