diff --git a/src/router/index.js b/src/router/index.js index b5e4cf8..5879617 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -47,12 +47,14 @@ { path: '/', component: Layout, - redirect: 'overview', + redirect: 'dashboard', name: 'Dashboard', - hidden: true, + // hidden: true, children: [{ path: 'dashboard', - component: () => import('@/views/dashboard/index') + component: () => import('@/views/dashboard/index'), + name: 'Dashboard', + meta: { title: '首页', icon: 'dashboard', noCache: true, affix: true } }] } ] @@ -115,11 +117,11 @@ meta: { title: '告警工单', icon: 'icon-config', keepAlive: true, permission: ['/job'] } }, { - path: '/detailJob', + path: '/detailJob/:id(\\d+)', name: 'DetailJob', component: () => import('@/views/jobManage/detailJob'), hidden: true, - meta: { title: '工单详情', icon: 'icon-config', permission: ['/job'] } + meta: { title: '工单详情', icon: 'icon-config', permission: ['/job/info'] } }, { path: '/listRule', diff --git a/src/router/index.js b/src/router/index.js index b5e4cf8..5879617 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -47,12 +47,14 @@ { path: '/', component: Layout, - redirect: 'overview', + redirect: 'dashboard', name: 'Dashboard', - hidden: true, + // hidden: true, children: [{ path: 'dashboard', - component: () => import('@/views/dashboard/index') + component: () => import('@/views/dashboard/index'), + name: 'Dashboard', + meta: { title: '首页', icon: 'dashboard', noCache: true, affix: true } }] } ] @@ -115,11 +117,11 @@ meta: { title: '告警工单', icon: 'icon-config', keepAlive: true, permission: ['/job'] } }, { - path: '/detailJob', + path: '/detailJob/:id(\\d+)', name: 'DetailJob', component: () => import('@/views/jobManage/detailJob'), hidden: true, - meta: { title: '工单详情', icon: 'icon-config', permission: ['/job'] } + meta: { title: '工单详情', icon: 'icon-config', permission: ['/job/info'] } }, { path: '/listRule', diff --git a/src/store/getters.js b/src/store/getters.js index 1a3b943..a7e0c78 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -1,6 +1,8 @@ const getters = { sidebar: state => state.app.sidebar, // 侧边栏 device: state => state.app.device, // 设备 + visitedViews: state => state.tagsView.visitedViews, + cachedViews: state => state.tagsView.cachedViews, kaptcha: state => state.app.kaptcha, // 验证码 publicKey: state => state.app.publicKey, // 公钥 sid: state => state.app.sid, // 用户标识 diff --git a/src/router/index.js b/src/router/index.js index b5e4cf8..5879617 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -47,12 +47,14 @@ { path: '/', component: Layout, - redirect: 'overview', + redirect: 'dashboard', name: 'Dashboard', - hidden: true, + // hidden: true, children: [{ path: 'dashboard', - component: () => import('@/views/dashboard/index') + component: () => import('@/views/dashboard/index'), + name: 'Dashboard', + meta: { title: '首页', icon: 'dashboard', noCache: true, affix: true } }] } ] @@ -115,11 +117,11 @@ meta: { title: '告警工单', icon: 'icon-config', keepAlive: true, permission: ['/job'] } }, { - path: '/detailJob', + path: '/detailJob/:id(\\d+)', name: 'DetailJob', component: () => import('@/views/jobManage/detailJob'), hidden: true, - meta: { title: '工单详情', icon: 'icon-config', permission: ['/job'] } + meta: { title: '工单详情', icon: 'icon-config', permission: ['/job/info'] } }, { path: '/listRule', diff --git a/src/store/getters.js b/src/store/getters.js index 1a3b943..a7e0c78 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -1,6 +1,8 @@ const getters = { sidebar: state => state.app.sidebar, // 侧边栏 device: state => state.app.device, // 设备 + visitedViews: state => state.tagsView.visitedViews, + cachedViews: state => state.tagsView.cachedViews, kaptcha: state => state.app.kaptcha, // 验证码 publicKey: state => state.app.publicKey, // 公钥 sid: state => state.app.sid, // 用户标识 diff --git a/src/store/index.js b/src/store/index.js index 9bfa6a7..c7e23ff 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -3,6 +3,7 @@ import app from './modules/app' import user from './modules/user' import permission from './modules/permission' +import tagsView from './modules/tagsView' import getters from './getters' Vue.use(Vuex) @@ -11,7 +12,8 @@ modules: { app, user, - permission + permission, + tagsView }, getters }) diff --git a/src/router/index.js b/src/router/index.js index b5e4cf8..5879617 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -47,12 +47,14 @@ { path: '/', component: Layout, - redirect: 'overview', + redirect: 'dashboard', name: 'Dashboard', - hidden: true, + // hidden: true, children: [{ path: 'dashboard', - component: () => import('@/views/dashboard/index') + component: () => import('@/views/dashboard/index'), + name: 'Dashboard', + meta: { title: '首页', icon: 'dashboard', noCache: true, affix: true } }] } ] @@ -115,11 +117,11 @@ meta: { title: '告警工单', icon: 'icon-config', keepAlive: true, permission: ['/job'] } }, { - path: '/detailJob', + path: '/detailJob/:id(\\d+)', name: 'DetailJob', component: () => import('@/views/jobManage/detailJob'), hidden: true, - meta: { title: '工单详情', icon: 'icon-config', permission: ['/job'] } + meta: { title: '工单详情', icon: 'icon-config', permission: ['/job/info'] } }, { path: '/listRule', diff --git a/src/store/getters.js b/src/store/getters.js index 1a3b943..a7e0c78 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -1,6 +1,8 @@ const getters = { sidebar: state => state.app.sidebar, // 侧边栏 device: state => state.app.device, // 设备 + visitedViews: state => state.tagsView.visitedViews, + cachedViews: state => state.tagsView.cachedViews, kaptcha: state => state.app.kaptcha, // 验证码 publicKey: state => state.app.publicKey, // 公钥 sid: state => state.app.sid, // 用户标识 diff --git a/src/store/index.js b/src/store/index.js index 9bfa6a7..c7e23ff 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -3,6 +3,7 @@ import app from './modules/app' import user from './modules/user' import permission from './modules/permission' +import tagsView from './modules/tagsView' import getters from './getters' Vue.use(Vuex) @@ -11,7 +12,8 @@ modules: { app, user, - permission + permission, + tagsView }, getters }) diff --git a/src/store/modules/tagsView.js b/src/store/modules/tagsView.js new file mode 100644 index 0000000..50f0df9 --- /dev/null +++ b/src/store/modules/tagsView.js @@ -0,0 +1,165 @@ +const state = { + visitedViews: [], // 用户访问过的页面 + cachedViews: [], // 实际keep-alive的路由,可以在配置路由的时候通过meta.noCache来设置是否需要缓存,默认都缓存 +} + +const mutations = { + ADD_VISITED_VIEW: (state, view) => { + if (state.visitedViews.some(v => v.path === view.path)) return + state.visitedViews.push( + Object.assign({}, view, { + title: view.meta.title || 'no-name' + }) + ) + }, + ADD_CACHED_VIEW: (state, view) => { + if (state.cachedViews.includes(view.name)) return + if (!view.meta.noCache) { + state.cachedViews.push(view.name) + } + }, + + DEL_VISITED_VIEW: (state, view) => { + for (const [i, v] of state.visitedViews.entries()) { + if (v.path === view.path) { + state.visitedViews.splice(i, 1) + break + } + } + }, + DEL_CACHED_VIEW: (state, view) => { + for (const i of state.cachedViews) { + if (i === view.name) { + const index = state.cachedViews.indexOf(i) + state.cachedViews.splice(index, 1) + break + } + } + }, + + DEL_OTHERS_VISITED_VIEWS: (state, view) => { + state.visitedViews = state.visitedViews.filter(v => { + return v.meta.affix || v.path === view.path + }) + }, + DEL_OTHERS_CACHED_VIEWS: (state, view) => { + for (const i of state.cachedViews) { + if (i === view.name) { + const index = state.cachedViews.indexOf(i) + state.cachedViews = state.cachedViews.slice(index, index + 1) + break + } + } + }, + + DEL_ALL_VISITED_VIEWS: state => { + // keep affix tags + const affixTags = state.visitedViews.filter(tag => tag.meta.affix) + state.visitedViews = affixTags + }, + DEL_ALL_CACHED_VIEWS: state => { + state.cachedViews = [] + }, + + UPDATE_VISITED_VIEW: (state, view) => { + for (let v of state.visitedViews) { + if (v.path === view.path) { + v = Object.assign(v, view) + break + } + } + } +} + +const actions = { + addView({ dispatch }, view) { + dispatch('addVisitedView', view) + dispatch('addCachedView', view) + }, + addVisitedView({ commit }, view) { + commit('ADD_VISITED_VIEW', view) + }, + addCachedView({ commit }, view) { + commit('ADD_CACHED_VIEW', view) + }, + + delView({ dispatch, state }, view) { + return new Promise(resolve => { + dispatch('delVisitedView', view) + dispatch('delCachedView', view) + resolve({ + visitedViews: [...state.visitedViews], + cachedViews: [...state.cachedViews] + }) + }) + }, + delVisitedView({ commit, state }, view) { + return new Promise(resolve => { + commit('DEL_VISITED_VIEW', view) + resolve([...state.visitedViews]) + }) + }, + delCachedView({ commit, state }, view) { + return new Promise(resolve => { + commit('DEL_CACHED_VIEW', view) + resolve([...state.cachedViews]) + }) + }, + + delOthersViews({ dispatch, state }, view) { + return new Promise(resolve => { + dispatch('delOthersVisitedViews', view) + dispatch('delOthersCachedViews', view) + resolve({ + visitedViews: [...state.visitedViews], + cachedViews: [...state.cachedViews] + }) + }) + }, + delOthersVisitedViews({ commit, state }, view) { + return new Promise(resolve => { + commit('DEL_OTHERS_VISITED_VIEWS', view) + resolve([...state.visitedViews]) + }) + }, + delOthersCachedViews({ commit, state }, view) { + return new Promise(resolve => { + commit('DEL_OTHERS_CACHED_VIEWS', view) + resolve([...state.cachedViews]) + }) + }, + + delAllViews({ dispatch, state }, view) { + return new Promise(resolve => { + dispatch('delAllVisitedViews', view) + dispatch('delAllCachedViews', view) + resolve({ + visitedViews: [...state.visitedViews], + cachedViews: [...state.cachedViews] + }) + }) + }, + delAllVisitedViews({ commit, state }) { + return new Promise(resolve => { + commit('DEL_ALL_VISITED_VIEWS') + resolve([...state.visitedViews]) + }) + }, + delAllCachedViews({ commit, state }) { + return new Promise(resolve => { + commit('DEL_ALL_CACHED_VIEWS') + resolve([...state.cachedViews]) + }) + }, + + updateVisitedView({ commit }, view) { + commit('UPDATE_VISITED_VIEW', view) + } +} + +export default { + namespaced: true, + state, + mutations, + actions +} diff --git a/src/router/index.js b/src/router/index.js index b5e4cf8..5879617 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -47,12 +47,14 @@ { path: '/', component: Layout, - redirect: 'overview', + redirect: 'dashboard', name: 'Dashboard', - hidden: true, + // hidden: true, children: [{ path: 'dashboard', - component: () => import('@/views/dashboard/index') + component: () => import('@/views/dashboard/index'), + name: 'Dashboard', + meta: { title: '首页', icon: 'dashboard', noCache: true, affix: true } }] } ] @@ -115,11 +117,11 @@ meta: { title: '告警工单', icon: 'icon-config', keepAlive: true, permission: ['/job'] } }, { - path: '/detailJob', + path: '/detailJob/:id(\\d+)', name: 'DetailJob', component: () => import('@/views/jobManage/detailJob'), hidden: true, - meta: { title: '工单详情', icon: 'icon-config', permission: ['/job'] } + meta: { title: '工单详情', icon: 'icon-config', permission: ['/job/info'] } }, { path: '/listRule', diff --git a/src/store/getters.js b/src/store/getters.js index 1a3b943..a7e0c78 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -1,6 +1,8 @@ const getters = { sidebar: state => state.app.sidebar, // 侧边栏 device: state => state.app.device, // 设备 + visitedViews: state => state.tagsView.visitedViews, + cachedViews: state => state.tagsView.cachedViews, kaptcha: state => state.app.kaptcha, // 验证码 publicKey: state => state.app.publicKey, // 公钥 sid: state => state.app.sid, // 用户标识 diff --git a/src/store/index.js b/src/store/index.js index 9bfa6a7..c7e23ff 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -3,6 +3,7 @@ import app from './modules/app' import user from './modules/user' import permission from './modules/permission' +import tagsView from './modules/tagsView' import getters from './getters' Vue.use(Vuex) @@ -11,7 +12,8 @@ modules: { app, user, - permission + permission, + tagsView }, getters }) diff --git a/src/store/modules/tagsView.js b/src/store/modules/tagsView.js new file mode 100644 index 0000000..50f0df9 --- /dev/null +++ b/src/store/modules/tagsView.js @@ -0,0 +1,165 @@ +const state = { + visitedViews: [], // 用户访问过的页面 + cachedViews: [], // 实际keep-alive的路由,可以在配置路由的时候通过meta.noCache来设置是否需要缓存,默认都缓存 +} + +const mutations = { + ADD_VISITED_VIEW: (state, view) => { + if (state.visitedViews.some(v => v.path === view.path)) return + state.visitedViews.push( + Object.assign({}, view, { + title: view.meta.title || 'no-name' + }) + ) + }, + ADD_CACHED_VIEW: (state, view) => { + if (state.cachedViews.includes(view.name)) return + if (!view.meta.noCache) { + state.cachedViews.push(view.name) + } + }, + + DEL_VISITED_VIEW: (state, view) => { + for (const [i, v] of state.visitedViews.entries()) { + if (v.path === view.path) { + state.visitedViews.splice(i, 1) + break + } + } + }, + DEL_CACHED_VIEW: (state, view) => { + for (const i of state.cachedViews) { + if (i === view.name) { + const index = state.cachedViews.indexOf(i) + state.cachedViews.splice(index, 1) + break + } + } + }, + + DEL_OTHERS_VISITED_VIEWS: (state, view) => { + state.visitedViews = state.visitedViews.filter(v => { + return v.meta.affix || v.path === view.path + }) + }, + DEL_OTHERS_CACHED_VIEWS: (state, view) => { + for (const i of state.cachedViews) { + if (i === view.name) { + const index = state.cachedViews.indexOf(i) + state.cachedViews = state.cachedViews.slice(index, index + 1) + break + } + } + }, + + DEL_ALL_VISITED_VIEWS: state => { + // keep affix tags + const affixTags = state.visitedViews.filter(tag => tag.meta.affix) + state.visitedViews = affixTags + }, + DEL_ALL_CACHED_VIEWS: state => { + state.cachedViews = [] + }, + + UPDATE_VISITED_VIEW: (state, view) => { + for (let v of state.visitedViews) { + if (v.path === view.path) { + v = Object.assign(v, view) + break + } + } + } +} + +const actions = { + addView({ dispatch }, view) { + dispatch('addVisitedView', view) + dispatch('addCachedView', view) + }, + addVisitedView({ commit }, view) { + commit('ADD_VISITED_VIEW', view) + }, + addCachedView({ commit }, view) { + commit('ADD_CACHED_VIEW', view) + }, + + delView({ dispatch, state }, view) { + return new Promise(resolve => { + dispatch('delVisitedView', view) + dispatch('delCachedView', view) + resolve({ + visitedViews: [...state.visitedViews], + cachedViews: [...state.cachedViews] + }) + }) + }, + delVisitedView({ commit, state }, view) { + return new Promise(resolve => { + commit('DEL_VISITED_VIEW', view) + resolve([...state.visitedViews]) + }) + }, + delCachedView({ commit, state }, view) { + return new Promise(resolve => { + commit('DEL_CACHED_VIEW', view) + resolve([...state.cachedViews]) + }) + }, + + delOthersViews({ dispatch, state }, view) { + return new Promise(resolve => { + dispatch('delOthersVisitedViews', view) + dispatch('delOthersCachedViews', view) + resolve({ + visitedViews: [...state.visitedViews], + cachedViews: [...state.cachedViews] + }) + }) + }, + delOthersVisitedViews({ commit, state }, view) { + return new Promise(resolve => { + commit('DEL_OTHERS_VISITED_VIEWS', view) + resolve([...state.visitedViews]) + }) + }, + delOthersCachedViews({ commit, state }, view) { + return new Promise(resolve => { + commit('DEL_OTHERS_CACHED_VIEWS', view) + resolve([...state.cachedViews]) + }) + }, + + delAllViews({ dispatch, state }, view) { + return new Promise(resolve => { + dispatch('delAllVisitedViews', view) + dispatch('delAllCachedViews', view) + resolve({ + visitedViews: [...state.visitedViews], + cachedViews: [...state.cachedViews] + }) + }) + }, + delAllVisitedViews({ commit, state }) { + return new Promise(resolve => { + commit('DEL_ALL_VISITED_VIEWS') + resolve([...state.visitedViews]) + }) + }, + delAllCachedViews({ commit, state }) { + return new Promise(resolve => { + commit('DEL_ALL_CACHED_VIEWS') + resolve([...state.cachedViews]) + }) + }, + + updateVisitedView({ commit }, view) { + commit('UPDATE_VISITED_VIEW', view) + } +} + +export default { + namespaced: true, + state, + mutations, + actions +} diff --git a/src/views/layout/Layout.vue b/src/views/layout/Layout.vue index a451849..723a0d4 100644 --- a/src/views/layout/Layout.vue +++ b/src/views/layout/Layout.vue @@ -9,6 +9,8 @@