<template>
  <view class="login">
    <div class="title">
      <img :src="globalConfig" alt="" class="logo" style="width: 250rpx" />
      <span class="text">钉钉登录认证中...</span>
    </div>
    <u-toast ref="uToast"></u-toast>
  </view>
</template>

<script>
import * as dd from "dingtalk-jsapi";
import { DDSSO } from "@/api/login.js";
import logo from "@/static/logo.png";
import localStorage from "@/utils/localStorage";
export default {
  data() {
    return {
      globalConfig: logo,
      billID: "",
      billKindID: "",
    };
  },
  onReady() {
    this.$ddFunction.setTitle("钉钉免登录认证");
  },
  onShow() {
    const currentUrl = document.location.toString();
    //解析url中包含的corpId
    const obj = this.parseURLParams(currentUrl);
    const corpId = obj.corpid;
    this.billID = obj.billid;
    this.billKindID = obj.billKindID;
    if (corpId == "" || corpId == null || corpId == undefined) {
      this.$modal.showToast("企业id获取失败, 请使用钉钉手机端登录或重新加载");
    } else {
      this.$store.commit("SET_DINGCROPID", corpId);
      this.dingtalkLogin(corpId);
    }
  },
  methods: {
    // 解析URL字符串
    parseURLParams(url) {
      const params = {};
      const paramStr = url.split("?")[1];
      if (paramStr) {
        const paramPairs = paramStr.split("&");
        paramPairs.forEach((pair) => {
          const [key, value] = pair.split("=");
          params[key] = decodeURIComponent(value);
        });
      }
      return params;
    },
    dingtalkLogin(corpId) {
      let that = this;
      if (dd.env.platform !== "notInDingTalk") {
        dd.ready(() => {
          //使用SDK 获取免登授权码
          dd.runtime.permission.requestAuthCode({
            corpId: corpId,
            onSuccess: function (result) {
              let code = result.code;
              that.loginDDSSO(code);
            },
            onFail: (err) => {
              console.info("获取个人信息异常，请稍后重试!", err);
            },
          });
        });
      }
    },
    async loginDDSSO(code) {
      let params = {
        code: code,
      };
      let res = await DDSSO(params);
      if (res.code == 1) {
        localStorage.set("MIDDLEGROUND-TOKEN", res.data[0]);
        localStorage.set("MIDDLEGROUND-USERINFO", res.data[1]);
        // 合同
        if (this.billKindID == "45") {
          let url =
            "/pages/contractManagement/contractList/contractAssistant?billid=" +
            this.billID +
            "&billKindID=" +
            this.billKindID +
            "&type=audit&external=external";
          uni.reLaunch({
            url: url,
          });
        } else if (this.billKindID == "187") {
          //合同交接
          let url =
            "/pages/contractManagement/contractHandover/handoverAssistant?billid=" +
            this.billID +
            "&billKindID=" +
            this.billKindID +
            "&type=audit&external=external";
          uni.reLaunch({
            url: url,
          });
        } else if (this.billKindID == "171") {
          //商机
          let url =
            "/pages/opportunityManagement/opportunityAssistant?billid=" +
            this.billID +
            "&billKindID=" +
            this.billKindID +
            "&type=audit&external=external";
          uni.reLaunch({
            url: url,
          });
        } else {
          uni.switchTab({
            url: "/pages/staging/index",
          });
        }
      } else if (res.code == "1005") {
        if (
          res.data[0] == "" ||
          res.data[0] == null ||
          res.data[0] == undefined
        ) {
          this.$refs.uToast.show({
            type: "warning",
            message: "登录时获取的 code 为空, 请联系管理员处理！",
          });
        } else {
          this.$refs.uToast.show({
            type: "warning",
            message: "您尚未进行登录认证，请先认证！",
          });
          setTimeout(() => {
            localStorage.set("MIDDLEGROUND-USERID", res.data[0]);
            uni.reLaunch({
              url: "/pages/loginAuthen/loginDingDing",
            });
          }, 1000);
        }
      } else {
        // do something
        localStorage.remove("MIDDLEGROUND-TOKEN");
        localStorage.remove("MIDDLEGROUND-USERINFO");
        localStorage.remove("MIDDLEGROUND-USERID");
      }
    },
  },
};
</script>

<style scoped>
.title {
  padding-top: 80rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.title .logo {
  vertical-align: middle;
}
.title .text {
  font-size: 17px;
  font-weight: 700;
  vertical-align: middle;
  padding: 24px;
  color: var(--text-color-light);
}
</style>
<style scoped>
page {
  background: #fff;
}
</style>