<template>
  <div id="app">
    <router-view />
    <theme-picker />
  </div>
</template>

<script>
import ThemePicker from "@/components/ThemePicker";

import { authToken } from "@/api/login";
import { openAuth } from "dingtalk-design-libs/biz/openAuth";
import { setToken } from "@/utils/auth";
import * as dd from "dingtalk-jsapi"; // 在此引入
export default {
  name: "App",
  components: { ThemePicker },
  created() {
    let that = this;
    // 判断是否是移动端
    let isMobileEnv = this.isMobile();
    // 储存
    this.$store.commit("app/SET_IS_MOBILE_ENV", isMobileEnv);
    // 取值-同步
    console.log("是否是移动端=", this.$store.state.app.isMobileEnv);
    if (dd.env.platform !== "notInDingTalk") {
      dd.ready(function () {
        // 钉钉免登录认证
        // dd.getAuthCode({
        //   corpId: "ding4ab75ecd53106cde4ac5d6980864d335",
        //   success: (res) => {
        //     console.log("res=", res);
        //     avoidLogin(res.code).then((res) => {
        //       console.log("===avoidLogin_res=",res)
        //     });
        //   },
        //   fail: (err) => {
        //     console.log("err=", err);
        //   },
        //   complete: () => {},
        // });
        // 唤起授权
        openAuth({
          clientId: "dingwlimimzllguvqf8x", // 应用ID(唯一标识)
          corpId: "ding4ab75ecd53106cde4ac5d6980864d335", // 当前组织的corpId
          rpcScope: "Contact.User.Read", //通讯录
          fieldScope: "Contact.User.mobile", //手机号
          type: 0, // 0 标识授权个人信息；1 标识授权组织信息
        }).then((res) => {
          // 处理返回数据
          console.log("免登res=", res);
          authToken(res.result.authCode).then((res) => {
            let userInfo = res.sysUser;
            setToken(res.token);
            that.$store.commit("SET_TOKEN", res.token);
            console.log("----钉钉环境----");
            that.toTargetPage();

            // userInfo.nick
            // userInfo.mobile
            // userInfo.avatarUrl
            // openId  unionId
          });
        });
      });
    }
  },
  metaInfo() {
    return {
      title:
        this.$store.state.settings.dynamicTitle &&
        this.$store.state.settings.title,
      titleTemplate: (title) => {
        return title
          ? `${title} - ${process.env.VUE_APP_TITLE}`
          : process.env.VUE_APP_TITLE;
      },
    };
  },
  methods: {
    //判断是否是移动端
    isMobile() {
      return (
        navigator.userAgent.match(
          /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
        ) !== null
      );
    },
    // 判断是否是移动端跳转
    toTargetPage() {
      let that = this;
      // 判断客户端环境
      let isMobileEnv = this.$store.state.app.isMobileEnv;
      console.log("app=====isMobileEnv=", isMobileEnv);
      if (isMobileEnv) {
        that.$router.replace({
          path: that.redirect || "/mobile/resume/index",
        });
      } else {
        that.$router.push({
          path: that.redirect || "/",
          query: { type: "admin", title: "智能简历" },
        });
      }
    },
  },
};
</script>
<style scoped>
#app .theme-picker {
  display: none;
}
</style>
