<template>
    
    <div class="divClass">认证中...</div>
</template>


<script>

import AuthSettingResource from '../api/commom/AuthSettingResource.js';


export default {

  components: {
    
  },
  data: function () {
    return {

      isUniqueLoginModel: false, //唯一认证方式
    }
  },

  
  mounted: function () {

    this.getIsUniqueLoginModel();
    this.initData();
  },

  
  methods: {

    /**
     * 初始化数据
     * @author GuoZhiBo 20200218
     */
    initData: function () {
      var _self = this;

      _self.appName = _self.$route.query.appName;
      console.log(_self.$route.query);
      console.log(_self.appName);
      if (
        _self.appName != undefined &&
        window.dd != undefined &&
        window.dd.env.platform != 'notInDingTalk'
      ) {
        _self.corpId = _self.$route.query.corpId;
        _self.clientId = _self.$route.query.clientId;
        _self.type = 'Ding';
        _self.getDingDingConfig();
      } else {
        _self.type = 'Browser';
      }
    },
    /**
     * 获取钉钉免登Config
     */
    getDingDingConfig: function () {
      var _self = this;
      //获取临时登录码
      window.dd.ready(function () {
        window.dd.runtime.permission.requestAuthCode({
          corpId: _self.corpId, // 企业id
          onSuccess: function (info) {
            _self.code = info.code; // 通过该免登授权码可以获取用户身份
            _self.dingDingAutoLogin();
          },
        });
      });
    },
    /**
     * 钉钉自动登录
     */
    dingDingAutoLogin: function () {
      var _self = this;
      $.ajax({
        url:
          Common.getApiUrl2('authApi/LoginResource/dingTalkAutoLogin'),
        data: {
          code: _self.code,
          clientId: _self.clientId,
          appName: _self.appName,
          languageId: _self.languageSelected,
        },
        type: 'GET',
        dataType: 'json',
        success: function (loginInfo) {
          Common.clearLocalStorage();

          // path为指定路径，直接删除该路径下的cookie
          // $.removeCookie('token', { path: '/' });

          // bug fixed by jack
          // 清除掉/pcapp路径的cookie
          // $.removeCookie('token', { path: '/pcapp' });

          // $.cookie('token', loginInfo.token, { expires: 7, path: '/', secure: true, sameSite: 'Strict' });

          $('#input-pwd').attr('type', 'text').val('');
          if (_self.redirectUrl != '' && _self.redirectUrl.length > 0) {
            window.location = _self.redirectUrl;
          } else {
            _self.$router.push('/desktop/dashboard');
          }
          loginInfo.accountName = _self.accountItem.accountName;
          LoginService.setLoginInfo(loginInfo.data, _self.$router);
          LoginService.saveLocalStorage(_self);
          _self.saveLocalStorage(loginInfo);
          _self.setTokenClient();
        },
        error: function (e) {
          _self.isDing = 'Browser';
        },
      });
    },

    
    /**
     * 设置TokenClient
     * @param {Object} TokenClient
     */
    setTokenClient: function(){
      TokenClientResource.saveTokenClient().then(
        successData => {
          if (successData != null) {
            console.log(successData);
          }
        },
      );
    },

  }

}
</script>

<style scoped>
.divClass {
  text-align: center;
  font-size: 30px;
  color: red;
}
</style>