vue2-client 1.17.23 → 1.17.25
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/.env.message +19 -19
- package/package.json +112 -112
- package/src/base-client/components/common/ImagePreviewModal/ImagePreviewModal.vue +427 -427
- package/src/base-client/components/common/ImagePreviewModal/index.js +3 -3
- package/src/base-client/components/common/Upload/Upload.vue +344 -338
- package/src/base-client/components/common/XAddNativeForm/XAddNativeForm.vue +1288 -1288
- package/src/base-client/components/common/XAddNativeForm/demo.vue +54 -54
- package/src/base-client/components/common/XForm/XTreeSelect.vue +276 -276
- package/src/base-client/components/common/XFormTable/demo.vue +89 -89
- package/src/base-client/components/common/XTab/XTab.vue +612 -612
- package/src/components/FileImageItem/ImageItem.vue +11 -11
- package/src/components/ImagePreview/ImagePreview.vue +323 -0
- package/src/pages/WorkflowDetail/WorkFlowDemo.vue +1 -1
- package/src/router/async/router.map.js +133 -133
|
@@ -1,612 +1,612 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<a-card v-if="config" :bordered="false" :body-style="bodyStyle">
|
|
3
|
-
<a-tabs
|
|
4
|
-
:tabBarGutter="tabBarGutter"
|
|
5
|
-
:activeKey="activeKey"
|
|
6
|
-
@change="(activeKey) => tabPaneChange(activeKey, false)"
|
|
7
|
-
:hideAdd="true"
|
|
8
|
-
:tabBarStyle="{ display: showTabBar ? 'block' : 'none' }"
|
|
9
|
-
>
|
|
10
|
-
<template #tabBarExtraContent v-if="$slots.tabBarExtraContent || tabBarExtraContent">
|
|
11
|
-
<slot name="tabBarExtraContent"></slot>
|
|
12
|
-
</template>
|
|
13
|
-
<slot name="extraBeforeTabs"></slot>
|
|
14
|
-
<a-tab-pane
|
|
15
|
-
v-for="(tab, index) in config.data"
|
|
16
|
-
:key="index"
|
|
17
|
-
:tab="tab.title"
|
|
18
|
-
:forceRender="isTabLoaded(index)"
|
|
19
|
-
>
|
|
20
|
-
<component
|
|
21
|
-
v-if="isTabLoaded(index)"
|
|
22
|
-
:is="tab.slotType"
|
|
23
|
-
:key="'xTabPaneComp' + index"
|
|
24
|
-
:ref="`tab_com_${tab.slotType}_${index}`"
|
|
25
|
-
:serviceName="tab.serviceName"
|
|
26
|
-
:serverName="tab.serviceName"
|
|
27
|
-
:queryParamsName="tab.slotConfig"
|
|
28
|
-
v-on="getEventHandlers(tab,index)"
|
|
29
|
-
@hook:mounted="(h)=>onComponentMounted(h,tab,index)"
|
|
30
|
-
:config-name="tab.slotConfig"
|
|
31
|
-
:env="env"
|
|
32
|
-
v-bind="compProp"
|
|
33
|
-
:extra-data="extraData"
|
|
34
|
-
/>
|
|
35
|
-
</a-tab-pane>
|
|
36
|
-
</a-tabs>
|
|
37
|
-
</a-card>
|
|
38
|
-
</template>
|
|
39
|
-
|
|
40
|
-
<script>
|
|
41
|
-
import { getConfigByName, getConfigByNameAsync, runLogic } from '@vue2-client/services/api/common'
|
|
42
|
-
import { executeStrFunctionByContext } from '@vue2-client/utils/runEvalFunction'
|
|
43
|
-
import { getRealKeyData } from '@vue2-client/utils/util'
|
|
44
|
-
import { getMicroData, getWindow, isMicroAppEnv, microDispatch } from '@vue2-client/utils/microAppUtils'
|
|
45
|
-
import { mapState } from 'vuex'
|
|
46
|
-
|
|
47
|
-
export default {
|
|
48
|
-
name: 'XTab',
|
|
49
|
-
components: {
|
|
50
|
-
XFormTable: () => import('@vue2-client/base-client/components/common/XFormTable/XFormTable'),
|
|
51
|
-
XAddNativeForm: () => import('@vue2-client/base-client/components/common/XAddNativeForm/XAddNativeForm.vue'),
|
|
52
|
-
XReportGrid: () => import('@vue2-client/base-client/components/common/XReportGrid/XReport.vue')
|
|
53
|
-
},
|
|
54
|
-
inject: {
|
|
55
|
-
isInAModal: { default: false },
|
|
56
|
-
getSelectedId: { default: false },
|
|
57
|
-
getSelectedData: { default: false },
|
|
58
|
-
getOutEnv: { default: false },
|
|
59
|
-
setGlobalData: { default: false },
|
|
60
|
-
getGlobalData: { default: false },
|
|
61
|
-
generalFunction: { default: false },
|
|
62
|
-
},
|
|
63
|
-
provide () {
|
|
64
|
-
return {
|
|
65
|
-
currUser: this.currUser,
|
|
66
|
-
getSelectedId: this.getSelectedId,
|
|
67
|
-
// 暴露页签加载相关方法,供子组件调用
|
|
68
|
-
ensureTabLoaded: this.ensureTabLoaded,
|
|
69
|
-
getTabComponent: this.getTabComponent,
|
|
70
|
-
isTabLoaded: this.isTabLoaded
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
data () {
|
|
74
|
-
return {
|
|
75
|
-
activeKey: 0,
|
|
76
|
-
// 配置
|
|
77
|
-
config: undefined,
|
|
78
|
-
tabBarExtraContent: undefined,
|
|
79
|
-
attr: {},
|
|
80
|
-
showTabBar: true, // 默认显示页签
|
|
81
|
-
loadedTabs: [] // 记录已加载的页签索引
|
|
82
|
-
}
|
|
83
|
-
},
|
|
84
|
-
computed: {
|
|
85
|
-
...mapState('account', { currUser: 'user' })
|
|
86
|
-
},
|
|
87
|
-
mounted () {
|
|
88
|
-
this.activeKey = this.defaultActiveKey
|
|
89
|
-
// 标记默认激活的页签为已加载
|
|
90
|
-
this.markTabAsLoaded(this.defaultActiveKey)
|
|
91
|
-
this.tabPaneChange('initTabLoading', true)
|
|
92
|
-
},
|
|
93
|
-
methods: {
|
|
94
|
-
// 自定义函数中调用的方法 这个不能删
|
|
95
|
-
getWindow,
|
|
96
|
-
runLogic,
|
|
97
|
-
isMicroAppEnv,
|
|
98
|
-
microDispatch,
|
|
99
|
-
getMicroData,
|
|
100
|
-
getRealKeyData,
|
|
101
|
-
getConfigByName,
|
|
102
|
-
getConfigByNameAsync,
|
|
103
|
-
async tabPaneChange (newKey, initStatus = false) {
|
|
104
|
-
let result = {}
|
|
105
|
-
if (this.activeKey === newKey) {
|
|
106
|
-
return
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const oldKey = this.activeKey
|
|
110
|
-
|
|
111
|
-
// 标记新页签为已加载
|
|
112
|
-
if (this.config && this.config.data && this.config.data[newKey] !== undefined) {
|
|
113
|
-
this.markTabAsLoaded(newKey)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (this.config && this.config.changeFunc) {
|
|
117
|
-
let oldRef
|
|
118
|
-
let oldTabName
|
|
119
|
-
let newRef
|
|
120
|
-
let newTabName
|
|
121
|
-
|
|
122
|
-
if (!this.config.data[oldKey]) {
|
|
123
|
-
oldTabName = `tab_com_${oldKey}`
|
|
124
|
-
oldRef = this.$refs[oldTabName]
|
|
125
|
-
} else {
|
|
126
|
-
const oldTabSlotType = this.config.data[oldKey].slotType
|
|
127
|
-
oldTabName = `tab_com_${oldTabSlotType}_${oldKey}`
|
|
128
|
-
oldRef = this.$refs[oldTabName]
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (!this.config.data[newKey]) {
|
|
132
|
-
newTabName = `tab_com_${newKey}`
|
|
133
|
-
newRef = this.$refs[newTabName]
|
|
134
|
-
} else {
|
|
135
|
-
const newTabSlotType = this.config.data[newKey].slotType
|
|
136
|
-
newTabName = `tab_com_${newTabSlotType}_${newKey}`
|
|
137
|
-
newRef = this.$refs[newTabName]
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const args = [
|
|
141
|
-
oldKey,
|
|
142
|
-
newKey,
|
|
143
|
-
this.config.data[oldKey],
|
|
144
|
-
this.config.data[newKey],
|
|
145
|
-
oldRef ? oldRef[0] : undefined,
|
|
146
|
-
]
|
|
147
|
-
args.push(newTabName)
|
|
148
|
-
|
|
149
|
-
// 关键修改:确保新组件实例存在
|
|
150
|
-
if (!newRef || !newRef[0]) {
|
|
151
|
-
console.log('等待新页签组件初始化...')
|
|
152
|
-
// 等待组件挂载完成
|
|
153
|
-
await this.waitForComponentReady(newKey)
|
|
154
|
-
newRef = this.$refs[newTabName]
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
if (newRef && newRef[0]) {
|
|
158
|
-
args.push(newRef[0])
|
|
159
|
-
} else {
|
|
160
|
-
args.push(null)
|
|
161
|
-
console.warn('新页签组件实例获取失败')
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
args.push(this.extraData)
|
|
165
|
-
|
|
166
|
-
// 使用 $nextTick 确保 DOM 更新完成
|
|
167
|
-
await this.$nextTick()
|
|
168
|
-
|
|
169
|
-
// 确保组件完全就绪后再执行 changeFunc
|
|
170
|
-
if (newRef && newRef[0]) {
|
|
171
|
-
// 等待组件完全就绪(包括内部异步初始化)
|
|
172
|
-
await this.waitForComponentFullyReady(newRef[0], newKey)
|
|
173
|
-
} else {
|
|
174
|
-
// 如果没有组件实例,等待一个 tick 确保 DOM 更新
|
|
175
|
-
await this.$nextTick()
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
result = executeStrFunctionByContext(this, this.config.changeFunc, args)
|
|
179
|
-
|
|
180
|
-
if (result && result.noChange) {
|
|
181
|
-
console.info('不切换页签作为按钮使用')
|
|
182
|
-
// 如果返回 noChange,恢复原来的 activeKey
|
|
183
|
-
if (this.activeKey === newKey) {
|
|
184
|
-
this.activeKey = oldKey
|
|
185
|
-
}
|
|
186
|
-
} else {
|
|
187
|
-
// 确保 activeKey 已更新(如果还没更新的话)
|
|
188
|
-
if (this.activeKey !== newKey) {
|
|
189
|
-
this.activeKey = newKey
|
|
190
|
-
}
|
|
191
|
-
// 确保回调在组件完全就绪后执行
|
|
192
|
-
if (result && result.callback && typeof result.callback === 'function') {
|
|
193
|
-
await this.$nextTick()
|
|
194
|
-
result.callback()
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
} else {
|
|
198
|
-
this.activeKey = newKey
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (initStatus) {
|
|
202
|
-
this.activeKey = this.defaultActiveKey
|
|
203
|
-
}
|
|
204
|
-
},
|
|
205
|
-
|
|
206
|
-
// 新增:等待组件就绪的方法
|
|
207
|
-
waitForComponentReady (index) {
|
|
208
|
-
return new Promise((resolve) => {
|
|
209
|
-
const checkComponent = () => {
|
|
210
|
-
const tab = this.config.data[index]
|
|
211
|
-
const refName = `tab_com_${tab.slotType}_${index}`
|
|
212
|
-
const ref = this.$refs[refName]
|
|
213
|
-
|
|
214
|
-
if (ref && ref[0]) {
|
|
215
|
-
// 额外检查组件内部是否已初始化(如果有相关状态的话)
|
|
216
|
-
if (ref[0].isReady !== undefined) {
|
|
217
|
-
if (ref[0].isReady) {
|
|
218
|
-
resolve(ref[0])
|
|
219
|
-
} else {
|
|
220
|
-
setTimeout(checkComponent, 50)
|
|
221
|
-
}
|
|
222
|
-
} else {
|
|
223
|
-
resolve(ref[0])
|
|
224
|
-
}
|
|
225
|
-
} else {
|
|
226
|
-
setTimeout(checkComponent, 50)
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
checkComponent()
|
|
231
|
-
})
|
|
232
|
-
},
|
|
233
|
-
// 等待组件完全就绪(包括内部异步初始化)
|
|
234
|
-
waitForComponentFullyReady (component, index) {
|
|
235
|
-
return new Promise((resolve) => {
|
|
236
|
-
if (!component) {
|
|
237
|
-
resolve()
|
|
238
|
-
return
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
let attempts = 0
|
|
242
|
-
const maxAttempts = 60 // 最多等待3秒
|
|
243
|
-
const checkInterval = 50
|
|
244
|
-
|
|
245
|
-
const checkReady = () => {
|
|
246
|
-
attempts++
|
|
247
|
-
// 1. 检查组件是否有 loading 状态,确保不在加载中
|
|
248
|
-
if (component.loading === true) {
|
|
249
|
-
if (attempts < maxAttempts) {
|
|
250
|
-
setTimeout(checkReady, checkInterval)
|
|
251
|
-
return
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// 2. 检查组件是否有 loaded 状态(如 XAddNativeForm)
|
|
256
|
-
if (component.loaded === false) {
|
|
257
|
-
if (attempts < maxAttempts) {
|
|
258
|
-
setTimeout(checkReady, checkInterval)
|
|
259
|
-
return
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// 3. 检查组件是否有 realQueryConfig(如 XFormTable)
|
|
264
|
-
const tab = this.config.data[index]
|
|
265
|
-
const isFormTableType = tab?.slotType === 'x-form-table' ||
|
|
266
|
-
tab?.slotType === 'XFormTable' ||
|
|
267
|
-
tab?.slotType?.toLowerCase() === 'xformtable'
|
|
268
|
-
if (isFormTableType && component.realQueryConfig === undefined && !component.loadError) {
|
|
269
|
-
if (attempts < maxAttempts) {
|
|
270
|
-
setTimeout(checkReady, checkInterval)
|
|
271
|
-
return
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// 4. 检查关键方法是否存在(如 setForm、setFormWithKey)
|
|
276
|
-
const hasSetForm = typeof component.setForm === 'function'
|
|
277
|
-
const hasSetFormWithKey = typeof component.setFormWithKey === 'function'
|
|
278
|
-
const hasSetFormWithNoKey = typeof component.setFormWithNoKey === 'function'
|
|
279
|
-
// 5. 如果组件有这些方法,确保表单对象存在
|
|
280
|
-
if (hasSetForm || hasSetFormWithKey || hasSetFormWithNoKey) {
|
|
281
|
-
// 检查组件是否有 form 对象
|
|
282
|
-
if (component.form && Object.keys(component.form).length > 0) {
|
|
283
|
-
// 表单已初始化,等待 DOM 渲染完成
|
|
284
|
-
this.$nextTick(() => {
|
|
285
|
-
// 额外等待一个 tick,确保所有子组件也渲染完成
|
|
286
|
-
this.$nextTick(() => {
|
|
287
|
-
resolve()
|
|
288
|
-
})
|
|
289
|
-
})
|
|
290
|
-
return
|
|
291
|
-
} else if (component.loaded !== false) {
|
|
292
|
-
// 如果没有 form 对象但 loaded 不为 false,可能还在初始化
|
|
293
|
-
if (attempts < maxAttempts) {
|
|
294
|
-
setTimeout(checkReady, checkInterval)
|
|
295
|
-
return
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// 6. 如果以上都通过,或者超时,等待 DOM 渲染完成
|
|
301
|
-
if (attempts >= 3 || attempts >= maxAttempts) {
|
|
302
|
-
// 至少等待几次检查,确保有时间初始化
|
|
303
|
-
this.$nextTick(() => {
|
|
304
|
-
this.$nextTick(() => {
|
|
305
|
-
resolve()
|
|
306
|
-
})
|
|
307
|
-
})
|
|
308
|
-
return
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// 继续等待
|
|
312
|
-
setTimeout(checkReady, checkInterval)
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// 首次检查前等待一个 tick
|
|
316
|
-
this.$nextTick(() => {
|
|
317
|
-
checkReady()
|
|
318
|
-
})
|
|
319
|
-
})
|
|
320
|
-
},
|
|
321
|
-
initConfig () {
|
|
322
|
-
if (this.configName) {
|
|
323
|
-
this.getConfig()
|
|
324
|
-
} else if (this.localConfig) {
|
|
325
|
-
this.config = this.localConfig
|
|
326
|
-
// 设置是否显示页签
|
|
327
|
-
this.showTabBar = this.localConfig.showTabBar !== false
|
|
328
|
-
}
|
|
329
|
-
// 重置已加载页签集合,确保配置变更后重新加载
|
|
330
|
-
this.loadedTabs = []
|
|
331
|
-
// 标记默认激活的页签为已加载
|
|
332
|
-
if (this.config && this.config.data) {
|
|
333
|
-
this.markTabAsLoaded(this.defaultActiveKey)
|
|
334
|
-
}
|
|
335
|
-
},
|
|
336
|
-
getConfig () {
|
|
337
|
-
getConfigByName(this.configName, this.serverName, res => {
|
|
338
|
-
this.config = res
|
|
339
|
-
// 设置是否显示页签
|
|
340
|
-
this.showTabBar = res.showTabBar !== false
|
|
341
|
-
// 标记默认激活的页签为已加载
|
|
342
|
-
if (this.config && this.config.data) {
|
|
343
|
-
this.markTabAsLoaded(this.defaultActiveKey)
|
|
344
|
-
}
|
|
345
|
-
}, this.env === 'dev')
|
|
346
|
-
},
|
|
347
|
-
getEventHandlers (tab, index) {
|
|
348
|
-
const handlers = {}
|
|
349
|
-
if (!tab?.events || tab?.events?.length === 0) {
|
|
350
|
-
return handlers
|
|
351
|
-
}
|
|
352
|
-
tab.events.forEach(event => {
|
|
353
|
-
handlers[event.type] = (...args) => {
|
|
354
|
-
executeStrFunctionByContext(this.$refs[`tab_com_${tab.slotType}_${index}`][0], event.customFunction, args)
|
|
355
|
-
}
|
|
356
|
-
})
|
|
357
|
-
return handlers
|
|
358
|
-
},
|
|
359
|
-
onComponentMounted (h, tab, index) {
|
|
360
|
-
if (tab.slotType === 'x-add-native-form') {
|
|
361
|
-
// 建议表单需要主动调用初始化方法
|
|
362
|
-
getConfigByName(tab.slotConfig, tab.serviceName, async (res) => {
|
|
363
|
-
// 如果配置了 表单初始化logic
|
|
364
|
-
// 调用 logic 获取参数
|
|
365
|
-
let param = {}
|
|
366
|
-
let selectedId
|
|
367
|
-
if (res.paramLogicName) {
|
|
368
|
-
if (!!this.getSelectedId) {
|
|
369
|
-
selectedId = this.getSelectedId()
|
|
370
|
-
if (typeof selectedId !== 'object') {
|
|
371
|
-
selectedId = { selectedId: selectedId }
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
param = Object.assign(param, await runLogic(res.paramLogicName, selectedId, tab.serviceName))
|
|
375
|
-
}
|
|
376
|
-
this.$refs[`tab_com_${tab.slotType}_${index}`][0].init({
|
|
377
|
-
formItems: res.formJson,
|
|
378
|
-
showSubmitBtn: !this.isInAModal,
|
|
379
|
-
businessType: '新增',
|
|
380
|
-
layout: res.xAddFormLayout,
|
|
381
|
-
...res,
|
|
382
|
-
fixedAddForm: param,
|
|
383
|
-
modifyModelData: {
|
|
384
|
-
files: param.files,
|
|
385
|
-
images: param.images
|
|
386
|
-
}
|
|
387
|
-
})
|
|
388
|
-
}, this.env === 'dev')
|
|
389
|
-
}
|
|
390
|
-
},
|
|
391
|
-
// 判断页签是否已加载
|
|
392
|
-
isTabLoaded (index) {
|
|
393
|
-
const numIndex = Number(index)
|
|
394
|
-
return this.loadedTabs.includes(numIndex) || Number(this.activeKey) === numIndex
|
|
395
|
-
},
|
|
396
|
-
// 标记页签为已加载
|
|
397
|
-
markTabAsLoaded (index) {
|
|
398
|
-
const numIndex = Number(index)
|
|
399
|
-
// 确保是有效的数字索引,且未添加过
|
|
400
|
-
if (!isNaN(numIndex) && numIndex >= 0 && !this.loadedTabs.includes(numIndex)) {
|
|
401
|
-
this.loadedTabs.push(numIndex)
|
|
402
|
-
}
|
|
403
|
-
},
|
|
404
|
-
// 确保页签已加载,如果未加载则先加载,返回 Promise 等待组件渲染完成
|
|
405
|
-
ensureTabLoaded (index, options = {}) {
|
|
406
|
-
return new Promise((resolve, reject) => {
|
|
407
|
-
const numIndex = Number(index)
|
|
408
|
-
const innerRefs = Array.isArray(options.innerRefs) ? options.innerRefs : []
|
|
409
|
-
const allowSwitch = options.allowSwitch !== false
|
|
410
|
-
// 验证索引有效性
|
|
411
|
-
if (isNaN(numIndex) || numIndex < 0) {
|
|
412
|
-
reject(new Error(`无效的页签索引: ${index}`))
|
|
413
|
-
return
|
|
414
|
-
}
|
|
415
|
-
if (!this.config || !this.config.data || !this.config.data[numIndex]) {
|
|
416
|
-
reject(new Error(`页签索引 ${index} 不存在`))
|
|
417
|
-
return
|
|
418
|
-
}
|
|
419
|
-
// 如果已经加载,直接返回组件实例
|
|
420
|
-
if (this.isTabLoaded(numIndex)) {
|
|
421
|
-
const component = this.getTabComponent(numIndex)
|
|
422
|
-
if (component) {
|
|
423
|
-
if (innerRefs.length === 0) {
|
|
424
|
-
resolve(component)
|
|
425
|
-
} else {
|
|
426
|
-
this.waitForInnerRefs(component, innerRefs, resolve, reject)
|
|
427
|
-
}
|
|
428
|
-
} else {
|
|
429
|
-
// 已标记但组件可能还在渲染中,等待一下
|
|
430
|
-
this.$nextTick(() => {
|
|
431
|
-
const comp = this.getTabComponent(numIndex)
|
|
432
|
-
if (comp) {
|
|
433
|
-
if (innerRefs.length === 0) {
|
|
434
|
-
resolve(comp)
|
|
435
|
-
} else {
|
|
436
|
-
this.waitForInnerRefs(comp, innerRefs, resolve, reject)
|
|
437
|
-
}
|
|
438
|
-
} else {
|
|
439
|
-
// 等待组件mounted
|
|
440
|
-
this.waitForComponent(numIndex, (c) => {
|
|
441
|
-
if (innerRefs.length === 0) {
|
|
442
|
-
resolve(c)
|
|
443
|
-
} else {
|
|
444
|
-
this.waitForInnerRefs(c, innerRefs, resolve, reject)
|
|
445
|
-
}
|
|
446
|
-
}, reject)
|
|
447
|
-
}
|
|
448
|
-
})
|
|
449
|
-
}
|
|
450
|
-
return
|
|
451
|
-
}
|
|
452
|
-
// 标记为已加载,触发渲染
|
|
453
|
-
this.markTabAsLoaded(numIndex)
|
|
454
|
-
// 优先尝试在不切换页签的情况下获取组件
|
|
455
|
-
this.$nextTick(() => {
|
|
456
|
-
const comp = this.getTabComponent(numIndex)
|
|
457
|
-
if (comp) {
|
|
458
|
-
if (innerRefs.length === 0) resolve(comp)
|
|
459
|
-
else this.waitForInnerRefs(comp, innerRefs, resolve, reject)
|
|
460
|
-
return
|
|
461
|
-
}
|
|
462
|
-
// 若仍未渲染,且允许临时切换,则进行一次激活-恢复回退
|
|
463
|
-
if (!allowSwitch) {
|
|
464
|
-
// 不允许切换则进入常规等待(可能更慢)
|
|
465
|
-
this.waitForComponent(numIndex, (c) => {
|
|
466
|
-
if (innerRefs.length === 0) resolve(c)
|
|
467
|
-
else this.waitForInnerRefs(c, innerRefs, resolve, reject)
|
|
468
|
-
}, reject)
|
|
469
|
-
return
|
|
470
|
-
}
|
|
471
|
-
const prevActive = this.activeKey
|
|
472
|
-
this.activeKey = numIndex
|
|
473
|
-
this.$nextTick(() => {
|
|
474
|
-
this.waitForComponent(numIndex, (c) => {
|
|
475
|
-
// 组件到位后再恢复原激活页签
|
|
476
|
-
if (prevActive !== numIndex) {
|
|
477
|
-
this.$nextTick(() => { this.activeKey = prevActive })
|
|
478
|
-
}
|
|
479
|
-
if (innerRefs.length === 0) resolve(c)
|
|
480
|
-
else this.waitForInnerRefs(c, innerRefs, resolve, reject)
|
|
481
|
-
}, reject)
|
|
482
|
-
})
|
|
483
|
-
})
|
|
484
|
-
})
|
|
485
|
-
},
|
|
486
|
-
// 等待组件渲染完成
|
|
487
|
-
waitForComponent (index, resolve, reject) {
|
|
488
|
-
const maxAttempts = 50 // 最多尝试50次
|
|
489
|
-
let attempts = 0
|
|
490
|
-
const checkInterval = 50 // 每50ms检查一次
|
|
491
|
-
const tab = this.config.data[index]
|
|
492
|
-
const refName = `tab_com_${tab.slotType}_${index}`
|
|
493
|
-
const checkComponent = () => {
|
|
494
|
-
attempts++
|
|
495
|
-
const component = this.$refs[refName]
|
|
496
|
-
if (component && component[0]) {
|
|
497
|
-
resolve(component[0])
|
|
498
|
-
return
|
|
499
|
-
}
|
|
500
|
-
if (attempts >= maxAttempts) {
|
|
501
|
-
reject(new Error(`等待页签 ${index} 组件加载超时`))
|
|
502
|
-
return
|
|
503
|
-
}
|
|
504
|
-
setTimeout(checkComponent, checkInterval)
|
|
505
|
-
}
|
|
506
|
-
// 首次检查
|
|
507
|
-
this.$nextTick(() => {
|
|
508
|
-
checkComponent()
|
|
509
|
-
})
|
|
510
|
-
},
|
|
511
|
-
// 等待内部 $refs 就绪
|
|
512
|
-
waitForInnerRefs (component, innerRefs, resolve, reject) {
|
|
513
|
-
const maxAttempts = 50
|
|
514
|
-
let attempts = 0
|
|
515
|
-
const checkInterval = 50
|
|
516
|
-
const allReady = () => innerRefs.every(name => component.$refs && component.$refs[name])
|
|
517
|
-
const check = () => {
|
|
518
|
-
attempts++
|
|
519
|
-
if (allReady()) {
|
|
520
|
-
resolve(component)
|
|
521
|
-
return
|
|
522
|
-
}
|
|
523
|
-
if (attempts >= maxAttempts) {
|
|
524
|
-
reject(new Error(`等待内部 refs [${innerRefs.join(', ')}] 就绪超时`))
|
|
525
|
-
return
|
|
526
|
-
}
|
|
527
|
-
setTimeout(check, checkInterval)
|
|
528
|
-
}
|
|
529
|
-
this.$nextTick(() => check())
|
|
530
|
-
},
|
|
531
|
-
// 获取页签的组件实例
|
|
532
|
-
getTabComponent (index) {
|
|
533
|
-
const numIndex = Number(index)
|
|
534
|
-
if (isNaN(numIndex) || numIndex < 0 || !this.config || !this.config.data || !this.config.data[numIndex]) {
|
|
535
|
-
return null
|
|
536
|
-
}
|
|
537
|
-
const tab = this.config.data[numIndex]
|
|
538
|
-
const refName = `tab_com_${tab.slotType}_${numIndex}`
|
|
539
|
-
const ref = this.$refs[refName]
|
|
540
|
-
return ref && ref[0] ? ref[0] : null
|
|
541
|
-
},
|
|
542
|
-
|
|
543
|
-
},
|
|
544
|
-
props: {
|
|
545
|
-
// 配置名
|
|
546
|
-
configName: {
|
|
547
|
-
type: String,
|
|
548
|
-
default: undefined
|
|
549
|
-
},
|
|
550
|
-
serverName: {
|
|
551
|
-
type: String,
|
|
552
|
-
default: undefined
|
|
553
|
-
},
|
|
554
|
-
env: {
|
|
555
|
-
type: String,
|
|
556
|
-
default: 'prod'
|
|
557
|
-
},
|
|
558
|
-
// 本地配置,调试用
|
|
559
|
-
localConfig: {
|
|
560
|
-
type: Object,
|
|
561
|
-
default: undefined
|
|
562
|
-
},
|
|
563
|
-
tabBarGutter: {
|
|
564
|
-
type: Number,
|
|
565
|
-
default: 10
|
|
566
|
-
},
|
|
567
|
-
bodyStyle: {
|
|
568
|
-
type: Object,
|
|
569
|
-
default: () => {
|
|
570
|
-
return {}
|
|
571
|
-
}
|
|
572
|
-
},
|
|
573
|
-
defaultActiveKey: {
|
|
574
|
-
type: [String, Number],
|
|
575
|
-
default: 0
|
|
576
|
-
},
|
|
577
|
-
// 新增compProp参数,用于穿透到动态组件
|
|
578
|
-
compProp: {
|
|
579
|
-
type: Object,
|
|
580
|
-
default: () => {
|
|
581
|
-
return {}
|
|
582
|
-
}
|
|
583
|
-
},
|
|
584
|
-
// 额外数据
|
|
585
|
-
extraData: {
|
|
586
|
-
type: Object,
|
|
587
|
-
default: () => {
|
|
588
|
-
return {}
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
},
|
|
592
|
-
watch: {
|
|
593
|
-
configName: {
|
|
594
|
-
deep: true,
|
|
595
|
-
immediate: true,
|
|
596
|
-
handler (val) {
|
|
597
|
-
this.initConfig()
|
|
598
|
-
}
|
|
599
|
-
},
|
|
600
|
-
// 如果本地配置更改了,重新初始化
|
|
601
|
-
localConfig: {
|
|
602
|
-
deep: true,
|
|
603
|
-
immediate: true,
|
|
604
|
-
handler (val) {
|
|
605
|
-
this.initConfig()
|
|
606
|
-
}
|
|
607
|
-
},
|
|
608
|
-
},
|
|
609
|
-
}
|
|
610
|
-
</script>
|
|
611
|
-
<style scoped>
|
|
612
|
-
</style>
|
|
1
|
+
<template>
|
|
2
|
+
<a-card v-if="config" :bordered="false" :body-style="bodyStyle">
|
|
3
|
+
<a-tabs
|
|
4
|
+
:tabBarGutter="tabBarGutter"
|
|
5
|
+
:activeKey="activeKey"
|
|
6
|
+
@change="(activeKey) => tabPaneChange(activeKey, false)"
|
|
7
|
+
:hideAdd="true"
|
|
8
|
+
:tabBarStyle="{ display: showTabBar ? 'block' : 'none' }"
|
|
9
|
+
>
|
|
10
|
+
<template #tabBarExtraContent v-if="$slots.tabBarExtraContent || tabBarExtraContent">
|
|
11
|
+
<slot name="tabBarExtraContent"></slot>
|
|
12
|
+
</template>
|
|
13
|
+
<slot name="extraBeforeTabs"></slot>
|
|
14
|
+
<a-tab-pane
|
|
15
|
+
v-for="(tab, index) in config.data"
|
|
16
|
+
:key="index"
|
|
17
|
+
:tab="tab.title"
|
|
18
|
+
:forceRender="isTabLoaded(index)"
|
|
19
|
+
>
|
|
20
|
+
<component
|
|
21
|
+
v-if="isTabLoaded(index)"
|
|
22
|
+
:is="tab.slotType"
|
|
23
|
+
:key="'xTabPaneComp' + index"
|
|
24
|
+
:ref="`tab_com_${tab.slotType}_${index}`"
|
|
25
|
+
:serviceName="tab.serviceName"
|
|
26
|
+
:serverName="tab.serviceName"
|
|
27
|
+
:queryParamsName="tab.slotConfig"
|
|
28
|
+
v-on="getEventHandlers(tab,index)"
|
|
29
|
+
@hook:mounted="(h)=>onComponentMounted(h,tab,index)"
|
|
30
|
+
:config-name="tab.slotConfig"
|
|
31
|
+
:env="env"
|
|
32
|
+
v-bind="compProp"
|
|
33
|
+
:extra-data="extraData"
|
|
34
|
+
/>
|
|
35
|
+
</a-tab-pane>
|
|
36
|
+
</a-tabs>
|
|
37
|
+
</a-card>
|
|
38
|
+
</template>
|
|
39
|
+
|
|
40
|
+
<script>
|
|
41
|
+
import { getConfigByName, getConfigByNameAsync, runLogic } from '@vue2-client/services/api/common'
|
|
42
|
+
import { executeStrFunctionByContext } from '@vue2-client/utils/runEvalFunction'
|
|
43
|
+
import { getRealKeyData } from '@vue2-client/utils/util'
|
|
44
|
+
import { getMicroData, getWindow, isMicroAppEnv, microDispatch } from '@vue2-client/utils/microAppUtils'
|
|
45
|
+
import { mapState } from 'vuex'
|
|
46
|
+
|
|
47
|
+
export default {
|
|
48
|
+
name: 'XTab',
|
|
49
|
+
components: {
|
|
50
|
+
XFormTable: () => import('@vue2-client/base-client/components/common/XFormTable/XFormTable'),
|
|
51
|
+
XAddNativeForm: () => import('@vue2-client/base-client/components/common/XAddNativeForm/XAddNativeForm.vue'),
|
|
52
|
+
XReportGrid: () => import('@vue2-client/base-client/components/common/XReportGrid/XReport.vue')
|
|
53
|
+
},
|
|
54
|
+
inject: {
|
|
55
|
+
isInAModal: { default: false },
|
|
56
|
+
getSelectedId: { default: false },
|
|
57
|
+
getSelectedData: { default: false },
|
|
58
|
+
getOutEnv: { default: false },
|
|
59
|
+
setGlobalData: { default: false },
|
|
60
|
+
getGlobalData: { default: false },
|
|
61
|
+
generalFunction: { default: false },
|
|
62
|
+
},
|
|
63
|
+
provide () {
|
|
64
|
+
return {
|
|
65
|
+
currUser: this.currUser,
|
|
66
|
+
getSelectedId: this.getSelectedId,
|
|
67
|
+
// 暴露页签加载相关方法,供子组件调用
|
|
68
|
+
ensureTabLoaded: this.ensureTabLoaded,
|
|
69
|
+
getTabComponent: this.getTabComponent,
|
|
70
|
+
isTabLoaded: this.isTabLoaded
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
data () {
|
|
74
|
+
return {
|
|
75
|
+
activeKey: 0,
|
|
76
|
+
// 配置
|
|
77
|
+
config: undefined,
|
|
78
|
+
tabBarExtraContent: undefined,
|
|
79
|
+
attr: {},
|
|
80
|
+
showTabBar: true, // 默认显示页签
|
|
81
|
+
loadedTabs: [] // 记录已加载的页签索引
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
computed: {
|
|
85
|
+
...mapState('account', { currUser: 'user' })
|
|
86
|
+
},
|
|
87
|
+
mounted () {
|
|
88
|
+
this.activeKey = this.defaultActiveKey
|
|
89
|
+
// 标记默认激活的页签为已加载
|
|
90
|
+
this.markTabAsLoaded(this.defaultActiveKey)
|
|
91
|
+
this.tabPaneChange('initTabLoading', true)
|
|
92
|
+
},
|
|
93
|
+
methods: {
|
|
94
|
+
// 自定义函数中调用的方法 这个不能删
|
|
95
|
+
getWindow,
|
|
96
|
+
runLogic,
|
|
97
|
+
isMicroAppEnv,
|
|
98
|
+
microDispatch,
|
|
99
|
+
getMicroData,
|
|
100
|
+
getRealKeyData,
|
|
101
|
+
getConfigByName,
|
|
102
|
+
getConfigByNameAsync,
|
|
103
|
+
async tabPaneChange (newKey, initStatus = false) {
|
|
104
|
+
let result = {}
|
|
105
|
+
if (this.activeKey === newKey) {
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const oldKey = this.activeKey
|
|
110
|
+
|
|
111
|
+
// 标记新页签为已加载
|
|
112
|
+
if (this.config && this.config.data && this.config.data[newKey] !== undefined) {
|
|
113
|
+
this.markTabAsLoaded(newKey)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (this.config && this.config.changeFunc) {
|
|
117
|
+
let oldRef
|
|
118
|
+
let oldTabName
|
|
119
|
+
let newRef
|
|
120
|
+
let newTabName
|
|
121
|
+
|
|
122
|
+
if (!this.config.data[oldKey]) {
|
|
123
|
+
oldTabName = `tab_com_${oldKey}`
|
|
124
|
+
oldRef = this.$refs[oldTabName]
|
|
125
|
+
} else {
|
|
126
|
+
const oldTabSlotType = this.config.data[oldKey].slotType
|
|
127
|
+
oldTabName = `tab_com_${oldTabSlotType}_${oldKey}`
|
|
128
|
+
oldRef = this.$refs[oldTabName]
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!this.config.data[newKey]) {
|
|
132
|
+
newTabName = `tab_com_${newKey}`
|
|
133
|
+
newRef = this.$refs[newTabName]
|
|
134
|
+
} else {
|
|
135
|
+
const newTabSlotType = this.config.data[newKey].slotType
|
|
136
|
+
newTabName = `tab_com_${newTabSlotType}_${newKey}`
|
|
137
|
+
newRef = this.$refs[newTabName]
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const args = [
|
|
141
|
+
oldKey,
|
|
142
|
+
newKey,
|
|
143
|
+
this.config.data[oldKey],
|
|
144
|
+
this.config.data[newKey],
|
|
145
|
+
oldRef ? oldRef[0] : undefined,
|
|
146
|
+
]
|
|
147
|
+
args.push(newTabName)
|
|
148
|
+
|
|
149
|
+
// 关键修改:确保新组件实例存在
|
|
150
|
+
if (!newRef || !newRef[0]) {
|
|
151
|
+
console.log('等待新页签组件初始化...')
|
|
152
|
+
// 等待组件挂载完成
|
|
153
|
+
await this.waitForComponentReady(newKey)
|
|
154
|
+
newRef = this.$refs[newTabName]
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (newRef && newRef[0]) {
|
|
158
|
+
args.push(newRef[0])
|
|
159
|
+
} else {
|
|
160
|
+
args.push(null)
|
|
161
|
+
console.warn('新页签组件实例获取失败')
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
args.push(this.extraData)
|
|
165
|
+
|
|
166
|
+
// 使用 $nextTick 确保 DOM 更新完成
|
|
167
|
+
await this.$nextTick()
|
|
168
|
+
|
|
169
|
+
// 确保组件完全就绪后再执行 changeFunc
|
|
170
|
+
if (newRef && newRef[0]) {
|
|
171
|
+
// 等待组件完全就绪(包括内部异步初始化)
|
|
172
|
+
await this.waitForComponentFullyReady(newRef[0], newKey)
|
|
173
|
+
} else {
|
|
174
|
+
// 如果没有组件实例,等待一个 tick 确保 DOM 更新
|
|
175
|
+
await this.$nextTick()
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
result = executeStrFunctionByContext(this, this.config.changeFunc, args)
|
|
179
|
+
|
|
180
|
+
if (result && result.noChange) {
|
|
181
|
+
console.info('不切换页签作为按钮使用')
|
|
182
|
+
// 如果返回 noChange,恢复原来的 activeKey
|
|
183
|
+
if (this.activeKey === newKey) {
|
|
184
|
+
this.activeKey = oldKey
|
|
185
|
+
}
|
|
186
|
+
} else {
|
|
187
|
+
// 确保 activeKey 已更新(如果还没更新的话)
|
|
188
|
+
if (this.activeKey !== newKey) {
|
|
189
|
+
this.activeKey = newKey
|
|
190
|
+
}
|
|
191
|
+
// 确保回调在组件完全就绪后执行
|
|
192
|
+
if (result && result.callback && typeof result.callback === 'function') {
|
|
193
|
+
await this.$nextTick()
|
|
194
|
+
result.callback()
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
} else {
|
|
198
|
+
this.activeKey = newKey
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (initStatus) {
|
|
202
|
+
this.activeKey = this.defaultActiveKey
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
// 新增:等待组件就绪的方法
|
|
207
|
+
waitForComponentReady (index) {
|
|
208
|
+
return new Promise((resolve) => {
|
|
209
|
+
const checkComponent = () => {
|
|
210
|
+
const tab = this.config.data[index]
|
|
211
|
+
const refName = `tab_com_${tab.slotType}_${index}`
|
|
212
|
+
const ref = this.$refs[refName]
|
|
213
|
+
|
|
214
|
+
if (ref && ref[0]) {
|
|
215
|
+
// 额外检查组件内部是否已初始化(如果有相关状态的话)
|
|
216
|
+
if (ref[0].isReady !== undefined) {
|
|
217
|
+
if (ref[0].isReady) {
|
|
218
|
+
resolve(ref[0])
|
|
219
|
+
} else {
|
|
220
|
+
setTimeout(checkComponent, 50)
|
|
221
|
+
}
|
|
222
|
+
} else {
|
|
223
|
+
resolve(ref[0])
|
|
224
|
+
}
|
|
225
|
+
} else {
|
|
226
|
+
setTimeout(checkComponent, 50)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
checkComponent()
|
|
231
|
+
})
|
|
232
|
+
},
|
|
233
|
+
// 等待组件完全就绪(包括内部异步初始化)
|
|
234
|
+
waitForComponentFullyReady (component, index) {
|
|
235
|
+
return new Promise((resolve) => {
|
|
236
|
+
if (!component) {
|
|
237
|
+
resolve()
|
|
238
|
+
return
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
let attempts = 0
|
|
242
|
+
const maxAttempts = 60 // 最多等待3秒
|
|
243
|
+
const checkInterval = 50
|
|
244
|
+
|
|
245
|
+
const checkReady = () => {
|
|
246
|
+
attempts++
|
|
247
|
+
// 1. 检查组件是否有 loading 状态,确保不在加载中
|
|
248
|
+
if (component.loading === true) {
|
|
249
|
+
if (attempts < maxAttempts) {
|
|
250
|
+
setTimeout(checkReady, checkInterval)
|
|
251
|
+
return
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// 2. 检查组件是否有 loaded 状态(如 XAddNativeForm)
|
|
256
|
+
if (component.loaded === false) {
|
|
257
|
+
if (attempts < maxAttempts) {
|
|
258
|
+
setTimeout(checkReady, checkInterval)
|
|
259
|
+
return
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// 3. 检查组件是否有 realQueryConfig(如 XFormTable)
|
|
264
|
+
const tab = this.config.data[index]
|
|
265
|
+
const isFormTableType = tab?.slotType === 'x-form-table' ||
|
|
266
|
+
tab?.slotType === 'XFormTable' ||
|
|
267
|
+
tab?.slotType?.toLowerCase() === 'xformtable'
|
|
268
|
+
if (isFormTableType && component.realQueryConfig === undefined && !component.loadError) {
|
|
269
|
+
if (attempts < maxAttempts) {
|
|
270
|
+
setTimeout(checkReady, checkInterval)
|
|
271
|
+
return
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// 4. 检查关键方法是否存在(如 setForm、setFormWithKey)
|
|
276
|
+
const hasSetForm = typeof component.setForm === 'function'
|
|
277
|
+
const hasSetFormWithKey = typeof component.setFormWithKey === 'function'
|
|
278
|
+
const hasSetFormWithNoKey = typeof component.setFormWithNoKey === 'function'
|
|
279
|
+
// 5. 如果组件有这些方法,确保表单对象存在
|
|
280
|
+
if (hasSetForm || hasSetFormWithKey || hasSetFormWithNoKey) {
|
|
281
|
+
// 检查组件是否有 form 对象
|
|
282
|
+
if (component.form && Object.keys(component.form).length > 0) {
|
|
283
|
+
// 表单已初始化,等待 DOM 渲染完成
|
|
284
|
+
this.$nextTick(() => {
|
|
285
|
+
// 额外等待一个 tick,确保所有子组件也渲染完成
|
|
286
|
+
this.$nextTick(() => {
|
|
287
|
+
resolve()
|
|
288
|
+
})
|
|
289
|
+
})
|
|
290
|
+
return
|
|
291
|
+
} else if (component.loaded !== false) {
|
|
292
|
+
// 如果没有 form 对象但 loaded 不为 false,可能还在初始化
|
|
293
|
+
if (attempts < maxAttempts) {
|
|
294
|
+
setTimeout(checkReady, checkInterval)
|
|
295
|
+
return
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// 6. 如果以上都通过,或者超时,等待 DOM 渲染完成
|
|
301
|
+
if (attempts >= 3 || attempts >= maxAttempts) {
|
|
302
|
+
// 至少等待几次检查,确保有时间初始化
|
|
303
|
+
this.$nextTick(() => {
|
|
304
|
+
this.$nextTick(() => {
|
|
305
|
+
resolve()
|
|
306
|
+
})
|
|
307
|
+
})
|
|
308
|
+
return
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// 继续等待
|
|
312
|
+
setTimeout(checkReady, checkInterval)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// 首次检查前等待一个 tick
|
|
316
|
+
this.$nextTick(() => {
|
|
317
|
+
checkReady()
|
|
318
|
+
})
|
|
319
|
+
})
|
|
320
|
+
},
|
|
321
|
+
initConfig () {
|
|
322
|
+
if (this.configName) {
|
|
323
|
+
this.getConfig()
|
|
324
|
+
} else if (this.localConfig) {
|
|
325
|
+
this.config = this.localConfig
|
|
326
|
+
// 设置是否显示页签
|
|
327
|
+
this.showTabBar = this.localConfig.showTabBar !== false
|
|
328
|
+
}
|
|
329
|
+
// 重置已加载页签集合,确保配置变更后重新加载
|
|
330
|
+
this.loadedTabs = []
|
|
331
|
+
// 标记默认激活的页签为已加载
|
|
332
|
+
if (this.config && this.config.data) {
|
|
333
|
+
this.markTabAsLoaded(this.defaultActiveKey)
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
getConfig () {
|
|
337
|
+
getConfigByName(this.configName, this.serverName, res => {
|
|
338
|
+
this.config = res
|
|
339
|
+
// 设置是否显示页签
|
|
340
|
+
this.showTabBar = res.showTabBar !== false
|
|
341
|
+
// 标记默认激活的页签为已加载
|
|
342
|
+
if (this.config && this.config.data) {
|
|
343
|
+
this.markTabAsLoaded(this.defaultActiveKey)
|
|
344
|
+
}
|
|
345
|
+
}, this.env === 'dev')
|
|
346
|
+
},
|
|
347
|
+
getEventHandlers (tab, index) {
|
|
348
|
+
const handlers = {}
|
|
349
|
+
if (!tab?.events || tab?.events?.length === 0) {
|
|
350
|
+
return handlers
|
|
351
|
+
}
|
|
352
|
+
tab.events.forEach(event => {
|
|
353
|
+
handlers[event.type] = (...args) => {
|
|
354
|
+
executeStrFunctionByContext(this.$refs[`tab_com_${tab.slotType}_${index}`][0], event.customFunction, args)
|
|
355
|
+
}
|
|
356
|
+
})
|
|
357
|
+
return handlers
|
|
358
|
+
},
|
|
359
|
+
onComponentMounted (h, tab, index) {
|
|
360
|
+
if (tab.slotType === 'x-add-native-form') {
|
|
361
|
+
// 建议表单需要主动调用初始化方法
|
|
362
|
+
getConfigByName(tab.slotConfig, tab.serviceName, async (res) => {
|
|
363
|
+
// 如果配置了 表单初始化logic
|
|
364
|
+
// 调用 logic 获取参数
|
|
365
|
+
let param = {}
|
|
366
|
+
let selectedId
|
|
367
|
+
if (res.paramLogicName) {
|
|
368
|
+
if (!!this.getSelectedId) {
|
|
369
|
+
selectedId = this.getSelectedId()
|
|
370
|
+
if (typeof selectedId !== 'object') {
|
|
371
|
+
selectedId = { selectedId: selectedId }
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
param = Object.assign(param, await runLogic(res.paramLogicName, selectedId, tab.serviceName))
|
|
375
|
+
}
|
|
376
|
+
this.$refs[`tab_com_${tab.slotType}_${index}`][0].init({
|
|
377
|
+
formItems: res.formJson,
|
|
378
|
+
showSubmitBtn: !this.isInAModal,
|
|
379
|
+
businessType: '新增',
|
|
380
|
+
layout: res.xAddFormLayout,
|
|
381
|
+
...res,
|
|
382
|
+
fixedAddForm: param,
|
|
383
|
+
modifyModelData: {
|
|
384
|
+
files: param.files,
|
|
385
|
+
images: param.images
|
|
386
|
+
}
|
|
387
|
+
})
|
|
388
|
+
}, this.env === 'dev')
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
// 判断页签是否已加载
|
|
392
|
+
isTabLoaded (index) {
|
|
393
|
+
const numIndex = Number(index)
|
|
394
|
+
return this.loadedTabs.includes(numIndex) || Number(this.activeKey) === numIndex
|
|
395
|
+
},
|
|
396
|
+
// 标记页签为已加载
|
|
397
|
+
markTabAsLoaded (index) {
|
|
398
|
+
const numIndex = Number(index)
|
|
399
|
+
// 确保是有效的数字索引,且未添加过
|
|
400
|
+
if (!isNaN(numIndex) && numIndex >= 0 && !this.loadedTabs.includes(numIndex)) {
|
|
401
|
+
this.loadedTabs.push(numIndex)
|
|
402
|
+
}
|
|
403
|
+
},
|
|
404
|
+
// 确保页签已加载,如果未加载则先加载,返回 Promise 等待组件渲染完成
|
|
405
|
+
ensureTabLoaded (index, options = {}) {
|
|
406
|
+
return new Promise((resolve, reject) => {
|
|
407
|
+
const numIndex = Number(index)
|
|
408
|
+
const innerRefs = Array.isArray(options.innerRefs) ? options.innerRefs : []
|
|
409
|
+
const allowSwitch = options.allowSwitch !== false
|
|
410
|
+
// 验证索引有效性
|
|
411
|
+
if (isNaN(numIndex) || numIndex < 0) {
|
|
412
|
+
reject(new Error(`无效的页签索引: ${index}`))
|
|
413
|
+
return
|
|
414
|
+
}
|
|
415
|
+
if (!this.config || !this.config.data || !this.config.data[numIndex]) {
|
|
416
|
+
reject(new Error(`页签索引 ${index} 不存在`))
|
|
417
|
+
return
|
|
418
|
+
}
|
|
419
|
+
// 如果已经加载,直接返回组件实例
|
|
420
|
+
if (this.isTabLoaded(numIndex)) {
|
|
421
|
+
const component = this.getTabComponent(numIndex)
|
|
422
|
+
if (component) {
|
|
423
|
+
if (innerRefs.length === 0) {
|
|
424
|
+
resolve(component)
|
|
425
|
+
} else {
|
|
426
|
+
this.waitForInnerRefs(component, innerRefs, resolve, reject)
|
|
427
|
+
}
|
|
428
|
+
} else {
|
|
429
|
+
// 已标记但组件可能还在渲染中,等待一下
|
|
430
|
+
this.$nextTick(() => {
|
|
431
|
+
const comp = this.getTabComponent(numIndex)
|
|
432
|
+
if (comp) {
|
|
433
|
+
if (innerRefs.length === 0) {
|
|
434
|
+
resolve(comp)
|
|
435
|
+
} else {
|
|
436
|
+
this.waitForInnerRefs(comp, innerRefs, resolve, reject)
|
|
437
|
+
}
|
|
438
|
+
} else {
|
|
439
|
+
// 等待组件mounted
|
|
440
|
+
this.waitForComponent(numIndex, (c) => {
|
|
441
|
+
if (innerRefs.length === 0) {
|
|
442
|
+
resolve(c)
|
|
443
|
+
} else {
|
|
444
|
+
this.waitForInnerRefs(c, innerRefs, resolve, reject)
|
|
445
|
+
}
|
|
446
|
+
}, reject)
|
|
447
|
+
}
|
|
448
|
+
})
|
|
449
|
+
}
|
|
450
|
+
return
|
|
451
|
+
}
|
|
452
|
+
// 标记为已加载,触发渲染
|
|
453
|
+
this.markTabAsLoaded(numIndex)
|
|
454
|
+
// 优先尝试在不切换页签的情况下获取组件
|
|
455
|
+
this.$nextTick(() => {
|
|
456
|
+
const comp = this.getTabComponent(numIndex)
|
|
457
|
+
if (comp) {
|
|
458
|
+
if (innerRefs.length === 0) resolve(comp)
|
|
459
|
+
else this.waitForInnerRefs(comp, innerRefs, resolve, reject)
|
|
460
|
+
return
|
|
461
|
+
}
|
|
462
|
+
// 若仍未渲染,且允许临时切换,则进行一次激活-恢复回退
|
|
463
|
+
if (!allowSwitch) {
|
|
464
|
+
// 不允许切换则进入常规等待(可能更慢)
|
|
465
|
+
this.waitForComponent(numIndex, (c) => {
|
|
466
|
+
if (innerRefs.length === 0) resolve(c)
|
|
467
|
+
else this.waitForInnerRefs(c, innerRefs, resolve, reject)
|
|
468
|
+
}, reject)
|
|
469
|
+
return
|
|
470
|
+
}
|
|
471
|
+
const prevActive = this.activeKey
|
|
472
|
+
this.activeKey = numIndex
|
|
473
|
+
this.$nextTick(() => {
|
|
474
|
+
this.waitForComponent(numIndex, (c) => {
|
|
475
|
+
// 组件到位后再恢复原激活页签
|
|
476
|
+
if (prevActive !== numIndex) {
|
|
477
|
+
this.$nextTick(() => { this.activeKey = prevActive })
|
|
478
|
+
}
|
|
479
|
+
if (innerRefs.length === 0) resolve(c)
|
|
480
|
+
else this.waitForInnerRefs(c, innerRefs, resolve, reject)
|
|
481
|
+
}, reject)
|
|
482
|
+
})
|
|
483
|
+
})
|
|
484
|
+
})
|
|
485
|
+
},
|
|
486
|
+
// 等待组件渲染完成
|
|
487
|
+
waitForComponent (index, resolve, reject) {
|
|
488
|
+
const maxAttempts = 50 // 最多尝试50次
|
|
489
|
+
let attempts = 0
|
|
490
|
+
const checkInterval = 50 // 每50ms检查一次
|
|
491
|
+
const tab = this.config.data[index]
|
|
492
|
+
const refName = `tab_com_${tab.slotType}_${index}`
|
|
493
|
+
const checkComponent = () => {
|
|
494
|
+
attempts++
|
|
495
|
+
const component = this.$refs[refName]
|
|
496
|
+
if (component && component[0]) {
|
|
497
|
+
resolve(component[0])
|
|
498
|
+
return
|
|
499
|
+
}
|
|
500
|
+
if (attempts >= maxAttempts) {
|
|
501
|
+
reject(new Error(`等待页签 ${index} 组件加载超时`))
|
|
502
|
+
return
|
|
503
|
+
}
|
|
504
|
+
setTimeout(checkComponent, checkInterval)
|
|
505
|
+
}
|
|
506
|
+
// 首次检查
|
|
507
|
+
this.$nextTick(() => {
|
|
508
|
+
checkComponent()
|
|
509
|
+
})
|
|
510
|
+
},
|
|
511
|
+
// 等待内部 $refs 就绪
|
|
512
|
+
waitForInnerRefs (component, innerRefs, resolve, reject) {
|
|
513
|
+
const maxAttempts = 50
|
|
514
|
+
let attempts = 0
|
|
515
|
+
const checkInterval = 50
|
|
516
|
+
const allReady = () => innerRefs.every(name => component.$refs && component.$refs[name])
|
|
517
|
+
const check = () => {
|
|
518
|
+
attempts++
|
|
519
|
+
if (allReady()) {
|
|
520
|
+
resolve(component)
|
|
521
|
+
return
|
|
522
|
+
}
|
|
523
|
+
if (attempts >= maxAttempts) {
|
|
524
|
+
reject(new Error(`等待内部 refs [${innerRefs.join(', ')}] 就绪超时`))
|
|
525
|
+
return
|
|
526
|
+
}
|
|
527
|
+
setTimeout(check, checkInterval)
|
|
528
|
+
}
|
|
529
|
+
this.$nextTick(() => check())
|
|
530
|
+
},
|
|
531
|
+
// 获取页签的组件实例
|
|
532
|
+
getTabComponent (index) {
|
|
533
|
+
const numIndex = Number(index)
|
|
534
|
+
if (isNaN(numIndex) || numIndex < 0 || !this.config || !this.config.data || !this.config.data[numIndex]) {
|
|
535
|
+
return null
|
|
536
|
+
}
|
|
537
|
+
const tab = this.config.data[numIndex]
|
|
538
|
+
const refName = `tab_com_${tab.slotType}_${numIndex}`
|
|
539
|
+
const ref = this.$refs[refName]
|
|
540
|
+
return ref && ref[0] ? ref[0] : null
|
|
541
|
+
},
|
|
542
|
+
|
|
543
|
+
},
|
|
544
|
+
props: {
|
|
545
|
+
// 配置名
|
|
546
|
+
configName: {
|
|
547
|
+
type: String,
|
|
548
|
+
default: undefined
|
|
549
|
+
},
|
|
550
|
+
serverName: {
|
|
551
|
+
type: String,
|
|
552
|
+
default: undefined
|
|
553
|
+
},
|
|
554
|
+
env: {
|
|
555
|
+
type: String,
|
|
556
|
+
default: 'prod'
|
|
557
|
+
},
|
|
558
|
+
// 本地配置,调试用
|
|
559
|
+
localConfig: {
|
|
560
|
+
type: Object,
|
|
561
|
+
default: undefined
|
|
562
|
+
},
|
|
563
|
+
tabBarGutter: {
|
|
564
|
+
type: Number,
|
|
565
|
+
default: 10
|
|
566
|
+
},
|
|
567
|
+
bodyStyle: {
|
|
568
|
+
type: Object,
|
|
569
|
+
default: () => {
|
|
570
|
+
return {}
|
|
571
|
+
}
|
|
572
|
+
},
|
|
573
|
+
defaultActiveKey: {
|
|
574
|
+
type: [String, Number],
|
|
575
|
+
default: 0
|
|
576
|
+
},
|
|
577
|
+
// 新增compProp参数,用于穿透到动态组件
|
|
578
|
+
compProp: {
|
|
579
|
+
type: Object,
|
|
580
|
+
default: () => {
|
|
581
|
+
return {}
|
|
582
|
+
}
|
|
583
|
+
},
|
|
584
|
+
// 额外数据
|
|
585
|
+
extraData: {
|
|
586
|
+
type: Object,
|
|
587
|
+
default: () => {
|
|
588
|
+
return {}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
},
|
|
592
|
+
watch: {
|
|
593
|
+
configName: {
|
|
594
|
+
deep: true,
|
|
595
|
+
immediate: true,
|
|
596
|
+
handler (val) {
|
|
597
|
+
this.initConfig()
|
|
598
|
+
}
|
|
599
|
+
},
|
|
600
|
+
// 如果本地配置更改了,重新初始化
|
|
601
|
+
localConfig: {
|
|
602
|
+
deep: true,
|
|
603
|
+
immediate: true,
|
|
604
|
+
handler (val) {
|
|
605
|
+
this.initConfig()
|
|
606
|
+
}
|
|
607
|
+
},
|
|
608
|
+
},
|
|
609
|
+
}
|
|
610
|
+
</script>
|
|
611
|
+
<style scoped>
|
|
612
|
+
</style>
|