<template>
  <div>
    <van-image
      round
      width="8rem"
      height="8rem"
      :src="this.state.user.avatar"
    />
    {{this.state.user.name}}
  </div>
</template>

<script>
import * as dd from "dingtalk-jsapi";
import apiFunc from "@/apis/api";

export default {
  name: "myInfo",
  data(){
    return {
      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.getInfo();
  },
  methods: {
    getInfo() {
      // 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;
                });
              },
              onFail: function (err) {
                alert("免登授权码获取失败: " + JSON.stringify(err));
              },
            })
            .catch((err) => {
              console.log(err + "");
            });
        })
        .catch((err) => {
          alert("获取JS鉴权信息失败: " + JSON.stringify(err));
        });
    },
  },
};
</script>
