import Vue from 'vue' import Router from 'vue-router' import store from '@/store' import NotFound from '@/views/NotFound.vue' import Login from '@/views/Login.vue' import Jump from '@/views/Jump.vue' import NoAuth from '@/views/NoAuth.vue' import MobileAuth from '@/views/MobileAuth.vue' import YKZAuth from '@/views/YKZAuth.vue' import MobileAuthSelf from '@/views/MobileAuthSelf.vue' import req from '@/request.js' import qs from 'qs' import uc from '@/api/uc.js' import { MessageBox } from 'element-ui' import { Loading } from 'element-ui' import axios from 'axios' const noRightPath = 'http://23.99.8.1:8080/fvue/NotFound' const noRightPathMobile = 'http://23.99.8.1:8080/mobilevue/NotFound' //路由 import mainRouter from '@/module_router.js' import Layout from '@/components/layout/Layout.vue' Vue.use(Router) var router = new Router({ mode: 'history', base: process.env.BASE_URL, routes: [ { path: '/', redirect: { name: 'login' } }, { path: '/login', name: 'login', component: Login, meta: { single: true, anonymous: true } }, { path: '/Jump', name: 'Jump', component: Jump, meta: { single: true, anonymous: true } }, { path: '/JumpDetailComponent', name: 'JumpDetailComponent', component: Jump, meta: { single: true, anonymous: true } }, { path: '/ProjectJump', name: 'ProjectJump', component: Jump, meta: { single: true, anonymous: true } }, { path: '/threeLevelProjectJump', name: 'threeLevelProjectJump', component: Jump, meta: { single: true, anonymous: true } }, { path: '/NoAuth', name: 'NoAuth', component: NoAuth, meta: { single: true, anonymous: true } }, { path: '/MobileAuth', name: 'MobileAuth', component: MobileAuth, meta: { single: true, anonymous: true } }, { path: '/YKZAuth', name: 'YKZAuth', component: YKZAuth, meta: { single: true, anonymous: true } }, { path: '/MobileAuthSelf', name: 'MobileAuthSelf', component: MobileAuthSelf, meta: { single: true, anonymous: true } }, { path: '/resetPwdView', name: 'resetPwdView', props: true, component: () => import('@/views/ResetPwdView.vue'), meta: { single: true, anonymous: true } }, { path: '*', component: NotFound, meta: { single: true } }, { path: '/MajorProjectsView', name: 'MajorProjectsView', component: () => import('@/views/BigScreen/MajorProjectsView.vue') }, { path: '/MajorProjectsViewTest', name: 'MajorProjectsViewTest', component: () => import('@/views/BigScreen/MajorProjectsView.vue') }, // { // path: '/MajorProjectsViewDataFinish', // name: 'MajorProjectsViewDataFinish', // component: () => import('@/views/BigScreen/MajorProjectsViewDataFinish.vue') // }, { path: '/MajorProjectsSubView', name: 'MajorProjectsSubView', component: () => // import('@/views/BigScreen/MajorProjectsSubView/index.vue') import('@/views/BigScreen/MajorProjectsSubView.vue') }, { path: '/DistrictReformView', name: 'DistrictReformView', component: () => import('@/views/BigScreen/DistrictReformView.vue') }, { path: '/DistrictReformSubView', name: 'DistrictReformSubView', component: () => import('@/views/BigScreen/DistrictReformSubView.vue') }, { path: '/DetailComponent', name: 'DetailComponent', component: () => import('@/views/BigScreen/DetailComponentPage.vue'), }, { path: '/reportPreview', name: 'reportPreview', component: () => import('@/views/BigScreen/components/subComponents/reportPreview.vue'), }, { path: '/MajorProjectsMobile', name: 'MajorProjectsMobile', component: () => import('@/views/MobileScreen/MajorProjectsMobile.vue'), }, { path: '/DistrictReformMobile', name: 'DistrictReformMobile', component: () => import('@/views/MobileScreen/DistrictReformMobile.vue'), }, { path: '/platLayout', name: 'platLayout', component: Layout, meta: { single: true, anonymous: true }, children: [ { path: 'PersonalInfo', name: 'PersonalInfoInner', props: true, component: () => import('@/views/PersonalInfo.vue') }, { path: 'resetPwdView', name: 'resetPwdViewInner', props: true, component: () => import('@/views/ResetPwdView.vue') }, ...mainRouter ] } ] }) const createRouter = () => new Router({ mode: 'history', routes: [] }) // 版本号 // router.beforeEach(async (to, from, next) => { // let oldV = localStorage.getItem('version') // // if (!oldV) { // // oldV = require('../public/js/version.json') // // } // axios.get(`/fvue/js/version.json`).then(res => { // let { version } = res.data // if (oldV !== version) { // // localStorage.setItem('version', version) // // if (to.name !== 'login' && from.name !== 'login') { // // MessageBox.confirm('版本已更新,请重新登录。', '提示', { // // confirmButtonText: '确定', // // showClose: false, // // showCancelButton: false, // // closeOnClickModal: false, // // type: 'success' // // }).then(() => { // // next({ path: '/login' }) // // window.location.reload() // // }) // // } else { // // next() // // window.location.reload() // // } // // window.location.reload() // // localStorage.setItem('version', version) // // next({ path: '/login',}) // } // next() // }) // }) //重写动态添加路由方法 router.$addRoutes = params => { const newRouter = createRouter() router.matcher = newRouter.matcher //重置路由规则 router.addRoutes(params) // 添加路由 } //动态新增二级路由 router.addList = (path, list) => { let isAddPath = false let isChildren = false return isChildren } let registerRouteFresh = true const ssoRedirect = () => { if (window.ssoConfig.mode == 'cas') { let service = window.location.href localStorage.setItem('service', service) window.location.href = window.ssoConfig.url + '?service=' + service } else if (window.ssoConfig.mode == 'oauth') { if (!window.ssoConfig.clientId) { throw 'oauth模式下必须提供clientId' } const service = window.location.href localStorage.setItem('service', service) window.location.href = `${window.ssoConfig.url}?response_type=code&client_id=${window.ssoConfig.clientId}&redirect_uri=${service}` } } const handelShortUrl = (to, from, next) => { req .get( '${portal}/portal/shorturlManage/v1/getLongUrlByShortUrl?shortUrl=' + to.fullPath.substr(1), '', false ) .then(resp => { if (resp && resp.data && resp.data.state) { store .dispatch( 'login/validAndCompletedCurrent', resp.data.value.guestToken ) .then(() => { next({ path: resp.data.value.url }) }) .catch(() => { logoutAndCleanUp(to, from, next) }) } else { next({ path: '/messageFillPage/' + resp.data.message }) } }) .catch(() => { logoutAndCleanUp(to, from, next) }) } const logoutAndCleanUp = (to, from, next) => { store.dispatch('login/logoutAndCleanUp').then(() => { if (window.ssoConfig.mode && window.ssoConfig.mode != 'jwt' && !ticket) { ssoRedirect() } else if (!window.ssoConfig.mode || window.ssoConfig.mode == 'jwt') { next({ path: '/login', // query: { // redirect: to.fullPath // } }) } else { throw '登录失败' } }) } // 移除url后面的指定参数 const removeUrlParams = param => { const query = window.location.search.substring(1) if (!query) { return } const vars = query.split('&') const newVars = [] for (var i = 0; i < vars.length; i++) { var pair = vars[i].split('=') if (pair[0] != param) { newVars.push(vars[i]) } } console.log(newVars) setTimeout(() => { const path = `${window.location.pathname}${newVars.length > 0 ? '?' : '' }${newVars.join('&')}` window.history.replaceState(null, null, path) }, 1000) } const findFirstChild = (meuns) => { if (meuns[0].children && meuns[0].children.length) { return findFirstChild(meuns[0].children) } else { return meuns[0].alias } } const ykzLogin = (code, type, next) => { store.dispatch('login/loginByYKZ', code).then(user => { if (user.state === undefined) { if (user.loginStatus) { store.dispatch('login/getAllDict') store.dispatch('menu/actionFrontMenus').then(res => { if (type) { next({ path: '/MajorProjectsMobile' }) } else { let path = '' let dataArr = res && res.filter( (item) => item.alias === 'bigScreen' ) if (dataArr && dataArr.length !== 0) { if ( dataArr[0].children.length !== 0 ) { path = `/${dataArr[0].children[0].alias}` } } else { let dataMenus = res && res.filter( (item) => item.alias === 'normal' ) if ( dataMenus && dataMenus.length !== 0 ) { let alias = findFirstChild(dataMenus) path = `/platLayout/${alias}` } } next({ path }) } }) } else { let route = { name: 'YKZAuth', query: { binding: true }, params: { account: user.account, openid: user.openid, } } if (type) route.query.type = type next(route) } } else if (user.state === false) { let route = { path: '/YKZAuth', query: { msg: user.message }, } if (type) route.query.type = type next(route) } }) } let searchDateOrigin const analysisQuarter = (quarter) => { console.log(quarter, "quarter") searchDateOrigin = sessionStorage.getItem('searchDate') console.log(searchDateOrigin, "searchDateOrigin") if (quarter) { let splitArr = quarter.split('Q') let searchDate = { year: parseInt(splitArr[0]), quarter: parseInt(splitArr[1]), month: parseInt(splitArr[1]) ? 3 * splitArr[1] - 2 : 12, } sessionStorage.setItem('searchDate', JSON.stringify(searchDate)) } } const resetQuarter = () => { if (searchDateOrigin) { sessionStorage.setItem('searchDate', searchDateOrigin) } } const reportLogin = (ykzEmployeeCode, type, next) => { let data = qs.stringify({ ykzEmployeeCode }) store.dispatch('login/loginByReports', data).then(res => { console.log(res.loginStatus) if (res && res.loginStatus) { store.dispatch('login/getAllDict') store.dispatch('menu/actionFrontMenus').then(res => { if (type == 'mobile') { next({ path: '/DistrictReformMobile' }) } else { next({ path: `/${res[0].children[0].alias}`, }) } }) } }).catch(err => { if (type == 'mobile') { window.location.href = noRightPathMobile } else { window.location.href = noRightPath } return let route = { path: '/NoAuth', } if (type) { route.query = { type } } next(route) }) } const ProjectJumpLogin = (ykzEmployeeCode, query, next) => { let data = qs.stringify({ ykzEmployeeCode }) let { type, comp, year, month, pageType, title, projectName } = query store.dispatch('login/loginByReports', data).then(res => { if (res && res.loginStatus) { if (!year||!month) { let date = new Date() year = date.getFullYear() month = date.getMonth() + 1 } let quarter = 1 if (month <= 3) { quarter = 1 } else if (month <= 6) { quarter = 2 } else if (month <= 9) { quarter = 3 } else if (month <= 12) { quarter = 4 } let dateData = { year: year - 0, month: month - 0, quarter } sessionStorage.setItem('searchDate', JSON.stringify(dateData)) store.dispatch('login/getAllDict') store.dispatch('menu/actionFrontMenus').then(res => { if (type == 'mobile') { next({ path: '/DistrictReformMobile' }) } else { let path = `/DetailComponent?comp=${comp}` + (pageType ? `&type=${pageType}` : '') + (title ? `&title=${title}` : '') + (projectName ? `&projectName=${projectName}` : '') next({ path }) } }) } }).catch(err => { if (type == 'mobile') { window.location.href = noRightPathMobile } else { window.location.href = noRightPath } return }) } const threeLevelProjectJumpLogin = (userToken, query, next) => { let data = qs.stringify({ userToken }) let { type, comp, year, month, pageType, title } = query store.dispatch('login/threeLevelLoginByReports', data).then(res => { if (res && res.loginStatus) { if (!year||!month) { let date = new Date() year = date.getFullYear() month = date.getMonth() + 1 } let quarter = 1 if (month <= 3) { quarter = 1 } else if (month <= 6) { quarter = 2 } else if (month <= 9) { quarter = 3 } else if (month <= 12) { quarter = 4 } let dateData = { year: year - 0, month: month - 0, quarter } sessionStorage.setItem('searchDate', JSON.stringify(dateData)) store.dispatch('login/getAllDict') store.dispatch('menu/actionFrontMenus').then(res => { // console.log(res, '------------'); // next({ // path: `/${res[0].children[0].alias}`, // }) if (type == 'mobile') { next({ path: '/DistrictReformMobile' }) } else { let path = `/MajorProjectsView` next({ path }) } }) } }).catch(err => { if (type == 'mobile') { window.location.href = noRightPathMobile } else { window.location.href = noRightPath } return }) } const tokenLogin = (userInfo, type, next) => { store.dispatch('login/loginByPrincipal', userInfo).then(loginStatus => { if (loginStatus && loginStatus == true) { store.dispatch('login/getAllDict') store.dispatch('menu/actionFrontMenus').then(res => { if (type) { next({ path: '/DistrictReformMobile' }) } else { next({ path: `/${res[0].children[0].alias}`, }) } }) } }) } const reportDetailLogin = (ykzEmployeeCode, quarter, next, type) => { let data = qs.stringify({ ykzEmployeeCode }) store.dispatch('login/loginByReports', data).then(res => { console.log(res.loginStatus) if (res && res.loginStatus) { analysisQuarter(quarter) store.dispatch('login/getAllDict') store.dispatch('menu/actionFrontMenus').then(res => { console.log(res) if (type == 'mobile') { next({ path: '/DistrictReformMobile' }) } else { next({ path: `/reportPreview`, }) } }) } }).catch(err => { if (type == 'mobile') { window.location.href = noRightPathMobile } else { window.location.href = noRightPath } return let route = { path: '/NoAuth', } if (type) { route.query = { type } } next(route) }) } // 登录后可以任意访问 const noAuthAfterLogin = [ // { // alias: "MajorProjectsMobile", // name: '移动端重大项目' // }, { // alias: "DistrictReformMobile", // name: '移动端区县报表' // }, { alias: "PersonalInfoInner", name: '个人中心' }, { alias: "resetPwdViewInner", name: '重置密码' }] // 非后台配置的静态菜单 const staticMenus = [{ alias: "MajorProjectsSubView", name: '重大项目子页面', parentAlias: 'MajorProjectsView' }, { alias: "DistrictReformSubView", name: '区县报表子页面', parentAlias: 'DistrictReformView' }, { alias: "DetailComponent", name: '报表下转详情', parentAlias: 'bigscreen' }, { alias: "ReformProjectDetails", name: '自主探索改革项目清单', parentAlias: 'worktable' }, { alias: "SatisfactionApplication", name: '区县满意度表单', parentAlias: 'SatisfactionSurveys' }, { alias: "SatisfactionApplication", name: '区县满意度表单', parentAlias: 'SatisfactionAudits' }, { alias: "SatisfactionApplication", name: '区县满意度表单', parentAlias: 'AuditConfirmation' }, { alias: "reportPreview", name: '报表详情', parentAlias: 'bigscreen' }, { alias: "saveOrUpdatePage", name: '数字重庆应用表单', parentAlias: 'InputAndUpdate' }, { alias: "saveOrUpdatePage", name: '数字重庆应用表单', parentAlias: 'DGAppBaseInfoSearch' }, { alias: "saveOrUpdatePage", name: '数字重庆应用表单', parentAlias: 'AppInfoDeptReview' }, { alias: "saveOrUpdatePage", name: '数字重庆应用表单', parentAlias: 'TopicGroupReview' }, { alias: "saveOrUpdatePage", name: '数字重庆应用表单', parentAlias: 'ReformConfirme' }, { alias: "saveOrUpdatePage", name: '数字重庆应用表单', parentAlias: 'DGAppSearch' }, { alias: "ThreeOnDetailPage", name: '三攻坚一盘活表单', parentAlias: 'InputUpload' }, { alias: "ThreeOnDetailPage", name: '三攻坚一盘活表单', parentAlias: 'threeOneAudit' }, { alias: "ThreeOnDetailPage", name: '三攻坚一盘活表单', parentAlias: 'threeOneApprove' }] let allAlias = [] // 判断用户菜单中是否拥有当前路由菜单 const filterUserMenus = (to, from, next) => { return new Promise((resolve, reject) => { store.dispatch('menu/actionFrontMenus').then(res => { let menus = [] if (res && res.length) menus = [...res] menus.push(...noAuthAfterLogin) // 加上不需权限的菜单 let targetAlias = to.name allAlias = [] // 遍历当前用户所有菜单 监测权限并收集所有alias let hasCurrentRouterMenu = menusFilter(menus, targetAlias) // 监测当前菜单是不是静态权限菜单 let targetMenu = staticMenus.filter(item => item.alias.toLowerCase() == targetAlias.toLowerCase()) if (targetMenu.length) { // 当前用户是否拥有当前静态权限菜单的父级菜单权限 hasCurrentRouterMenu = targetMenu.some(item => { if (allAlias.includes(item.parentAlias.toLowerCase())) { return true } }) } resolve(hasCurrentRouterMenu) }) }) } // 遍历查到路由别名是否存在于用户菜单中 const menusFilter = (menuData, alias) => { let res = menuData.find(item => { allAlias.push(item.alias.toLowerCase()) return item.alias.toLowerCase() == alias.toLowerCase() }) if (res) { return true } else { for (let i = 0; i < menuData.length; i++) { if (menuData[i].children && menuData[i].children.length) { res = menusFilter(menuData[i].children, alias) if (res) { return true } } } return false } } router.beforeEach(async (to, from, next) => { console.log(to); console.log(to.name, from.name, "to,from") // 愉快政单点登录 if (to.name == 'YKZAuth' && to.query.auth_code) { let code = to.query.auth_code // 愉快政登录code let type = to.query.type // 愉快政跳转类型, 移动端或者pc ykzLogin( code, type, next ) } // 大屏跳转 if (to.name == 'Jump' && to.query.userinfo) { let type = to.query.type let ykzEmployeeCode = to.query.userinfo reportLogin( ykzEmployeeCode, type, next ) } // 报表详跳转 if (to.name == 'JumpDetailComponent' && to.query.userinfo) { // let type = to.query.type let ykzEmployeeCode = to.query.userinfo let quarter = to.query.quarter reportDetailLogin( ykzEmployeeCode, quarter, next, // type, ) } // 市级驾驶舱子页面跳转 if (to.name == 'ProjectJump' && to.query.userinfo) { // let { type, comp } = to.query let ykzEmployeeCode = to.query.userinfo ProjectJumpLogin( ykzEmployeeCode, to.query, next ) } // 市级驾驶舱页面跳转(三级治理中心) if (to.name == 'threeLevelProjectJump' && to.query.userinfo) { // let { type, comp } = to.query let userToken = to.query.userinfo console.log(userToken); threeLevelProjectJumpLogin( userToken, to.query, next ) } // // 大屏跳转 // if (to.name == 'Jump' && to.query.tokenKey) { // let type = to.query.type // tokenLogin( // { // account: 'guest', // password: '123456', // }, // type, // next // ) // } store.dispatch('menu/getAllmenus', {}).then(menus => { //href菜单不进这段代码 if (to.fullPath.indexOf('href') == -1) { let alias = to.fullPath.split('/')[1] for (let i = 0; i < menus.length; i++) { if (menus[i].alias === alias) { store.dispatch('menu/getCurrentMenu', { alias: menus[i].alias, name: menus[i].name }) break } } } }) if (to.fullPath.startsWith('/surl/')) { handelShortUrl(to, from, next) return } // 1.当前为单点登录模式,且要路由到login页面,则做单点登录重定向; if ( to.name == 'login' && window.ssoConfig.mode && window.ssoConfig.mode != 'jwt' ) { // 单点登录时如果已经携带了code或者ticket时,如果是/login路径需要跳转到默认页面(解决反复跳转的问题) if ( (window.ssoConfig.mode == 'cas' || window.ssoConfig.mode == 'oauth') && (to.query.code || to.query.ticket) ) { window.location.href = window.location.href.replace('/login', '') return } else { ssoRedirect() } } if (from.name != 'defStart' || to.name != 'instStart') { sessionStorage.removeItem('formImportTempRefId') sessionStorage.removeItem('formImportTempJson') } if ( sessionStorage.menu_alias && to.fullPath && to.fullPath.split('/')[1] == 'home' ) { sessionStorage.menu_alias = '' } if (to.matched.some(record => !record.meta.anonymous)) { let ticket = to.query.token // 2.当前已经从单点页面重定向回来了,且在url地址中携带了ticket或code; if (window.ssoConfig.mode == 'cas') { ticket = to.query.ticket } else if (window.ssoConfig.mode == 'oauth') { ticket = to.query.code } // 用户菜单鉴权,用户是否拥有当前菜单 let hasMenus = true hasMenus = await filterUserMenus(to, from, next) console.log(hasMenus, "hasMenus") if (!hasMenus) { //鉴权失败时根据当前模式重定向到对应页面。 logoutAndCleanUp(to, from, next) } store // 3.携带ticket做用户鉴权; .dispatch('login/validAndCompletedCurrent', ticket) .then(() => { store._vm.$root.$emit('login-completed') if (registerRouteFresh) { if (sessionStorage.menu_alias) { let path = '/' + sessionStorage.menu_alias let isChildren = router.addList(path, router.options.routes) //动态新增二级路由 //当前点击菜单的路由别名不存在 router.js 里面 // if (!isChildren) { // let alias_new = { // path: path, // name: 'alias_new', // component: resolve => // require(['@/views/menuContent.vue'], resolve), // children: [] // } // router.options.routes.push(alias_new) // router.addList(path, router.options.routes) //动态新增二级路由 // } router.replace({ path: to.path, query: to.query }) } registerRouteFresh = false } next() // url地址带token的方式认证成功后删除url中的token // removeUrlParams('tokenKey') }) .catch(() => { // 4.鉴权失败时根据当前模式重定向到对应页面。 logoutAndCleanUp(to, from, next) }) if ((to.name === 'MajorProjectsView' && from.name === 'DistrictReformView') || (from.name === 'MajorProjectsView' && to.name === 'DistrictReformView')) { sessionStorage.removeItem('searchDate') } } else { next() } }) export default router