<template>
    <div>
        <!-- <button @click="csToken">获取钉钉授权码</button> -->
    </div>
</template>

<script>
import * as dd from 'dingtalk-jsapi';
import { getCode } from "@/api/ddLogin";
import router from "../router";
import { getToken, setToken, removeToken } from '@/utils/auth'

export default {
    data() {
        return {
            redirect: "/index", // 这里假设要跳转到首页
            code: null,
            token: null,

        };
    },
    created() {
        // 钉钉code码，正式时打开
        this.getCode()
        //  token,测试用
        // this.csToken()
    },
    methods: {
        // 获取code
        getCode() {
            dd.getAuthCode({
                //测试
                // corpId: 'dingaa3937ff8b7dd267f2c783f7214b6d69',
                //正式
                corpId: 'dingccea40788226c988f2c783f7214b6d69', 
                success: (res) => {
                    console.log('获取新的免登码成功', res);
                    const code = res.code
                    // this.code = code;
                    // this.authCodeTimestamp = currentTime;
                    // console.log('this.authCodeTimestamp', this.authCodeTimestamp);
                    console.log('code', code);
                    this.getToken(code)
                    // console.log('loginResponse', loginResponse);
                },
                fail: (res) => {
                    console.log('获取免登码失败', res);
                },
                complete: () => { },
            });
        },
        // 获取token
        getToken(code) {
            getCode(code).then(res => {
                console.log('res', res.token);
                if (res.token) {
                    setToken(res.token)
                    this.$store.commit('SET_TOKEN', res.token)
                    this.$router.push({ path: '/index' });
                    // 判断当前用户是否已拉取完user_info信息
                    this.$store.dispatch('GetInfo').then((res) => {
                        this.$store.dispatch('GenerateRoutes').then(accessRoutes => {
                            // 根据roles权限生成可访问的路由表
                            router.addRoutes(accessRoutes) // 动态添加可访问路由表
                            // this.init()

                        })
                    })
                } else {
                    this.$message.error(res.message)
                }

            })
        },
        // 测试token
        csToken() {
            getCode(123).then(res => {
                console.log('res', res.token);
                if (res.token) {
                    setToken(res.token)
                    this.$store.commit('SET_TOKEN', res.token)
                    this.$router.push({ path: '/index' });
                    // 判断当前用户是否已拉取完user_info信息
                    this.$store.dispatch('GetInfo').then((res) => {
                        this.$store.dispatch('GenerateRoutes').then(accessRoutes => {
                            // 根据roles权限生成可访问的路由表
                            router.addRoutes(accessRoutes) // 动态添加可访问路由表
                            this.init()
                        })
                    })
                } else {
                    this.$message.error(res.message)
                }

            })
        },

    },
};
</script>