<template>
  <div>飞书-授权认证中</div>
</template>
<script>
import req from '@/utils/request'
import utils from '@/utils/jump'
import {getTenantInfo} from '@/api/uc.js'
import store from '@/store'
export default {
  components: {},
  props: ['tenant'],
  data() {
    return {
      layout: {},
      gridList: []
    };
  },
  created() {
    const loading = this.$loading({
      lock: true,
      text: "授权中",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.7)"
    });
    let Base64 = require("js-base64").Base64;
    var params = utils.getParameters("params")
    params = decodeURIComponent(params);
    if(params){
      try {
        params = Base64.decode(params);
      } catch (error) {
        loading.close();
        this.$message.error("传入的参数params解码错误了："+error)
        return;
      }
    }

    var code = utils.getParameters("code");
    var redirect = decodeURIComponent(utils.getParameters("redirect"));
    //获取租户信息
    const this_ = this;
    let tenantCode = this.tenant ? this.tenant : 'platform'
    getTenantInfo(tenantCode).then((res) => {
      if (res && res.state) {
        let tenantId = res.value.id
        this_.proxyLogin(code, redirect, loading, params, tenantId);
      }
    })
  },
  methods: {
    proxyLogin(code, redirect_uri, loading,params, tenantId) {
      let url = window.context.auth + "/sso/flybook?code=" + code + "&tenantId=" + tenantId;
      req.get(url).then((response) =>{
        loading.close();
        if (response) {
          store
            .dispatch('user/authentication', response)
            .then(() => {
              this.toRedirectUri(redirect_uri)
            })
            .catch(() => {
              this.$router.push({path:'/home'})
            })
        } else {
          this.toRedirectUri(redirect_uri)
        }
      });
    },
    toRedirectUri(redirect_uri){
      if(redirect_uri){
        location.href = redirect_uri;
      }else{
        if(!params){
          this.$router.push({path:'/home'})
        }else{
          this.parseParams(params);
        }
      }
    },
    parseParams(params){
      let paramsJson = JSON.parse(params||"{}");
      //审批的消息
      if(paramsJson.templateType ==='bpmnapproval'|| paramsJson.templateType ==='bpmnback' || paramsJson.templateType ==='promoter' 
        || paramsJson.templateType ==='bpmntasktrans' || paramsJson.templateType ==='bpmnagent' || paramsJson.templateType ==='bpmtasksetexecutors' 
        || paramsJson.templateType ==='bpmhandto'){
        let url = `/task/${paramsJson.taskId}/0`;
        this.$router.push({path:url});
      } else if(paramsJson.templateType ==='copyto'){
        let url = `/noticeTask/${paramsJson.readId}/COPYTO/0/0/transmittedMatters`;
        this.$router.push({path:url});
      } else if(paramsJson.templateType ==='bpmendprocess' || paramsJson.templateType ==='bpmrenewprocess'){
        let url = `/inst/${paramsJson.instId}/request`;
        this.$router.push({path:url});
      } else{
        this.$router.push({path:'/home'})
      }
    }
  }
};
</script>
