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

<script>
import * as dd from "dingtalk-jsapi";
import { authApi } from "@/api/login";
import logo from "@/static/logo1.png";
import localStorage from "@/utils/localStorage";
export default {
  data() {
    return {
      globalConfig: logo,
    };
  },
  onReady() {
    this.$ddFunction.setTitle("钉钉免登录认证");
  },
  onShow() {
    const currentUrl = document.location.toString();
    //解析url中包含的corpId
    const corpId = currentUrl.split("corpid=")[1];
    if (corpId == "" || corpId == null || corpId == undefined) {
      this.$modal.showToast("企业id获取失败, 请使用钉钉手机端登录或重新加载");
    } else {
      this.$store.commit("SET_DINGCROPID", corpId);
      this.dingtalkLogin(corpId);
    }
  },
  methods: {
    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,
        appType: "DD", //移动端类型
        appId: "800043", //请求的APIID  默认
        apiCode: "8000430000", //请求的API编码  默认
        userApiCode: "8000430004", //请求认证的API编码  默认
      };
      let res = await authApi("loginService", "", "appDoLogin", "", params);
      if (res.status === "200") {
        localStorage.set("MIDDLEGROUND-TOKEN", res.attribute.token);
        localStorage.set("MIDDLEGROUND-USERINFO", res.attribute.userInfo);
        uni.switchTab({
          url: "/pages/homePage/index",
        });
      } else if (res.status == "1005") {
        if (
          res.attribute.userid == "" ||
          res.attribute.userid == null ||
          res.attribute.userid == undefined
        ) {
          this.$refs.uToast.show({
            type: "warning",
            message: "登录时获取的 code 为空, 请联系管理员处理！",
          });
        } else {
          this.$refs.uToast.show({
            type: "warning",
            message: "您尚未进行登录认证，请先认证！",
          });
          setTimeout(() => {
            localStorage.set("MIDDLEGROUND-USERID", res.attribute.userid);
            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;
}
.logo-content {
  background: #c73b26;
  border-radius: 10px;
  padding: 15px;
}
.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>