<template>
  <div id="app">
    <div v-show="flag === true">
      <router-view />
    <van-tabbar route v-model="active" @change="onChange">
      <!-- <van-tabbar-item to="/" icon="home-o">消息</van-tabbar-item> -->
      <van-tabbar-item to="/" icon="records">考试</van-tabbar-item>
      <van-tabbar-item to="/index3" icon="completed">计划</van-tabbar-item>
      <van-tabbar-item to="/myInfo" icon="user-circle-o">我的</van-tabbar-item>
    </van-tabbar>
    </div>
    <div v-show="flag === false">
      <span>请在钉钉客户端内进入！</span>
    </div>
  </div>
</template>

<script>
import * as dd from "dingtalk-jsapi";
import { Toast, Lazyload } from "vant";
import apiFunc from "@/apis/api";
import Cookies from "js-cookie";

export default {
  name: "App",
  setup() {
    // const active = ref(0);
    // const onChange = (index) => Toast(`标签 ${index}`);
    return {
      icon,
      active,
      onChange,
    };
  },
  data() {
    return {
      flag :true,
      state: {
        error: null,
        isLoaded: false,
        env: dd.env,
        user: {
          name: "用户名",
          userid: "userid",
          avatar: null,
        },
        authCode: null,
        config: {
          corpId: null,
          agentId: null,
        },
        current: "home",
      },
    };
  },
  created() {
    this.getPhoneInfo();
  },
  methods: {
    getPhoneInfo() {
      // console.log(this.state.env.appType);
      apiFunc
        .getConfig()
        .then((res) => {
          // console.log(res)
          this.state.config = res.data.result;
          let that = this;
          dd.runtime.permission
            .requestAuthCode({
              corpId: that.state.config.corpId,
              onSuccess: function (info) {
                that.state.authCode = info.code;
                apiFunc.login(that.state.authCode).then((res) => {
                  that.state.user = res.data.result;
                  Cookies.set("userName",that.state.user.name, { expires: 30 });
                  Cookies.set("userid",that.state.user.userid, { expires: 30 });
                });
              },
              onFail: function (err) {
                this.flag = false;
                alert("免登授权码获取失败: " + JSON.stringify(err));
              },
            })
            .catch((err) => {
              this.flag = false;
              console.log(err + "");
            });
        })
        .catch((err) => {
          this.flag = false;
          alert("获取JS鉴权信息失败: " + JSON.stringify(err));
        });
    },
  },
};
</script>

<style>
* {
  padding: 0;
  margin: 0;
}
</style>
