{"size": 2251, "ext": "js", "max_stars_repo_path": "GQA-FRONTEND/src/boot/permission.js", "max_stars_repo_name": "aehm2k5/gin-quasar-admin", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "GQA-FRONTEND/src/boot/permission.js", "max_issues_repo_name": "aehm2k5/gin-quasar-admin", "max_issues_repo_issues_event_min_datetime": "2021-11-15T07:31:24.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-29T07:24:25.000Z", "max_forks_repo_path": "GQA-FRONTEND/src/boot/permission.js", "max_forks_repo_name": "aehm2k5/gin-quasar-admin", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 66, "max_forks_count": null, "avg_line_length": 30.4189189189, "max_line_length": 70, "alphanum_fraction": 0.4624611284}
import { boot } from 'quasar/wrappers'
import { LoadingBar, Loading, QSpinnerGears } from 'quasar'
// import { Allowlist } from 'src/settings'
// import { GetToken } from 'src/utils/cookies'
import { useUserStore } from 'src/stores/user'
import { usePermissionStore } from 'src/stores/permission'
import useCommon from 'src/composables/useCommon'

LoadingBar.setDefaults({
    color: 'red',
    size: '4px',
    position: 'top'
})

function startLoading() {
    Loading.show({
        // spinner: QSpinnerGears,
        message: function () {
            return this.$t('SystemLoading')
        }
    })
    LoadingBar.start()
}

function stopLoading() {
    Loading.hide()
    LoadingBar.stop()
}

export default boot(({ router, store }) => {
    router.beforeEach((to, from, next) => {
        const userStore = useUserStore()
        const permissionStore = usePermissionStore()
        startLoading()
        const token = userStore.GetToken()
        const { AllowList } = useCommon()
        if (token) {
            if (AllowList.indexOf(to.path) !== -1) {
                next({ path: '/' })
                stopLoading()
            } else {
                if (!permissionStore.userMenu.length) {
                    permissionStore.GetUserMenu().then(res => {
                        // 在vue-router4中，addRoutes被废弃，改为了addRoute，循环调用
                        // 动态添加鉴权路由表
                        if (res) {
                            res.forEach(item => {
                                router.addRoute(item)
                            })
                            next({ ...to, replace: true })
                        } else {
                            store.dispatch('user/HandleLogout')
                            next({ path: '/', replace: true })
                        }
                    })
                } else {
                    next()
                }
                stopLoading()
            }
        } else {
            if (AllowList.indexOf(to.path) !== -1) {
                next()
                stopLoading()
            } else {
                next(`/login?redirect=${to.fullPath}`)
                stopLoading()
            }
        }
    })
    router.afterEach(() => {
        stopLoading()
    })
})