<template>
    <!-- v-loading="loading"
        :element-loading-background="loadingColor" -->
    <div class="auth-page">
        企业微信-授权认证中......
        {{ message }}
        {{ err }}
    </div>
</template>

<script>
import utils from '@/utils.js'
import uc from '@/api/uc.js'
export default {
    components: {},
    data() {
        return {
            loading: true,
            code: '',
            message: '',
            err: '',
            loadingColor: 'rgba(88, 115, 134, 0.3)',
        }
    },
    mounted() {
        // 根据企业微信链接分离出需要的参数
        this.code = utils.getParameters('code')
        console.log(this.code, 'this.code ')
        if (!this.code) {
            this.loading = false
            this.$message.warning({
                type: 'warning',
                message: '企业微信登录失败，将跳转至登录页面！',
            })
            this.$router.push({
                path: '/login',
            })
            return
        } else {
            this.proxyLogin(this.code)
        }
    },
    methods: {
        proxyLogin(code) {
            this.$store
                .dispatch('login/loginByQYWX', code)
                .then((loginStatus) => {
                    this.message = loginStatus
                    if (loginStatus && loginStatus == true) {
                        this.$store.dispatch('menu/getCurrentUserMethodAuth')
                        this.$store.dispatch('login/getAllDict')
                        this.$store
                            .dispatch('menu/actionFrontMenus')
                            .then((res) => {
                                if (!res || !res.length) {
                                    this.loading = false
                                    this.$message.warning(
                                        '抱歉！您尚无可访问页面，请联系管理员添加！'
                                    )
                                    setTimeout(() => {
                                        this.$router.push({
                                            path: '/login',
                                        })
                                    }, 5000)
                                    return
                                }
                                let bigScreen =
                                    res &&
                                    res.filter(
                                        (item) => item.alias === 'bigScreen'
                                    )
                                // 如果用户有大屏菜单进去大屏页面
                                if (bigScreen && bigScreen.length !== 0) {
                                    this.$router.push({
                                        path: `/${bigScreen[0].children[0].alias}`,
                                    })
                                    return
                                }
                            })
                    } else if (loginStatus == false) {
                        this.$router.push({
                            name: 'resetPwdView',
                            query: {type: 'login'},
                        })
                    }
                })
                .catch((msg) => {
                    this.loading = false
                    this.err = msg
                    this.$message.warning(msg)
                    if (msg.indexOf && msg.indexOf('timeout of') !== -1) {
                        this.$message.warning('登录超时')
                    }
                    setTimeout(() => {
                        this.$router.push({
                            path: '/login',
                        })
                    }, 2000)
                })
        },
    },
}
</script>
<style>
.auth-page{
    width: 100%;
    height: 100%;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>
