<template>
    <div></div>
</template>

<script>
export default {
    name: 'LoginSucc',
    data () {
        return {}
    },
    created () {
        this.getParams()
        // 先判断本地有没有 token 和 过期时间, 如果有且没有过期就进入目标页开始定时刷新 token, 否则进入登录页
        // 在地址栏直接进入的情况: 登录页初始化时判断 token 没过期则跳转首页, 首页初始化时 token 过期则跳转登录页
        // 在登录成功后定期刷新 token
    },
    methods: {
        // 保存参数
        getParams () {
            try {
                const { url, schoolCode, jwtToken, refreshTokenTime } = this.$route.query
                this.$router.replace(url === '/login' ? '/' : url)
                if (schoolCode) this.$store.commit('saveSchoolCode', schoolCode)
                this.$store.commit('saveToken', jwtToken)
                this.$store.commit('saveExpireTime', Number(refreshTokenTime) - 8 * 60 * 60 * 1000)
                setTimeout(this.refreshToken, localStorage.expireTime - Date.now())
                // if (localStorage.token && localStorage.expireTime && localStorage.expireTime > Date.now()) {
                // } else {
                //     this.$router.replace('/login')
                // }
            } catch (e) {}
        },
        // 刷新 token
        refreshToken () {
            this.$api.loginApi.refreshToken().then(res => {
                const { Status, Data } = res.data
                if (Status) {
                    const refreshTime = Data.refreshTime - 8 * 60 * 60 * 1000
                    this.$store.commit('saveToken', Data.token)
                    this.$store.commit('saveExpireTime', refreshTime)
                    setTimeout(this.refreshToken, refreshTime - Date.now())
                }
            })
        }
    }
}
</script>

<style lang="less" scoped>
</style>
