index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* eslint-disable no-empty */
  2. export class Searchs {
  3. saveKey = 'searchs'
  4. initSearch = {
  5. form: {},
  6. page: {},
  7. }
  8. searchs = {}
  9. constructor(key) {
  10. this.key = key
  11. this.searchs = this.parse()
  12. }
  13. save() {
  14. sessionStorage.setItem(this.saveKey, JSON.stringify(this.searchs))
  15. }
  16. parse() {
  17. let json = {}
  18. try {
  19. const val = sessionStorage.getItem(this.saveKey)
  20. json = JSON.parse(val) || initSearch
  21. } catch (error) {}
  22. return json
  23. }
  24. get(key) {
  25. const k = (key || this.key)
  26. if (!this.searchs[k]) {
  27. this.searchs[k] = {...initSearch}
  28. }
  29. return this.searchs[k]
  30. }
  31. remove(type) {
  32. if (this.searchs && this.searchs[this.key]) {
  33. type ? this.searchs[this.key][type] : this.searchs[this.key]
  34. this.save()
  35. }
  36. return this.searchs
  37. }
  38. getSearchs() {
  39. return this.searchs
  40. }
  41. removeByKey(key) {
  42. delete this.searchs[key]
  43. this.save()
  44. return this.searchs
  45. }
  46. removeAll() {
  47. this.searchs = {}
  48. sessionStorage.setItem(this.saveKey, JSON.stringify(this.searchs))
  49. return this.searchs
  50. }
  51. update(data, key, type) {
  52. const k = (key || this.key)
  53. if (type) {
  54. this.searchs[k][type] = data
  55. } else {
  56. this.searchs[k] = data
  57. }
  58. this.save()
  59. return this.searchs
  60. }
  61. }
  62. const initSearch = {
  63. form: {},
  64. page: {},
  65. }