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

<script>
import { apiDingtalkLogin } from '@/api/api_staff'
import {apiDingtalkJump } from '@/api/api_staff'
import * as dd from 'dingtalk-jsapi'
var dingCode = null
export default {
  data () {
    return {
      isloading: false,
      dataForm: {
        userName: '',
        password: '',
        uuid: '',
        // captcha: ''
        verify: '身份验证中...'
      },
      req: {
        code: ''
      },
      // captchaPath: '' // 图像验证码请求地址
      time: 0,
      timer: true,
      hasSendCode: true,
      tick: 1
    }
  },
  created () {
    // this.handleGetImgCaptcha()
  },
  mounted () {
    if(this.isDingtalk()){
      if(typeof this.$route.query.code == 'undefined'){
        console.log('走钉钉免登流程')
        this.dingtalkLogin()
        this.checkDingTalkLogin()
      } else {
        this.loginJump()
      }
    } else {
      
        this.loginJump()
    }
  },
  methods: {
    loginJump () {
      this.dataForm.verify = '验证中'
      console.log(this.$route.query)
      this.req.code = this.$route.query.code
      apiDingtalkJump(this.req).then(res => {
            
            console.log(res)
            if (res && res.code == 200) {
              console.log('获取token')
              this.$cookie.set('token', res.token)
              window.location.href=this.$route.query.url

              //window.open(window.location.href + '&code=' + dingCode.code, "_blank")
              console.log('跳转')
            } else {
              this.dataForm.verify='身份验证失败' + res.msg
              this.$message.error(this.dataForm.verify)
              this.$router.replace({ name: 'login' })
            }
            this.hasSendCode = false;
          })
    },
    checkDingTalkLogin ()  {
      if(dingCode === null){
        if(this.tick < 30){
          this.isTick++
          setTimeout(() =>{
            this.checkDingTalkLogin()
          }, 300)
        }else{
          this.$message.error('登录失败，请用账户和密码登录')
          this.$router.replace({ name: 'login' })
        }
        
      } else {
        this.loginWtihCode(dingCode)
      }
    },
    isDingtalk () {
      if (/(DingTalk)/i.test(navigator.userAgent)) {
        return true
      } else {
        return false
      }
    },
    dingtalkLogin () {
      dd.runtime.permission.requestAuthCode({
        corpId: 'ding267474e759bbca1c35c2f4657eb6378f',
        onSuccess(result){
          
          dingCode = result
          
        },
        onFail(err) {
          
          console.log(err)
          dingCode = err
        }
      })
    },
    loginWtihCode (result) {
      console.log('免登流程')
          console.log(result)
          apiDingtalkLogin(result).then(res => {
            console.log('获取登录结果')
            console.log(res)
            this.hasSendCode = false
            if (res && res.code == 200) {
              console.log('获取token')
              this.$cookie.set('token', res.token)
              this.dataForm.verify='登录成功，跳转中...'
              window.open(window.location.href + '&code=' + dingCode.code, "_blank")
              console.log('跳转')
            } else {
              this.dataForm.verify='身份验证失败' + res.msg
              this.$message.error(this.dataForm.verify)
              this.$router.replace({ name: 'login' })
            }
          })
    }
  }
}
</script>
