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

<script>
import { apiDingtalkLuck } 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: '霖梓2020七夕抽奖登记中...，请勿离开！'
      },
      // captchaPath: '' // 图像验证码请求地址
      time: 0,
      timer: true,
      hasSendCode: true
    }
  },
  created () {
    // this.handleGetImgCaptcha()
  },
  mounted () {
    if(this.isDingtalk()){
      
      console.log('走钉钉免登流程')
      this.dingtalkLogin()
      this.checkDingTalkLogin()
    }
  },
  methods: {
    checkDingTalkLogin ()  {
      if(dingCode === null){
        setTimeout(() =>{
          this.checkDingTalkLogin()
        }, 300)
      } 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
          console.log(dingCode)
          
        },
        onFail(err) {
          
          console.log(err)
          dingCode = err
        }
      })
    },
    loginWtihCode (result) {
      console.log('免登流程')
          console.log(result)
          apiDingtalkLuck(result).then(res => {
            console.log('获取登录结果')
            console.log(res)
            this.hasSendCode = false
            if (res && res.code === 0) {
              console.log('获取token')
              
              this.dataForm.verify='已经登记，祝你好运！下午1点44分员工新天地现场开奖'
              
              console.log('跳转')
            } else {
              this.dataForm.verify='无法登记。' + res.msg
              this.$message.error(res.msg)
            }
          })
    }
  }
}
</script>

