import Vue from 'vue' import Router from 'vue-router' import store from '@/store' import storage from 'good-storage' import NotFound from '@/views/NotFound.vue' import Login from '@/views/Login' import Home from '@/views/Home' import { getLongUrlByShortUrl } from '@/api/portal' import { queryParams } from '@/utils/index.js' Vue.use(Router) var router = new Router({ mode: 'history', base: process.env.BASE_URL, routes: [ { path: '/login', name: 'login', component: Login, meta: { single: true, anonymous: true, }, }, { path: '/', redirect: { name: 'home', }, }, { path: '/login/:tenant', name: 'tenantLogin', props: true, component: Login, meta: { single: true, anonymous: true, }, }, { path: '/home', name: 'home', component: Home, }, { path: '/formDetail/:formId', name: 'formDetail', component: () => import('@/views/process/FormDetail.vue'), props: true, }, { path: '/matter/startProcess', name: 'startProcess', component: () => import('@/views/matter/StartProcess.vue'), props: true, }, { path: '/matter/approvalForm', name: 'approvalForm', component: () => import('@/views/matter/ApprovalForm.vue'), props: true, }, { path: '/matter/newMatter', name: 'newMatter', component: () => import('@/views/matter/newMatter/index.vue'), props: true, }, { path: '/matter/newMatter_search', name: 'newMatterSearch', component: () => import('@/views/matter/newMatter/Search.vue'), props: true, }, { path: '/matter/myTask', name: 'myDefaultMatters', redirect: '/matter/myTask/0', }, { path: '/matter/myTask/:activeNumber', name: 'myMatters', component: () => import('@/views/matter/task/index.vue'), props: true, }, /* 我的转办 */ /* { path: '/matter/myDelegate', name: 'myDelegate', component: () => import('@/views/matter/MyDelegate.vue'), props: true, }, */ /* 传阅事项 */ { path: '/matter/circulateMatter', name: 'circulateMatter', component: () => import('@/views/matter/circulateMatter/index.vue'), props: true, }, { path: '/matter/myRequest', name: 'myRequest', component: () => import('@/views/matter/myRequest/index.vue'), props: true, }, { path: '/canvas', name: 'canvas', component: () => import('@/components/Canvas.vue'), props: true, }, { path: '/shareInst/:instId/:isShare', name: 'shareInst', component: () => import('@/views/matter/ApprovalForm.vue'), props: true, }, { path: '/worker', name: 'worker', component: () => import('@/views/worker/index.vue'), props: true, }, { path: '/worker/formList', name: 'formList', component: () => import('@/views/worker/FormList.vue'), props: true, }, { path: '/worker/previewChart', name: 'previewChart', component: () => import('@/views/worker/previewChart.vue'), props: true, }, { path: '/personal', name: 'personal', component: () => import('@/views/Personal.vue'), props: true, }, { path: '/messageReceiverList', name: 'messageReceiverList', component: () => import('@/views/MessageReceiverList.vue'), props: true, }, { path: '*', component: NotFound, meta: { single: true, }, }, { path: '/previewColumn', name: 'previewColumn', component: () => import('@/views/portal/PreviewColumn.vue'), }, { path: '/newsList', name: 'newsList', component: () => import('@/views/portal/NewsList.vue'), }, { path: '/templateForm/:templateKey/:action', name: 'templateFormDetail', component: () => import('@/views/worker/TemplateFormDetail.vue'), meta: { singleInherit: true, //继承属性:是否继承上一个路由的single属性。 }, }, { path: '/QRCodeForm/:templateKey', component: () => import('@/views/worker/TemplateFormDetail.vue'), name: 'qRCodeForm', props: true, hidden: true, meta: { title: '表单详情',showButton: false }, }, //企微钉钉路由 //钉钉 { path: '/dingTalk', name: 'dingTalk', component: () => import('@/components/jump/dt_jump.vue'), props: true, meta: { single: true, anonymous: true, }, }, //钉钉-租户 { path: '/login/:tenant/dingTalk', name: 'dingTalk', component: () => import('@/components/jump/dt_jump.vue'), props: true, meta: { single: true, anonymous: true, }, }, //飞书 { path: '/flyBook', name: 'flyBook', component: () => import('@/components/jump/flybook_jump.vue'), props: true, meta: { single: true, anonymous: true, }, }, //飞书-租户 { path: '/login/:tenant/flyBook', name: 'flyBook', component: () => import('@/components/jump/flybook_jump.vue'), props: true, meta: { single: true, anonymous: true, }, }, //企业公众号 { path: '/weChatOffAcc', name: 'weChatOffAcc', component: () => import('@/components/jump/wcoa_jump.vue'), props: true, meta: { single: true, anonymous: true, }, }, //企业公众号-租户 { path: '/login/:tenant/weChatOffAcc', name: 'weChatOffAcc', component: () => import('@/components/jump/wcoa_jump.vue'), props: true, meta: { single: true, anonymous: true, }, }, //企业微信 { path: '/weChat', name: 'weChat', component: () => import('@/components/jump/wcw_jump.vue'), props: true, meta: { single: true, anonymous: true, }, }, //企业微信-租户 { path: '/login/:tenant/weChat', name: 'weChat', component: () => import('@/components/jump/wcw_jump.vue'), props: true, meta: { single: true, anonymous: true, }, }, //小程序 { path: '/miniprogram', name: 'miniprogram', component: () => import('@/components/jump/miniprogram_jump.vue'), props: true, meta: { single: true, anonymous: true, }, }, //小程序-租户 { path: '/login/:tenant/miniprogram', name: 'miniprogram', component: () => import('@/components/jump/miniprogram_jump.vue'), props: true, meta: { single: true, anonymous: true, }, }, //企微钉钉待办消息路由 { path: '/task/:taskId/:leaderId', name: 'task', component: () => import('@/views/matter/ApprovalForm.vue'), props: true, meta: { isGetApprovalBtn: true, }, }, ], }) const handelShortUrl = (to, from, next) => { getLongUrlByShortUrl(to.fullPath.substr(1)) .then((resp) => { if (resp && resp.state) { store .dispatch('login/validAndCompletedCurrent', resp.value.guestToken) .then(() => { next({ path: resp.value.url }) }) .catch(() => { store.dispatch('login/logoutAndCleanUp') }) } else { next({ path: '/messageFillPage/' + resp.message }) } }) .catch(() => { store.dispatch('login/logoutAndCleanUp') }) } // 移除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]) } } setTimeout(() => { window.history.replaceState( null, null, `${window.location.pathname}${ newVars.length > 0 ? '?' : '' }${newVars.join('&')}` ) }, 100) } router.beforeEach((to, from, next) => { if (to.fullPath.startsWith('/surl/')) { //为外链表单返回 storage.session.set('replaceHome', true) handelShortUrl(to, from, next) return } if (to.path === '/matter/startProcess' && !to.query.defId) { window.history.back() return } if (to.matched.some((record) => !record.meta.anonymous)) { let ticket = to.query.token if (window.ssoConfig.mode == 'cas') { ticket = to.query.ticket } store .dispatch('login/validAndCompletedCurrent', ticket) .then(() => { setTimeout(() => { // 登录成功后清除url参数 removeUrlParams('token') }, 3000) next() }) .catch(() => { if ( !store.state.login || !store.state.login.currentUser || !store.getters.login.token ) { let query = JSON.parse(JSON.stringify(to.query)) delete query.token let queryStr = queryParams(query, true) next({ path: '/login', query: { redirect: to.path + queryStr, }, }) return } store.dispatch('login/logoutAndCleanUp').then(() => { if (window.ssoConfig.mode == 'cas' && !ticket) { let service = window.location.href storage.set('service', service) window.location.href = window.ssoConfig.url + '?service=' + service } if (!window.ssoConfig.mode || window.ssoConfig.mode == 'jwt') { //此时token已经无效,去掉url中的token参数 let query = JSON.parse(JSON.stringify(to.query)) delete query.token let queryStr = queryParams(query, true) next({ path: '/login', query: { redirect: to.path + queryStr, }, }) } }) }) } else { if ( to.path === '/login' && store.state.login && store.state.login.currentUser ) { next({ path: '/home' }) } else { next() } } }) export default router