index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. localStorage.setItem(this.saveKey, JSON.stringify(this.searchs))
  15. }
  16. parse() {
  17. let json = {...initSearch}
  18. try {
  19. const val = localStorage.getItem(this.saveKey)
  20. json = JSON.parse(val) || json
  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. localStorage.setItem(this.saveKey, JSON.stringify(this.searchs))
  49. return this.searchs
  50. }
  51. update(data, key, type) {
  52. this.searchs = this.parse()
  53. const k = (key || this.key)
  54. if (!this.searchs[k]) {
  55. this.searchs[k] = {...initSearch}
  56. }
  57. if (type) {
  58. this.searchs[k][type] = data
  59. } else {
  60. this.searchs[k] = data
  61. }
  62. this.save()
  63. return this.searchs
  64. }
  65. }
  66. const initSearch = {
  67. form: {},
  68. page: {},
  69. }