historyStack.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. interface IHistory {
  2. name?: string
  3. pic?: number
  4. id?: number
  5. }
  6. class HistoryStack {
  7. private item: IHistory[]
  8. private wrap: {}
  9. constructor () {
  10. this.item = []
  11. this.wrap = {}
  12. this.init([])
  13. }
  14. public init (projects) {
  15. const store = localStorage.getItem('historyBrowser')
  16. const user = this.getUser()
  17. if (store && store.length) {
  18. const result = this.parse(store)
  19. this.wrap = result
  20. if (user) {
  21. const items = this.wrap[user]
  22. if (items && items.length) {
  23. this.item = items
  24. this.wrap[user] = this.item
  25. } else {
  26. this.item = []
  27. this.wrap[user] = this.item
  28. }
  29. }
  30. } else {
  31. if (user) {
  32. this.item = []
  33. this.wrap[user] = this.item
  34. }
  35. }
  36. const historyArr = []
  37. if (this.wrap && this.wrap[user] && this.wrap[user].length) {
  38. this.wrap[user].forEach((historyItem) => {
  39. projects.forEach((projectItem) => {
  40. if (historyItem.id === projectItem.id) {
  41. historyArr.push(projectItem)
  42. }
  43. })
  44. })
  45. }
  46. this.wrap[user] = historyArr
  47. }
  48. public pushNode (d?: IHistory) {
  49. const user = this.getUser()
  50. const store = localStorage.getItem('historyBrowser')
  51. if (store) {
  52. const result = this.parse(store)
  53. this.wrap = result
  54. if (user && user.length) {
  55. if (result && result[user]) {
  56. const userArr = result[user]
  57. this.item = (userArr && Array.isArray(userArr)) ? userArr : []
  58. this.wrap[user] = this.item
  59. } else {
  60. this.item = []
  61. this.wrap[user] = this.item
  62. }
  63. }
  64. } else {
  65. if (user) {
  66. this.item = []
  67. this.wrap[user] = this.item
  68. }
  69. }
  70. if (d) {
  71. this.item = this.item.filter((t) => t.id !== d.id)
  72. this.item.unshift(d)
  73. this.save()
  74. }
  75. }
  76. private getUser () {
  77. const user = localStorage.getItem('loginUser')
  78. const userObj = this.parse(user)
  79. if (userObj && userObj.id) {
  80. return userObj.id
  81. }
  82. return false
  83. }
  84. private save () {
  85. const user = this.getUser()
  86. if (user) {
  87. this.wrap[user] = this.item
  88. }
  89. localStorage.setItem('historyBrowser', this.stringify(this.wrap))
  90. }
  91. private parse (str: string) {
  92. try {
  93. if (str) {
  94. return JSON.parse(str)
  95. }
  96. } catch (err) {
  97. throw new Error(err)
  98. }
  99. }
  100. private stringify (data) {
  101. try {
  102. if (data) {
  103. return JSON.stringify(data)
  104. }
  105. } catch (err) {
  106. throw new Error(err)
  107. }
  108. }
  109. public clear () {
  110. this.item.length = 0
  111. }
  112. public getAll () {
  113. const user = this.getUser()
  114. if (user) {
  115. const items = this.wrap[user]
  116. if (items && items.length) {
  117. this.item = items
  118. this.wrap[user] = this.item
  119. } else {
  120. this.item = []
  121. this.wrap[user] = this.item
  122. }
  123. const projectList = this.wrap[user]
  124. return {
  125. projectList,
  126. proIdList: projectList.map((pro) => pro.id)
  127. }
  128. } else {
  129. return {
  130. projectList: [],
  131. proIdList: []
  132. }
  133. }
  134. }
  135. }
  136. export default HistoryStack