<template>
  <div>
    <h2 class="brand-info__text">{{dataForm.verify}}</h2>
  </div>
</template>

<script>
import * as dd from 'dingtalk-jsapi'
import { apiDingtalkLogin } from '@/api/login'

export default {
  data () {
    return {
      dataForm: {
        verify: '身份验证中...'
      },
      dingCode: '' // 获取钉钉授权码
    }
  },
  created () {
    this.handleGetDingtalkCode()
  },
  mounted () {},
  methods: {
    // 获取钉钉鉴权码
    handleGetDingtalkCode () {
      let _this = this
      dd.runtime.permission.requestAuthCode({
        corpId: 'ding267474e759bbca1c35c2f4657eb6378f',
        onSuccess (res) {
          console.log('result===', res)
          _this.handleDingtalkLogin(res)
          _this.dingCode = res
        },
        onFail (err) {
          console.log('err===', err)
          _this.dingCode = err
        }
      })
    },
    // 钉钉免登录
    handleDingtalkLogin (val) {
      apiDingtalkLogin(val).then(res => {
        if (res && res.code === 0) {
          this.$cookie.set('token', res.token)
          this.dataForm.verify = '登录成功，跳转中...'
          window.open(window.location.protocol + '//' + window.location.host + '/digitization/kpi/workbench/home?code=' + this.dingCode.code)
        } else {
          this.dataForm.verify = '身份验证失败' + res.msg
          this.$message.error(this.dataForm.verify)
          this.$router.replace({ name: 'dingTalkLogin' })
        }
      })
    }
  }
}
</script>
