123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import axios from 'axios'
- import router, { resetRouter } from './router'
- import store from './store'
- import storage from 'store'
- import NProgress from 'nprogress' // progress bar
- import '@/components/NProgress/nprogress.less' // progress bar custom style
- import notification from 'ant-design-vue/es/notification'
- import { setDocumentTitle, domTitle } from '@/utils/domUtil'
- import { userInfo } from '@/api/headerInfo/index.js'
- import { fileBimView } from '@/api/document/index.js'
- import { ACCESS_TOKEN } from '@/store/mutation-types'
- import { i18nRender } from '@/locales'
- NProgress.configure({ showSpinner: false }) // NProgress Configuration
- const allowList = ['login', 'register', 'registerResult', 'download', 'forget', 'oldRegister'] // no redirect allowList
- const loginRoutePath = '/user/login'
- const defaultRoutePath = '/'
- router.beforeEach(async (to, from, next) => {
- console.log('to',to);
- console.log('from', from);
- NProgress.start() // start progress bar
- to.meta && typeof to.meta.title !== 'undefined' && setDocumentTitle(`${i18nRender(to.meta.title)} - ${domTitle}`)
- /* has token */
- const token = storage.get(ACCESS_TOKEN)
- if (token) {
- if (to.path === loginRoutePath) {
- next({ path: defaultRoutePath })
- NProgress.done()
- } else {
- // check login user.roles is null
- if (store.getters.roles.length === 0) {
- // request login userInfo
- // store
- // .dispatch('GetInfo')
- // .then(res => {
- // // 根据用户权限信息生成可访问的路由表
- // store.dispatch('GenerateRoutes', { token, ...res }).then(() => {
- // // 动态添加可访问路由表
- // // VueRouter@3.5.0+ New API
- // resetRouter() // 重置路由 防止退出重新登录或者 token 过期后页面未刷新,导致的路由重复添加
- // store.getters.addRouters.forEach(r => {
- // router.addRoute(r)
- // })
- // // 请求带有 redirect 重定向时,登录自动重定向到该地址
- // const redirect = decodeURIComponent(from.query.redirect || to.path)
- // if (to.path === redirect) {
- // // set the replace: true so the navigation will not leave a history record
- // next({ ...to, replace: true })
- // } else {
- // // 跳转到目的路由
- // next({ path: redirect })
- // }
- // })
- // })
- // .catch(() => {
- // notification.error({
- // message: '错误',
- // description: '请求用户信息失败,请重试'
- // })
- // // 失败时,获取用户信息失败时,调用登出,来清空历史保留信息
- // store.dispatch('Logout').then(() => {
- // next({ path: loginRoutePath, query: { redirect: to.fullPath } })
- // })
- // })
- store
- .dispatch('getPermissions')
- .then((res) => {
- // 根据用户权限信息生成可访问的路由表
- store.dispatch('GenerateRoutes', { token, ...res }).then(() => {
- // 动态添加可访问路由表
- // VueRouter@3.5.0+ New API
- resetRouter() // 重置路由 防止退出重新登录或者 token 过期后页面未刷新,导致的路由重复添加
- store.getters.addRouters.forEach((r) => {
- router.addRoute(r)
- })
- // 请求带有 redirect 重定向时,登录自动重定向到该地址
- const redirect = decodeURIComponent(from.query.redirect || to.path)
- if (to.path === redirect) {
- // set the replace: true so the navigation will not leave a history record
- next({ ...to, replace: true })
- } else {
- // 跳转到目的路由
- next({ path: redirect })
- }
- })
- })
- .catch(() => {
- notification.error({
- message: '错误',
- description: '请求用户信息失败,请重试',
- })
- // 失败时,获取用户信息失败时,调用登出,来清空历史保留信息
- store.dispatch('Logout').then(() => {
- next({ path: loginRoutePath, query: { redirect: to.fullPath } })
- })
- })
- } else {
- if (to.path === '/4S') {
- const {
- data: { name, fs_password },
- } = await userInfo()
- axios
- .post(`http://newlinker.net:18001/login/pc?username=${name}&password=${fs_password}`)
- .then(({ data }) => {
- if (data.code === 200) {
- window.open('https://www.newlinker.net:8090?token=' + data.data.token)
- } else {
- return
- }
- })
- }
- if(to.path === '/IOT') {
- let {
- data: { bim_view },
- } = await fileBimView('1207')
- localStorage.setItem(bim_view.lightweightName, JSON.stringify([bim_view]))
- window.open(`/modelViewIOT?id=${bim_view.lightweightName}`, '_blank')
- }
- next()
- }
- }
- } else {
- if (allowList.includes(to.name)) {
- // 在免登录名单,直接进入
- next()
- } else {
- next({ path: loginRoutePath, query: { redirect: to.fullPath } })
- NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
- }
- }
- })
- router.afterEach(() => {
- NProgress.done() // finish progress bar
- })
|