vue2-client 1.22.2 → 1.22.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +30 -30
- package/.env.his +19 -19
- package/.eslintrc.js +74 -74
- package/.history/.eslintrc_20260521171150.js +74 -0
- package/.history/.eslintrc_20260521171213.js +74 -0
- package/.history/src/base-client/components/common/HIS/HAddNativeForm/HAddNativeForm_20260601154443.vue +726 -0
- package/.history/src/base-client/components/common/HIS/HAddNativeForm/HAddNativeForm_20260601154700.vue +478 -0
- package/.history/src/base-client/components/common/HIS/HButtons/HButtons_20260512175435.vue +706 -0
- package/.history/src/base-client/components/common/HIS/HButtons/HButtons_20260512175450.vue +694 -0
- package/.history/src/base-client/components/common/HIS/HButtons/HButtons_20260611152602.vue +755 -0
- package/.history/src/base-client/components/common/HIS/HForm/HForm_20260513145941.vue +524 -0
- package/.history/src/base-client/components/common/HIS/HForm/HForm_20260513153133.vue +731 -0
- package/.history/src/base-client/components/common/HIS/HForm/HForm_20260513160316.vue +525 -0
- package/.history/src/base-client/components/common/HIS/HForm/HForm_20260601144150.vue +1046 -0
- package/.history/src/base-client/components/common/HIS/HFormTable/HFormTable_20260310142713.vue +512 -0
- package/.history/src/base-client/components/common/HIS/HFormTable/HFormTable_20260310145118.vue +511 -0
- package/.history/src/base-client/components/common/HIS/HFormTable/HFormTable_20260311094834.vue +696 -0
- package/.history/src/base-client/components/common/HIS/HFormTable/HFormTable_20260320143028.vue +693 -0
- package/.history/src/base-client/components/common/HIS/HFormTable/HFormTable_20260409101450.vue +677 -0
- package/.history/src/base-client/components/common/HIS/HTab/HTab_20260508164645.vue +758 -0
- package/.history/src/base-client/components/common/HIS/HTab/HTab_20260508164714.vue +693 -0
- package/.history/src/base-client/components/common/HIS/HTab/HTab_20260508171651.vue +716 -0
- package/.history/src/base-client/components/common/HIS/HTab/HTab_20260509133717.vue +695 -0
- package/.history/src/base-client/components/common/HIS/HTab/HTab_20260509171115.vue +664 -0
- package/.history/src/base-client/components/common/XAddNativeForm/XAddNativeForm_20260513140637.vue +1455 -0
- package/.history/src/base-client/components/common/XAddNativeForm/XAddNativeForm_20260513140935.vue +1441 -0
- package/.history/src/base-client/components/common/XAddNativeForm/XAddNativeForm_20260513150818.vue +1441 -0
- package/.history/src/base-client/components/common/XAddNativeForm/XAddNativeForm_20260513153119.vue +1442 -0
- package/.history/src/base-client/components/common/XAddNativeForm/XAddNativeForm_20260513153126.vue +1486 -0
- package/.history/src/base-client/components/common/XForm/XFormItem_20260513140854.vue +1607 -0
- package/.history/src/base-client/components/common/XMarkdownViewer/XMarkdownViewer_20260519140403.vue +643 -0
- package/.history/src/base-client/components/common/XMarkdownViewer/XMarkdownViewer_20260519140829.vue +628 -0
- package/.history/src/base-client/components/common/XMarkdownViewer/demo_20260519142824.vue +104 -0
- package/.history/src/base-client/components/common/XMarkdownViewer/demo_20260519143155.vue +102 -0
- package/.history/src/base-client/components/common/XReportGrid/XReport_20260309171231.vue +1241 -0
- package/.history/src/base-client/components/common/XReportGrid/XReport_20260309171441.vue +1223 -0
- package/.history/src/base-client/components/his/HAi/HAi_20260612174826.vue +472 -0
- package/.history/src/base-client/components/his/HAi/HAi_20260612175839.vue +538 -0
- package/.history/src/base-client/components/his/HAi/HAi_20260615103331.vue +650 -0
- package/.history/src/base-client/components/his/XHDescriptions/XHDescriptions_20260424134504.vue +1469 -0
- package/.history/src/base-client/components/his/XSidebar/XSidebar_20260610171133.vue +788 -0
- package/.history/src/base-client/components/his/XSidebar/XSidebar_20260610171151.vue +780 -0
- package/.history/src/base-client/components/his/XTransfer/XTransfer_20260511170841.vue +585 -0
- package/.history/src/base-client/components/his/XTransfer/XTransfer_20260511171138.vue +787 -0
- package/.history/src/base-client/components/his/XTransfer/XTransfer_20260512141830.vue +739 -0
- package/.history/src/components/STable/index_20260409155138.js +806 -0
- package/.history/src/components/STable/index_20260409155218.js +814 -0
- package/.history/src/expression/core/Expression_20260305164427.js +1371 -0
- package/.history/src/expression/core/Expression_20260305170258.js +1358 -0
- package/.history/src/expression/core/Program_20260305111830.js +944 -0
- package/.history/src/expression/core/Program_20260305112041.js +931 -0
- package/.history/src/logic/LogicRunner_20260304154306.js +170 -0
- package/.history/src/logic/LogicRunner_20260304155553.js +112 -0
- package/.history/src/logic/LogicRunner_20260305105834.js +112 -0
- package/.history/src/logic/LogicRunner_20260305112718.js +129 -0
- package/.history/src/logic/LogicRunner_20260305182436.js +133 -0
- package/.history/src/logic/LogicRunner_20260306151301.js +213 -0
- package/.history/src/logic/LogicRunner_20260306152419.js +213 -0
- package/.history/src/logic/plugins/common/DateTools_20260305154159.js +61 -0
- package/.history/src/logic/plugins/common/DateTools_20260305154217.js +44 -0
- package/.history/src/logic/plugins/common/DateTools_20260305161014.js +44 -0
- package/.history/src/logic/plugins/common/HttpTools_20260305164352.js +80 -0
- package/.history/src/logic/plugins/common/HttpTools_20260305170258.js +75 -0
- package/.history/src/logic/plugins/common/HttpTools_20260305171634.js +75 -0
- package/.history/src/logic/plugins/common/HttpTools_20260306152419.js +72 -0
- package/.history/src/services/api/restTools_20260427142149.js +245 -0
- package/.history/src/services/api/restTools_20260427142853.js +230 -0
- package/.history/src/services/api/restTools_20260519135558.js +230 -0
- package/.history/src/services/api/restTools_20260519140825.js +230 -0
- package/.history/src/services/api/restTools_20260519151223.js +230 -0
- package/.history/src/utils/indexedDB_20260306150918.js +593 -0
- package/.history/src/utils/indexedDB_20260306151301.js +586 -0
- package/.idea/af-vue2-client.iml +9 -0
- package/.idea/codeStyles/Project.xml +62 -0
- package/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/.idea/misc.xml +6 -0
- package/.idea/modules.xml +1 -1
- package/Components.md +60 -60
- package/index.js +31 -31
- package/jest-transform-stub.js +8 -8
- package/jest.setup.js +7 -7
- package/package.json +1 -1
- package/preview-input-box.html +180 -0
- package/src/assets/img/querySlotDemo.svg +15 -15
- package/src/base-client/components/common/AmapMarker/AmapPointRendering.vue +120 -120
- package/src/base-client/components/common/CitySelect/index.js +3 -3
- package/src/base-client/components/common/CitySelect/index.md +109 -109
- package/src/base-client/components/common/FormGroupEdit/index.js +3 -3
- package/src/base-client/components/common/FormGroupEdit/index.md +43 -43
- package/src/base-client/components/common/HIS/HButtons/HButtons.vue +55 -1
- package/src/base-client/components/common/HIS/HForm/HForm.vue +1186 -1186
- package/src/base-client/components/common/HIS/HTab/HTab.vue +88 -1
- package/src/base-client/components/common/JSONToTree/jsontotree.vue +271 -271
- package/src/base-client/components/common/PersonSetting/index.js +3 -3
- package/src/base-client/components/common/Tree/index.js +2 -2
- package/src/base-client/components/common/Upload/index.js +3 -3
- package/src/base-client/components/common/XAddNativeForm/index.md +146 -146
- package/src/base-client/components/common/XAddReport/XAddReport.vue +16 -1
- package/src/base-client/components/common/XCard/XCard.vue +64 -64
- package/src/base-client/components/common/XDataDrawer/XDataDrawer.vue +180 -180
- package/src/base-client/components/common/XDataDrawer/index.js +3 -3
- package/src/base-client/components/common/XDataDrawer/index.md +41 -41
- package/src/base-client/components/common/XDescriptions/index.js +3 -3
- package/src/base-client/components/common/XDescriptions/index.md +382 -382
- package/src/base-client/components/common/XForm/index.md +178 -178
- package/src/base-client/components/common/XInput/XInput.vue +32 -1
- package/src/base-client/components/common/XInspectionDetailDrawer/index.vue +1 -1
- package/src/base-client/components/common/XMarkdownViewer/demo.vue +102 -102
- package/src/base-client/components/common/XStepView/XStepView.vue +252 -252
- package/src/base-client/components/common/XStepView/index.js +3 -3
- package/src/base-client/components/common/XStepView/index.md +31 -31
- package/src/base-client/components/common/XTable/index.md +255 -255
- package/src/base-client/components/his/HAi/HAi.vue +1177 -436
- package/src/base-client/components/his/XList/XList.vue +337 -58
- package/src/base-client/components/his/XSidebar/XSidebar.vue +36 -12
- package/src/base-client/components/his/XTransfer/index.md +327 -327
- package/src/base-client/components/system/DictionaryDetailsView/DictionaryDetailsView.vue +232 -232
- package/src/base-client/plugins/Config.js +19 -19
- package/src/base-client/plugins/tabs-page-plugin.js +39 -39
- package/src/components/Charts/Bar.vue +62 -62
- package/src/components/Charts/ChartCard.vue +134 -134
- package/src/components/Charts/Liquid.vue +67 -67
- package/src/components/Charts/MiniArea.vue +39 -39
- package/src/components/Charts/MiniBar.vue +39 -39
- package/src/components/Charts/MiniProgress.vue +75 -75
- package/src/components/Charts/MiniSmoothArea.vue +40 -40
- package/src/components/Charts/Radar.vue +68 -68
- package/src/components/Charts/RankList.vue +77 -77
- package/src/components/Charts/TagCloud.vue +113 -113
- package/src/components/Charts/TransferBar.vue +64 -64
- package/src/components/Charts/Trend.vue +82 -82
- package/src/components/Charts/chart.less +12 -12
- package/src/components/Charts/smooth.area.less +13 -13
- package/src/components/NumberInfo/NumberInfo.vue +54 -54
- package/src/components/NumberInfo/index.js +3 -3
- package/src/components/NumberInfo/index.less +54 -54
- package/src/components/NumberInfo/index.md +43 -43
- package/src/components/STable/index.js +953 -953
- package/src/components/card/ChartCard.vue +79 -79
- package/src/components/chart/Bar.vue +60 -60
- package/src/components/chart/MiniArea.vue +67 -67
- package/src/components/chart/MiniBar.vue +59 -59
- package/src/components/chart/MiniProgress.vue +57 -57
- package/src/components/chart/Radar.vue +80 -80
- package/src/components/chart/RankingList.vue +60 -60
- package/src/components/chart/Trend.vue +79 -79
- package/src/components/chart/index.less +9 -9
- package/src/components/checkbox/ColorCheckbox.vue +157 -157
- package/src/components/input/IInput.vue +66 -66
- package/src/components/menu/SideMenu.vue +75 -75
- package/src/components/menu/menu.js +273 -273
- package/src/components/tool/AStepItem.vue +60 -60
- package/src/layouts/CommonLayout.vue +56 -56
- package/src/lib.js +1 -1
- package/src/mock/extend/index.js +84 -84
- package/src/mock/goods/index.js +108 -108
- package/src/pages/dashboard/workplace/WorkPlace.vue +141 -141
- package/src/pages/system/dictionary/index.vue +44 -44
- package/src/pages/system/monitor/loginInfor/index.vue +37 -37
- package/src/pages/system/monitor/operLog/index.vue +37 -37
- package/src/services/api/cas.js +79 -79
- package/src/store/modules/setting.js +119 -119
- package/src/utils/errorCode.js +6 -6
- package//350/277/201/347/247/273/346/227/245/345/277/227.md +15 -15
- package/.idea/MarsCodeWorkspaceAppSettings.xml +0 -7
- package/.idea/google-java-format.xml +0 -6
- package/.idea/inspectionProfiles/Project_Default.xml +0 -24
- package/.idea/jsLinters/eslint.xml +0 -6
- package/.idea/vue2-client.iml +0 -12
- package/.vscode/settings.json +0 -28
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
import { post } from '@vue2-client/services/api'
|
|
2
|
+
|
|
3
|
+
// 内存缓存作为IndexedDB的备用方案,防止数据库损坏时应用崩溃
|
|
4
|
+
const MEMORY_CACHE = new Map()
|
|
5
|
+
const USE_MEMORY_CACHE = true // 是否启用内存缓存
|
|
6
|
+
|
|
7
|
+
const DB_CONFIG = {
|
|
8
|
+
NAME: window.__MICRO_APP_NAME__ ? `view_${window.__MICRO_APP_NAME__}` : 'view',
|
|
9
|
+
STORE_NAME: 'metaCache',
|
|
10
|
+
VERSION: 1,
|
|
11
|
+
CURRENT_VERSION: 1
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// 数据库健康状态标记,用于在数据库异常时切换到备用方案
|
|
15
|
+
let IS_DB_HEALTHY = true
|
|
16
|
+
|
|
17
|
+
const LIULI_WEB_DB_NAME = 'view_liuli_web'
|
|
18
|
+
|
|
19
|
+
export class IndexedDBManager {
|
|
20
|
+
constructor () {
|
|
21
|
+
this.db = undefined
|
|
22
|
+
this.locks = new Map()
|
|
23
|
+
this.requestTimeout = 10000
|
|
24
|
+
this.isInMicroApp = !!window.__MICRO_APP_NAME__
|
|
25
|
+
this.microAppName = window.__MICRO_APP_NAME__ || ''
|
|
26
|
+
this.indexedDB = window?.rawWindow?.indexedDB || window.indexedDB || window.webkitindexedDB
|
|
27
|
+
this.IDBKeyRange = window?.rawWindow?.IDBKeyRange || window.IDBKeyRange || window.webkitIDBKeyRange
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async openDatabase () {
|
|
31
|
+
try {
|
|
32
|
+
return await new Promise((resolve, reject) => {
|
|
33
|
+
// 添加超时机制,防止请求永久挂起
|
|
34
|
+
const timeout = setTimeout(() => {
|
|
35
|
+
console.error('[openDatabase] 请求超时,将尝试重建数据库')
|
|
36
|
+
reject(new Error('IndexedDB open timeout'))
|
|
37
|
+
}, 5000) // 5秒超时
|
|
38
|
+
|
|
39
|
+
const checkRequest = this.indexedDB.open(DB_CONFIG.NAME)
|
|
40
|
+
|
|
41
|
+
checkRequest.onsuccess = (e) => {
|
|
42
|
+
clearTimeout(timeout) // 清除超时
|
|
43
|
+
const db = e.target.result
|
|
44
|
+
const currentVersion = db.version
|
|
45
|
+
db.close()
|
|
46
|
+
|
|
47
|
+
DB_CONFIG.CURRENT_VERSION = Math.max(currentVersion, DB_CONFIG.VERSION)
|
|
48
|
+
const request = this.indexedDB.open(DB_CONFIG.NAME, DB_CONFIG.CURRENT_VERSION)
|
|
49
|
+
|
|
50
|
+
// 为第二次打开也添加超时
|
|
51
|
+
const openTimeout = setTimeout(() => {
|
|
52
|
+
console.error('[openDatabase] 二次打开请求超时')
|
|
53
|
+
reject(new Error('IndexedDB second open timeout'))
|
|
54
|
+
}, 5000)
|
|
55
|
+
|
|
56
|
+
request.onerror = (e) => {
|
|
57
|
+
clearTimeout(openTimeout)
|
|
58
|
+
reject(e.currentTarget.error)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
request.onsuccess = (e) => {
|
|
62
|
+
clearTimeout(openTimeout)
|
|
63
|
+
const db = e.target.result
|
|
64
|
+
if (!db.objectStoreNames.contains(DB_CONFIG.STORE_NAME)) {
|
|
65
|
+
db.close()
|
|
66
|
+
DB_CONFIG.CURRENT_VERSION++
|
|
67
|
+
this.upgradeDatabase(resolve, reject)
|
|
68
|
+
} else {
|
|
69
|
+
resolve(db)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
request.onupgradeneeded = (e) => {
|
|
74
|
+
clearTimeout(openTimeout)
|
|
75
|
+
const db = e.target.result
|
|
76
|
+
if (!db.objectStoreNames.contains(DB_CONFIG.STORE_NAME)) {
|
|
77
|
+
db.createObjectStore(DB_CONFIG.STORE_NAME, { keyPath: 'key' })
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
checkRequest.onerror = (e) => {
|
|
83
|
+
clearTimeout(timeout)
|
|
84
|
+
reject(e.currentTarget.error)
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
} catch (error) {
|
|
88
|
+
console.error('[openDatabase] 打开数据库失败:', error)
|
|
89
|
+
|
|
90
|
+
// 如果是超时错误或版本错误,尝试自动重建数据库
|
|
91
|
+
if (error.message?.includes('timeout') || error.message?.includes('version')) {
|
|
92
|
+
await this.forceRecreateDatabase()
|
|
93
|
+
return this.openDatabase() // 递归调用,重新尝试打开
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
throw error
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async upgradeDatabase (resolve, reject) {
|
|
101
|
+
console.warn('[upgradeDatabase] 升级数据库到版本:', DB_CONFIG.CURRENT_VERSION)
|
|
102
|
+
const request = this.indexedDB.open(DB_CONFIG.NAME, DB_CONFIG.CURRENT_VERSION)
|
|
103
|
+
|
|
104
|
+
request.onupgradeneeded = (e) => {
|
|
105
|
+
const db = e.target.result
|
|
106
|
+
if (!db.objectStoreNames.contains(DB_CONFIG.STORE_NAME)) {
|
|
107
|
+
db.createObjectStore(DB_CONFIG.STORE_NAME, { keyPath: 'key' })
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
request.onsuccess = (e) => {
|
|
112
|
+
resolve(e.target.result)
|
|
113
|
+
}
|
|
114
|
+
request.onerror = (e) => {
|
|
115
|
+
reject(e.currentTarget.error)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async openDB (callback) {
|
|
120
|
+
// 如果数据库不健康,直接调用回调并返回
|
|
121
|
+
if (!IS_DB_HEALTHY) {
|
|
122
|
+
console.warn('[openDB] 数据库不健康,跳过数据库操作')
|
|
123
|
+
setTimeout(() => {
|
|
124
|
+
try {
|
|
125
|
+
callback(null)
|
|
126
|
+
} catch (e) {
|
|
127
|
+
console.error('[openDB] 回调执行错误:', e)
|
|
128
|
+
}
|
|
129
|
+
}, 0)
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
if (this.db) {
|
|
135
|
+
const isAlive = await this.checkConnection()
|
|
136
|
+
if (isAlive) {
|
|
137
|
+
callback(this.db)
|
|
138
|
+
return
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 添加超时机制,防止卡死
|
|
143
|
+
const openDbPromise = this.openDatabase()
|
|
144
|
+
const timeoutPromise = new Promise((resolve, reject) => {
|
|
145
|
+
setTimeout(() => reject(new Error('openDB timeout')), 5000)
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
this.db = await Promise.race([openDbPromise, timeoutPromise])
|
|
150
|
+
callback(this.db)
|
|
151
|
+
} catch (error) {
|
|
152
|
+
console.error('[openDB] 打开数据库连接超时或失败', error)
|
|
153
|
+
// 标记数据库为不健康状态,后续操作使用备用方案
|
|
154
|
+
IS_DB_HEALTHY = false
|
|
155
|
+
try {
|
|
156
|
+
callback(null)
|
|
157
|
+
} catch (callbackError) {
|
|
158
|
+
console.error('[openDB] 回调执行错误:', callbackError)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
} catch (error) {
|
|
162
|
+
console.error('[openDB] 数据库操作失败:', error)
|
|
163
|
+
|
|
164
|
+
// 版本错误时尝试重建数据库
|
|
165
|
+
if (error.message?.includes('version')) {
|
|
166
|
+
try {
|
|
167
|
+
await this.recreateDatabase()
|
|
168
|
+
callback(this.db)
|
|
169
|
+
} catch (rebuildError) {
|
|
170
|
+
console.error('[openDB] 重建失败,标记数据库为不健康状态', rebuildError)
|
|
171
|
+
IS_DB_HEALTHY = false
|
|
172
|
+
try {
|
|
173
|
+
callback(null)
|
|
174
|
+
} catch (callbackError) {
|
|
175
|
+
console.error('[openDB] 回调执行错误:', callbackError)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
// 其他错误,标记为不健康并使用备用方案
|
|
180
|
+
IS_DB_HEALTHY = false
|
|
181
|
+
console.error('[openDB] 不可恢复的错误,标记数据库为不健康状态')
|
|
182
|
+
try {
|
|
183
|
+
callback(null)
|
|
184
|
+
} catch (callbackError) {
|
|
185
|
+
console.error('[openDB] 回调执行错误:', callbackError)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async checkConnection () {
|
|
192
|
+
try {
|
|
193
|
+
const transaction = this.db.transaction([DB_CONFIG.STORE_NAME], 'readwrite')
|
|
194
|
+
const store = transaction.objectStore(DB_CONFIG.STORE_NAME)
|
|
195
|
+
await this.promisifyRequest(store.add({ key: 'alive', data: true }))
|
|
196
|
+
return true
|
|
197
|
+
} catch (error) {
|
|
198
|
+
return false
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
promisifyRequest (request) {
|
|
203
|
+
return new Promise((resolve, reject) => {
|
|
204
|
+
request.onsuccess = () => {
|
|
205
|
+
resolve(request.result)
|
|
206
|
+
}
|
|
207
|
+
request.onerror = () => {
|
|
208
|
+
reject(request.error)
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async getByWeb (key, url, params, callback, processFun) {
|
|
214
|
+
// 数据库不健康时使用备用方案
|
|
215
|
+
if (!IS_DB_HEALTHY) {
|
|
216
|
+
console.warn(`[getByWeb] 数据库不健康,使用内存缓存或直接API调用`)
|
|
217
|
+
return this.getByWebWithoutDB(key, url, params, callback, processFun)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// 锁机制:防止同一键的并发请求
|
|
221
|
+
if (this.locks.has(key)) {
|
|
222
|
+
const timeoutPromise = new Promise((resolve, reject) => {
|
|
223
|
+
setTimeout(() => {
|
|
224
|
+
reject(new Error(`请求超时: ${key}`))
|
|
225
|
+
}, this.requestTimeout)
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
await Promise.race([this.locks.get(key), timeoutPromise])
|
|
230
|
+
return this.getByWeb(key, url, params, callback, processFun)
|
|
231
|
+
} catch (error) {
|
|
232
|
+
console.error(`[getByWeb] 等待锁期间出错或超时: ${error.message}`)
|
|
233
|
+
this.locks.delete(key)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const lockPromise = (async () => {
|
|
238
|
+
try {
|
|
239
|
+
// 首先检查内存缓存
|
|
240
|
+
if (USE_MEMORY_CACHE && MEMORY_CACHE.has(key)) {
|
|
241
|
+
const cachedData = MEMORY_CACHE.get(key)
|
|
242
|
+
console.log(`[indexedDB.getByWeb] 使用内存缓存,key: ${key}`)
|
|
243
|
+
callback(cachedData)
|
|
244
|
+
return
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// 再检查IndexedDB缓存
|
|
248
|
+
const data = DB_CONFIG.NAME === LIULI_WEB_DB_NAME ? undefined : await this.getData(key)
|
|
249
|
+
|
|
250
|
+
if (data && data.ERROR) {
|
|
251
|
+
console.error(`获取配置失败:` + data.ERROR)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (!data && url) {
|
|
255
|
+
// 缓存未命中,请求网络数据
|
|
256
|
+
console.log(`[indexedDB.getByWeb] 缓存未命中,发起网络请求,key: ${key}`)
|
|
257
|
+
} else if (data) {
|
|
258
|
+
console.log(`[indexedDB.getByWeb] 使用 IndexedDB 缓存,key: ${key}`)
|
|
259
|
+
} else {
|
|
260
|
+
console.log(`[indexedDB.getByWeb] 缓存不存在,key: ${key}, url: ${url}`)
|
|
261
|
+
}
|
|
262
|
+
// 获取当前 Logic 的 abortSignal,支持 cancel 模式(使用动态导入避免循环依赖)
|
|
263
|
+
let abortSignal = null
|
|
264
|
+
let abortControllerInfo = 'none'
|
|
265
|
+
try {
|
|
266
|
+
const LogicRunner = (await import('../logic/LogicRunner')).default
|
|
267
|
+
abortSignal = LogicRunner.getCurrentAbortSignal()
|
|
268
|
+
abortControllerInfo = abortSignal ? `aborted: ${abortSignal.aborted}, reason: ${abortSignal.reason}` : 'null'
|
|
269
|
+
} catch (e) {
|
|
270
|
+
console.log('[indexedDB.getByWeb] 获取 abortSignal 失败:', e.message)
|
|
271
|
+
}
|
|
272
|
+
console.log('[indexedDB.getByWeb] 发起请求,signal 状态:', abortControllerInfo)
|
|
273
|
+
|
|
274
|
+
let res
|
|
275
|
+
try {
|
|
276
|
+
res = await post(url, params, { signal: abortSignal })
|
|
277
|
+
} catch (e) {
|
|
278
|
+
// 重新抛出 AbortError,让上层可以正确处理取消逻辑
|
|
279
|
+
// 而不是把错误当作正常结果返回
|
|
280
|
+
// axios 取消请求时会抛出 CanceledError 或 AbortError
|
|
281
|
+
const errorName = e.name || ''
|
|
282
|
+
const errorCode = e.code || ''
|
|
283
|
+
if (errorName === 'CanceledError' || errorName === 'AbortError' || errorCode === 'ERR_CANCELED') {
|
|
284
|
+
console.log('[indexedDB.getByWeb] 请求被取消,抛出错误')
|
|
285
|
+
throw e
|
|
286
|
+
}
|
|
287
|
+
res = {
|
|
288
|
+
ERROR: e
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const processedData = processFun ? processFun(res) : res
|
|
292
|
+
|
|
293
|
+
// 同时保存到内存缓存
|
|
294
|
+
if (USE_MEMORY_CACHE) {
|
|
295
|
+
MEMORY_CACHE.set(key, processedData)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// 生产环境或非移动配置时保存到IndexedDB
|
|
299
|
+
if (process.env.NODE_ENV === 'production' || key !== 'webMobileConfig') {
|
|
300
|
+
await this.add(key, processedData)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
callback(processedData)
|
|
304
|
+
} else {
|
|
305
|
+
// 使用缓存数据
|
|
306
|
+
if (USE_MEMORY_CACHE && data) {
|
|
307
|
+
MEMORY_CACHE.set(key, data)
|
|
308
|
+
}
|
|
309
|
+
callback(data)
|
|
310
|
+
}
|
|
311
|
+
} catch (error) {
|
|
312
|
+
console.error(`[getByWeb] 获取数据失败`, error)
|
|
313
|
+
|
|
314
|
+
// IndexedDB操作失败时切换到备用方案
|
|
315
|
+
if (error.name === 'InvalidStateError' || error.message?.includes('database') || error.message?.includes('transaction')) {
|
|
316
|
+
console.error(`[getByWeb] 数据库操作失败,标记为不健康`)
|
|
317
|
+
IS_DB_HEALTHY = false
|
|
318
|
+
|
|
319
|
+
// 使用备用方案获取数据
|
|
320
|
+
return this.getByWebWithoutDB(key, url, params, callback, processFun)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (process.env.NODE_ENV === 'production' && key !== 'webMobileConfig') {
|
|
324
|
+
await this.add(key, null)
|
|
325
|
+
}
|
|
326
|
+
callback(null)
|
|
327
|
+
}
|
|
328
|
+
})()
|
|
329
|
+
|
|
330
|
+
this.locks.set(key, lockPromise)
|
|
331
|
+
|
|
332
|
+
try {
|
|
333
|
+
const timeoutPromise = new Promise((resolve, reject) => {
|
|
334
|
+
setTimeout(() => {
|
|
335
|
+
reject(new Error(`操作超时: ${key}`))
|
|
336
|
+
}, this.requestTimeout)
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
await Promise.race([this.locks.get(key), timeoutPromise])
|
|
340
|
+
} catch (error) {
|
|
341
|
+
console.error(`[getByWeb] 执行期间出错或超时: ${error.message}`)
|
|
342
|
+
} finally {
|
|
343
|
+
this.locks.delete(key)
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// 备用方法:不使用IndexedDB,直接使用内存缓存和API
|
|
348
|
+
async getByWebWithoutDB (key, url, params, callback, processFun) {
|
|
349
|
+
// 首先检查内存缓存
|
|
350
|
+
if (USE_MEMORY_CACHE && MEMORY_CACHE.has(key)) {
|
|
351
|
+
const cachedData = MEMORY_CACHE.get(key)
|
|
352
|
+
callback(cachedData)
|
|
353
|
+
return
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// 无URL则无法获取数据
|
|
357
|
+
if (!url) {
|
|
358
|
+
callback(null)
|
|
359
|
+
return
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// 获取当前 Logic 的 abortSignal,支持 cancel 模式(使用动态导入避免循环依赖)
|
|
363
|
+
let abortSignal = null
|
|
364
|
+
try {
|
|
365
|
+
const LogicRunner = (await import('../logic/LogicRunner')).default
|
|
366
|
+
abortSignal = LogicRunner.getCurrentAbortSignal()
|
|
367
|
+
} catch (e) {
|
|
368
|
+
// 如果导入失败,使用 null
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// 直接通过API获取数据
|
|
372
|
+
try {
|
|
373
|
+
const res = await post(url, params, { signal: abortSignal })
|
|
374
|
+
const processedData = processFun ? processFun(res) : res
|
|
375
|
+
|
|
376
|
+
// 保存到内存缓存
|
|
377
|
+
if (USE_MEMORY_CACHE) {
|
|
378
|
+
MEMORY_CACHE.set(key, processedData)
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
callback(processedData)
|
|
382
|
+
} catch (error) {
|
|
383
|
+
// 重新抛出 AbortError,让上层可以正确处理取消逻辑
|
|
384
|
+
const errorName = error.name || ''
|
|
385
|
+
const errorCode = error.code || ''
|
|
386
|
+
if (errorName === 'CanceledError' || errorName === 'AbortError' || errorCode === 'ERR_CANCELED') {
|
|
387
|
+
console.log('[getByWebWithoutDB] 请求被取消,抛出错误')
|
|
388
|
+
throw error
|
|
389
|
+
}
|
|
390
|
+
console.error(`[getByWebWithoutDB] 获取数据失败: ${error.message}`)
|
|
391
|
+
callback(null)
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
async getData (key) {
|
|
396
|
+
return new Promise((resolve) => {
|
|
397
|
+
this.openDB((db) => {
|
|
398
|
+
if (!db) {
|
|
399
|
+
resolve(null)
|
|
400
|
+
return
|
|
401
|
+
}
|
|
402
|
+
const store = db.transaction(DB_CONFIG.STORE_NAME, 'readwrite').objectStore(DB_CONFIG.STORE_NAME)
|
|
403
|
+
const request = store.get(key)
|
|
404
|
+
|
|
405
|
+
request.onsuccess = (e) => {
|
|
406
|
+
resolve(e.target.result?.data)
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
request.onerror = () => {
|
|
410
|
+
resolve(null)
|
|
411
|
+
}
|
|
412
|
+
})
|
|
413
|
+
})
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
async add (key, data) {
|
|
417
|
+
this.openDB((db) => {
|
|
418
|
+
if (!db) return
|
|
419
|
+
|
|
420
|
+
const store = db.transaction(DB_CONFIG.STORE_NAME, 'readwrite').objectStore(DB_CONFIG.STORE_NAME)
|
|
421
|
+
const request = store.add({ key, data })
|
|
422
|
+
|
|
423
|
+
request.onerror = () => {
|
|
424
|
+
// 添加失败时尝试更新
|
|
425
|
+
this.update(key, data)
|
|
426
|
+
}
|
|
427
|
+
})
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
async update (key, data) {
|
|
431
|
+
this.openDB((db) => {
|
|
432
|
+
if (!db) return
|
|
433
|
+
|
|
434
|
+
const store = db.transaction(DB_CONFIG.STORE_NAME, 'readwrite').objectStore(DB_CONFIG.STORE_NAME)
|
|
435
|
+
store.put({ key, data })
|
|
436
|
+
})
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
async delete (key) {
|
|
440
|
+
this.openDB((db) => {
|
|
441
|
+
if (!db) return
|
|
442
|
+
|
|
443
|
+
const store = db.transaction(DB_CONFIG.STORE_NAME, 'readwrite').objectStore(DB_CONFIG.STORE_NAME)
|
|
444
|
+
store.delete(key)
|
|
445
|
+
})
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
async clear (callback) {
|
|
449
|
+
this.openDB((db) => {
|
|
450
|
+
if (!db) {
|
|
451
|
+
if (typeof callback === 'function') callback()
|
|
452
|
+
return
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const store = db.transaction(DB_CONFIG.STORE_NAME, 'readwrite').objectStore(DB_CONFIG.STORE_NAME)
|
|
456
|
+
const request = store.clear()
|
|
457
|
+
|
|
458
|
+
request.onerror = () => {
|
|
459
|
+
if (typeof callback === 'function') callback()
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
request.onsuccess = () => {
|
|
463
|
+
if (typeof callback === 'function') callback()
|
|
464
|
+
}
|
|
465
|
+
})
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
clearCache () {
|
|
469
|
+
// 清除缓存并刷新页面
|
|
470
|
+
if (this.indexedDB) {
|
|
471
|
+
this.clear(() => {
|
|
472
|
+
location.reload()
|
|
473
|
+
})
|
|
474
|
+
} else {
|
|
475
|
+
location.reload()
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
async getAll () {
|
|
480
|
+
return new Promise((resolve, reject) => {
|
|
481
|
+
this.openDB((db) => {
|
|
482
|
+
if (!db) {
|
|
483
|
+
resolve([])
|
|
484
|
+
return
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const store = db.transaction(DB_CONFIG.STORE_NAME, 'readonly').objectStore(DB_CONFIG.STORE_NAME)
|
|
488
|
+
const request = store.getAll()
|
|
489
|
+
|
|
490
|
+
request.onsuccess = (e) => {
|
|
491
|
+
resolve(e.target.result)
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
request.onerror = (e) => {
|
|
495
|
+
reject(e.target.error)
|
|
496
|
+
}
|
|
497
|
+
})
|
|
498
|
+
})
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
getAllLegacy (callback) {
|
|
502
|
+
this.openDB((res) => {
|
|
503
|
+
if (!res) {
|
|
504
|
+
callback(null, new Error('打开数据库失败'))
|
|
505
|
+
return
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
const store = res.transaction(DB_CONFIG.STORE_NAME, 'readonly').objectStore(DB_CONFIG.STORE_NAME)
|
|
509
|
+
const request = store.getAll()
|
|
510
|
+
|
|
511
|
+
request.onerror = (e) => {
|
|
512
|
+
callback(null, e.target.error)
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
request.onsuccess = (e) => {
|
|
516
|
+
callback(e.target.result)
|
|
517
|
+
}
|
|
518
|
+
})
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
async recreateDatabase () {
|
|
522
|
+
try {
|
|
523
|
+
if (this.db) {
|
|
524
|
+
this.db.close()
|
|
525
|
+
this.db = undefined
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
await new Promise((resolve, reject) => {
|
|
529
|
+
const deleteRequest = this.indexedDB.deleteDatabase(DB_CONFIG.NAME)
|
|
530
|
+
|
|
531
|
+
deleteRequest.onsuccess = () => {
|
|
532
|
+
resolve()
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
deleteRequest.onerror = (e) => {
|
|
536
|
+
reject(e.target.error)
|
|
537
|
+
}
|
|
538
|
+
})
|
|
539
|
+
|
|
540
|
+
DB_CONFIG.CURRENT_VERSION = DB_CONFIG.VERSION
|
|
541
|
+
this.db = await this.openDatabase()
|
|
542
|
+
return this.db
|
|
543
|
+
} catch (error) {
|
|
544
|
+
console.error('重新创建数据库失败:', error)
|
|
545
|
+
throw error
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// 强制重建数据库的方法,带有超时保护
|
|
550
|
+
async forceRecreateDatabase () {
|
|
551
|
+
console.warn('[forceRecreateDatabase] 开始强制重建数据库')
|
|
552
|
+
try {
|
|
553
|
+
// 确保关闭所有连接
|
|
554
|
+
if (this.db) {
|
|
555
|
+
try {
|
|
556
|
+
this.db.close()
|
|
557
|
+
} catch (e) {
|
|
558
|
+
console.warn('[forceRecreateDatabase] 关闭现有连接出错')
|
|
559
|
+
}
|
|
560
|
+
this.db = undefined
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// 清空锁防止死锁
|
|
564
|
+
this.locks.clear()
|
|
565
|
+
|
|
566
|
+
// 强制删除数据库,带超时保护
|
|
567
|
+
await new Promise((resolve) => {
|
|
568
|
+
const deleteRequest = this.indexedDB.deleteDatabase(DB_CONFIG.NAME)
|
|
569
|
+
|
|
570
|
+
const timeout = setTimeout(() => {
|
|
571
|
+
resolve() // 即使超时也继续执行
|
|
572
|
+
}, 3000)
|
|
573
|
+
|
|
574
|
+
deleteRequest.onsuccess = () => {
|
|
575
|
+
clearTimeout(timeout)
|
|
576
|
+
resolve()
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
deleteRequest.onerror = () => {
|
|
580
|
+
clearTimeout(timeout)
|
|
581
|
+
resolve() // 即使出错也继续执行
|
|
582
|
+
}
|
|
583
|
+
})
|
|
584
|
+
|
|
585
|
+
// 重置版本
|
|
586
|
+
DB_CONFIG.CURRENT_VERSION = DB_CONFIG.VERSION
|
|
587
|
+
} catch (error) {
|
|
588
|
+
console.error('[forceRecreateDatabase] 强制重建过程中出错:', error)
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
export const indexedDB = new IndexedDBManager()
|