indexDB.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {defineStore} from "pinia";
  2. import {readonly} from "vue";
  3. import {ElLoading, ElNotification} from "element-plus";
  4. import {useThemeStore} from "@/stores/theme";
  5. export const useIndexDBStore = defineStore('indexDB', {
  6. state: () => ({
  7. DB: <any>null
  8. }),
  9. getters: {
  10. },
  11. actions: {
  12. initDB() {
  13. return new Promise((resolve, reject) => {
  14. const loading = ElLoading.service({
  15. lock: true,
  16. text: '数据库加载中',
  17. background: useThemeStore().loadingBg,
  18. })
  19. const request: any = window.indexedDB.open('SmartSearch', 1)
  20. request.onerror = (event) => {}
  21. request.onupgradeneeded = (event: any) => {
  22. this.DB = event.target.result
  23. if (!this.DB.objectStoreNames.contains('timestamp')) {
  24. const objectStore = this.DB.createObjectStore('timestamp', { keyPath: 'timestamp' })
  25. }
  26. }
  27. request.onsuccess = (event) => {
  28. this.DB = event.target.result
  29. resolve(this.DB)
  30. loading.close()
  31. }
  32. })
  33. },
  34. setTimestamp(params) {
  35. return new Promise((resolve, reject) => {
  36. const loading = ElLoading.service({
  37. lock: true,
  38. text: '查询参数构建中',
  39. background: useThemeStore().loadingBg,
  40. })
  41. const timestamp = String(new Date().getTime())
  42. const obj = {
  43. timestamp,
  44. json: JSON.stringify(params)
  45. }
  46. const re = this.DB.transaction(['timestamp'], 'readwrite').objectStore('timestamp').add(obj)
  47. re.onsuccess = (e: any) => {
  48. resolve(timestamp)
  49. loading.close()
  50. }
  51. })
  52. },
  53. getTimestamp(timestamp) {
  54. return new Promise((resolve, reject) => {
  55. const loading = ElLoading.service({
  56. lock: true,
  57. text: '查询参数解析中',
  58. background: useThemeStore().loadingBg,
  59. })
  60. const re = this.DB.transaction(['timestamp']).objectStore('timestamp').get(timestamp)
  61. re.onsuccess = (e: any) => {
  62. if (re.result) {
  63. resolve(JSON.parse(re.result.json))
  64. loading.close()
  65. } else {
  66. ElNotification({
  67. duration: 0,
  68. title: '提示',
  69. message: '查询条件获取失败,请重新输入!',
  70. type: 'warning',
  71. onClose: () => {
  72. }
  73. })
  74. reject()
  75. loading.close()
  76. }
  77. }
  78. })
  79. },
  80. clearTimestamp() {
  81. return new Promise((resolve, reject) => {
  82. const loading = ElLoading.service({
  83. lock: true,
  84. text: '数据库创建中',
  85. background: useThemeStore().loadingBg,
  86. })
  87. if (this.DB.objectStoreNames.contains('timestamp')) {
  88. const oldRe = this.DB.transaction(['timestamp'])?.objectStore('timestamp')
  89. oldRe.openCursor().onsuccess = (e: any) => {
  90. const cursor = e.target.result
  91. if (cursor) {
  92. this.DB.transaction(['timestamp'], 'readwrite').objectStore('timestamp').delete(cursor.primaryKey)
  93. cursor.continue();
  94. } else {
  95. resolve('')
  96. loading.close()
  97. }
  98. }
  99. } else {
  100. resolve('')
  101. loading.close()
  102. }
  103. })
  104. }
  105. },
  106. })