keepAlive.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * @Author: Badguy
  3. * @Date: 2022-03-08 17:31:03
  4. * @LastEditTime: 2022-05-17 20:14:20
  5. * @LastEditors: your name
  6. * @Description: 页面缓存
  7. * have a nice day!
  8. */
  9. const defaultPages = [{ name: 'BaggageManagement' }]
  10. const savedPages = JSON.parse(sessionStorage.getItem('keepAlivePages'))
  11. const state = {
  12. keepAlivePages: savedPages || defaultPages,
  13. clickedCells: JSON.parse(sessionStorage.getItem('clickedCells')) || []
  14. }
  15. const mutations = {
  16. ADD_PAGE(state, { name, fullPath }) {
  17. if (state.keepAlivePages.every(page => page.name !== name)) {
  18. state.keepAlivePages = [...state.keepAlivePages, { name, fullPath }]
  19. }
  20. sessionStorage.setItem('keepAlivePages', JSON.stringify(state.keepAlivePages))
  21. },
  22. EDIT_PAGE(state, { name, fullPath }) {
  23. state.keepAlivePages = state.keepAlivePages.map(page => {
  24. if (page.name === name) {
  25. page.fullPath = fullPath
  26. }
  27. return page
  28. })
  29. sessionStorage.setItem('keepAlivePages', JSON.stringify(state.keepAlivePages))
  30. },
  31. DELETE_PAGE(state, { name }) {
  32. const index = state.keepAlivePages.findIndex(page => page.name === name)
  33. if (index > -1) {
  34. state.keepAlivePages = state.keepAlivePages.slice(0, index + 1)
  35. } else {
  36. state.keepAlivePages = defaultPages
  37. }
  38. sessionStorage.setItem('keepAlivePages', JSON.stringify(state.keepAlivePages))
  39. },
  40. ADD_CLICKED_CELL(state, cell) {
  41. state.clickedCells.some(item =>
  42. Object.keys(item).every(key => {
  43. item[key] === cell[key]
  44. })
  45. ) || (state.clickedCells.push(cell), sessionStorage.setItem('clickedCells', JSON.stringify(state.clickedCells)))
  46. }
  47. }
  48. const actions = {
  49. addPage({ commit }, page) {
  50. commit('ADD_PAGE', page)
  51. },
  52. deletePage({ commit }, page) {
  53. commit('DELETE_PAGE', page)
  54. },
  55. editPage({ commit }, page) {
  56. commit('EDIT_PAGE', page)
  57. },
  58. addClickedCell({ commit }, cell) {
  59. commit('ADD_CLICKED_CELL', cell)
  60. }
  61. }
  62. export default {
  63. namespaced: true,
  64. state,
  65. mutations,
  66. actions
  67. }