vue2-client 1.20.78 → 1.20.79

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.
@@ -1,1288 +1,1286 @@
1
- <template>
2
- <div>
3
- <!-- 骨架屏 -->
4
- <a-card v-if="showSkeleton">
5
- <a-skeleton active />
6
- </a-card>
7
- <!-- 主体表格 -->
8
- <XReportDesign
9
- @selectRow="selectRow"
10
- @slotRendered="slotRendered"
11
- @allSlotsConfigEnded="onAllSlotsConfigEnded"
12
- v-if="scanFinish"
13
- :display-only="displayOnly"
14
- :config="originalConfig"
15
- :slot-config-name="undefined"
16
- :for-display="true"
17
- ref="XReportDesign"
18
- :server-name="serverName"
19
- :env="env"
20
- :show-title="showTitle"
21
- @listClick="listClick"
22
- >
23
- </XReportDesign>
24
- <a-row
25
- type="flex"
26
- justify="end"
27
- v-if="showSaveButton"
28
- >
29
- <a-space>
30
- <a-button @click="saveConfig">
31
- 提交
32
- </a-button>
33
- <a-button @click="cancelConfig">
34
- 取消
35
- </a-button>
36
- </a-space>
37
- </a-row>
38
- <!-- 弹出框 -->
39
- <x-add-report
40
- :env="env"
41
- ref="xAddReport"
42
- />
43
- <!-- 弹出框 -->
44
- <x-report-drawer
45
- :env="env"
46
- ref="xReportDrawer"
47
- />
48
- </div>
49
- </template>
50
- <script>
51
- /* eslint-disable */
52
- // 转PDF用
53
- import { mapState } from 'vuex'
54
- import { getConfigByName, runLogic } from '@vue2-client/services/api/common'
55
- import XReportDesign from './XReportDesign.vue'
56
- import { executeStrFunctionByContext } from '@vue2-client/utils/runEvalFunction'
57
- import { shortcutManager } from '@vue2-client/base-client/utils/shortcutManager'
58
-
59
- // import XAddReport from '@vue2-client/base-client/components/common/XAddReport'
60
-
61
- export default {
62
- name: 'XReport',
63
- props: {
64
- files: {
65
- type: Array,
66
- default: () => {
67
- return []
68
- },
69
- },
70
- // 控制用户权限,user和admin
71
- authority: {
72
- type: String,
73
- default: 'user',
74
- },
75
- // 是否为编辑模式
76
- editMode: {
77
- type: Boolean,
78
- default: true,
79
- },
80
- // 配置名
81
- configName: {
82
- type: String,
83
- required: true,
84
- },
85
- // 插槽名
86
- activatedSlotName: {
87
- type: String,
88
- default: undefined,
89
- },
90
- // 本地配置,调试用
91
- localConfig: {
92
- type: Object,
93
- default: undefined,
94
- },
95
- // 兼容老版本配置
96
- dontFormat: {
97
- type: Boolean,
98
- default: true,
99
- },
100
- showImgInCell: {
101
- type: Boolean,
102
- default: false,
103
- },
104
- // 数据
105
- configData: {
106
- type: Object,
107
- default: undefined,
108
- },
109
- // 命名空间
110
- serverName: {
111
- type: String,
112
- default: process.env.VUE_APP_SYSTEM_NAME,
113
- },
114
- // 环境
115
- env: {
116
- type: String,
117
- default: 'prod',
118
- },
119
- // 只做展示
120
- displayOnly: {
121
- type: Boolean,
122
- default: true,
123
- },
124
- // 表格没有边距
125
- noPadding: {
126
- type: Boolean,
127
- default: true,
128
- },
129
- // 表格没有上边框,与noPadding搭配可以实现连续表格
130
- noTopBorder: {
131
- type: Boolean,
132
- default: false,
133
- },
134
- // 是否展示标题
135
- showTitle: {
136
- type: Boolean,
137
- default: true,
138
- },
139
- // 是否展示保存按钮
140
- showSaveButton: {
141
- type: Boolean,
142
- default: false,
143
- },
144
- // 是否将组件注册到外层提供的容器中,方便外侧统一保存
145
- registerMap: {
146
- type: Array,
147
- default: undefined,
148
- },
149
- // TAB 页签时由 XTab 传入,仅接收不参与逻辑(注册由 XTab onComponentMounted 完成)
150
- slotRef: {
151
- type: String,
152
- default: undefined,
153
- },
154
- // 是否小插件模式,小插件模式不会有各种边境
155
- isWidget: {
156
- type: Boolean,
157
- default: false,
158
- },
159
- // 图片是否使用OSS来保存
160
- useOssForImg: {
161
- type: Boolean,
162
- default: true,
163
- },
164
- // 图片上传后添加前缀
165
- imgPrefix: {
166
- type: String,
167
- default: undefined,
168
- },
169
- },
170
- components: {
171
- XAddReport: () =>
172
- import('@vue2-client/base-client/components/common/XAddReport'),
173
- XReportDrawer: () =>
174
- import('@vue2-client/base-client/components/common/XReportDrawer'),
175
- XReportDesign,
176
- },
177
- data () {
178
- return {
179
- // 控制骨架屏显隐
180
- showSkeleton: true,
181
- // 配置
182
- config: undefined,
183
- // 当前显示模式,编辑模式,预览模式
184
- type: 'design',
185
- // 仅供展示,不可编辑
186
- onlyDisplay: false,
187
- // 每行最大列数,非必要请勿更改,现在的设计器完全是基于每行12列来设计的
188
- maxColSpan: 12,
189
- // 定义是否完成配置的扫描,未完成不要渲染子组件
190
- scanFinish: false,
191
- // 当前激活的配置文件
192
- activeConfig: null,
193
- // 原始配置文件
194
- // 用于展示。某些情况下“设计页”中的内容仅为“预览页”表格其中的一部分
195
- originalConfig: null,
196
- // 扫描到的配置
197
- configFromWeb: {},
198
- // 用于获取配置的锁
199
- timer: undefined,
200
- // 是否包含图片
201
- hasImages: false,
202
- // 图片列表
203
- imageList: [],
204
- // 保存最原始的数据,用于判断哪些数据被更改了
205
- dataCache: undefined,
206
- // 判断哪些数据被更改了,存储对应的key
207
- diff: [],
208
- globalData: {},
209
- // setData 在 config 未就绪时传入的 data,配置彻底结束后再执行
210
- // eslint-disable-next-line
211
- _pendingSetData: undefined,
212
- // 配置是否彻底结束:本报表配置就绪且所有 slot 子组件配置也结束(由 allSlotsConfigEnded 事件获知)
213
- configEnded: false,
214
- // 是否已收到 Design 的 allSlotsConfigEnded(所有 slot 子组件配置结束)
215
- allSlotsConfigEnded: false,
216
- // 本报表注册到全局组件注册表的名称(仅当配置了 globalRegistryName 时才有),destroy 时统一注销
217
- // eslint-disable-next-line
218
- _globalRegistrySlotNames: [],
219
- // 等待配置结束的 Promise,供 waitConfigEnd() 使用
220
- _configEndPromise: null,
221
- _configEndResolve: null,
222
- }
223
- },
224
- inject: {
225
- // 从祖先获取 getGridScopePath(可能是外层 XReport 或 XTab 提供的)
226
- upstreamGetGridScopePath: {
227
- from: 'getGridScopePath',
228
- default: null
229
- }
230
- },
231
- beforeDestroy () {
232
- clearInterval(this.timer)
233
- if (this.$componentRegistry && this._globalRegistrySlotNames.length) {
234
- this._globalRegistrySlotNames.forEach((name) => {
235
- this.$componentRegistry.unregister(name)
236
- })
237
- this._globalRegistrySlotNames = []
238
- }
239
- },
240
- watch: {
241
- // 如果配置名更改了,重新获取配置
242
- configName (val) {
243
- if (val) {
244
- // 先清空配置,避免异步期间使用旧配置注册快捷键
245
- this.originalConfig = null
246
- this.scanFinish = false
247
- getConfigByName(
248
- this.configName,
249
- undefined,
250
- (res) => {
251
- this.config = res
252
- this.configInit()
253
- this.$logger('report.configLoad').log('configName 加载完成', { configName: this.configName })
254
- },
255
- this.env === 'dev'
256
- )
257
- }
258
- },
259
- // 如果本地配置更改了,重新初始化
260
- localConfig: {
261
- deep: true,
262
- immediate: true,
263
- handler (val) {
264
- if (val) {
265
- this.config = val
266
- this.configInit()
267
- this.$logger('report.configLoad').log('localConfig 变更完成', { configName: this.configName })
268
- }
269
- },
270
- },
271
- },
272
- provide () {
273
- const report = this
274
- return {
275
- runLogic: runLogic,
276
- openDialog: report.openDialog.bind(report),
277
- openDrawer: report.openDrawer.bind(report),
278
- registerComponent: report.registerComponent.bind(report),
279
- getComponentByName: report.getComponentByName.bind(report),
280
- getParentComponentByName: report.getComponentByName.bind(report),
281
- getConfigByName: getConfigByName,
282
- isWidget: report.widget,
283
- currUser: report.currUser,
284
- getGlobalData: report.getGlobalData.bind(report),
285
- setGlobalData: report.setGlobalData.bind(report),
286
- findComponentByName: report.findComponentByName.bind(report),
287
- closeAddReport: report.closeAddReport.bind(report),
288
- setData: report.setData.bind(report),
289
- getRootReport: () => report,
290
- getGridScopePath: () => {
291
- // 1. 优先从注入的上游函数获取父路径
292
- let parent = ''
293
- if (typeof report.upstreamGetGridScopePath === 'function') {
294
- parent = report.upstreamGetGridScopePath()
295
- }
296
- // 2. 如果没有注入(根实例),回退到 prop(兼容旧写法)
297
- if (!parent && report.parentScopePath) {
298
- parent = report.parentScopePath
299
- }
300
- // 3. 拼上自己的 configName
301
- const current = report.configName
302
- return parent ? `${parent}/${current}` : current
303
- }
304
- }
305
- },
306
- methods: {
307
- findComponentByName (instance, componentName, maxDepth, findType) {
308
- findType = findType || 'children'
309
- maxDepth = maxDepth || 50
310
- // 基础判断:如果实例不存在或者已经超过最大深度,返回null
311
- if (!instance || maxDepth <= 0) {
312
- return null
313
- }
314
-
315
- // 检查当前实例是否有目标组件
316
- try {
317
- if (instance.getComponentByName && instance.getComponentByName(componentName)) {
318
- return instance.getComponentByName(componentName)
319
- }
320
- } catch (e) {
321
- this.$logger('report.findComponent').warn('查找组件时发生错误:', e)
322
- }
323
-
324
- // 递归查找子组件
325
- if (findType === 'parent') {
326
- // 向上查找父组件
327
- if (instance.$parent) {
328
- return this.findComponentByName(instance.$parent, componentName, maxDepth - 1, findType)
329
- }
330
- } else {
331
- if (instance.$children && instance.$children.length) {
332
- for (const child of instance.$children) {
333
- const result = this.findComponentByName(child, componentName, maxDepth - 1, findType)
334
- if (result) {
335
- return result
336
- }
337
- }
338
- }
339
- }
340
- return null
341
- },
342
- listClick (data) {
343
- this.$emit('listClick', data)
344
- },
345
- closeAddReport () {
346
- if (this.$refs.xAddReport) {
347
- this.$refs.xAddReport.close()
348
- }
349
- let parent = this.$parent
350
- while (parent) {
351
- if (parent.$refs && parent.$refs.xAddReport) {
352
- const addReportRefs = parent.$refs.xAddReport
353
- if (Array.isArray(addReportRefs)) {
354
- addReportRefs.forEach(ref => {
355
- if (ref && typeof ref.close === 'function') {
356
- ref.close()
357
- }
358
- })
359
- } else if (typeof addReportRefs.close === 'function') {
360
- addReportRefs.close()
361
- }
362
- }
363
- parent = parent.$parent
364
- }
365
- },
366
- slotRendered () {
367
- this.$logger('report.slotRendered').log('slotRendered 进入', { configName: this.configName, hasPendingSetData: this._pendingSetData !== undefined })
368
- if (this.config?.mountedFunction) {
369
- let func = this.config.mountedFunction
370
- if (func && func.startsWith('function')) {
371
- func = func.replace('function', 'async function')
372
- executeStrFunctionByContext(this, func, [this])
373
- }
374
- }
375
- },
376
- onAllSlotsConfigEnded () {
377
- this.allSlotsConfigEnded = true
378
- this._markConfigEnded()
379
- },
380
- // 把组件注册到refs中,方便调用;若传入 options.globalRegistryName 则同时注册到全局组件注册表(向后兼容:无该字段则不注册)
381
- registerComponent (componentName, component, options = {}) {
382
- this.$refs[componentName] = component
383
- const globalName = options.globalRegistryName
384
- if (globalName && this.$componentRegistry) {
385
- try {
386
- this.$componentRegistry.register(globalName, component)
387
- this._globalRegistrySlotNames.push(globalName)
388
- } catch (e) {
389
- this.$logger('report.registerComponent').warn('全局组件注册表注册失败:', e?.message || e)
390
- }
391
- }
392
- // 如果配置了 mountedLogicName,则运行该逻辑
393
- if (options.mountedLogicName) {
394
- this.runMountedLogic(component, options)
395
- }
396
- },
397
-
398
- // 运行组件挂载后的逻辑
399
- async runMountedLogic (component, options = {}) {
400
- const { mountedLogicName, mountedLogicMode, mountedLogicParam } = options
401
- if (!mountedLogicName) return
402
- try {
403
- const param = {
404
- proxy: component,
405
- ...(mountedLogicParam || {})
406
- }
407
- await this.$runFrontLogic(mountedLogicName, param, mountedLogicMode)
408
- } catch (e) {
409
- const cause = e?.cause || e
410
- if (cause?.name === 'AbortError' || cause?.name === 'CanceledError' || cause?.code === 'ERR_CANCELED') {
411
- return
412
- }
413
- this.$logger('report.runMountedLogic').warn('mountedLogicName 执行失败:', e?.message || e)
414
- }
415
- },
416
-
417
- // 把设计的table布局转换成可显示的珊格布局
418
- transformArray (inputList) {
419
- let operationIndex = 0
420
- let operationList = []
421
- const outputList = []
422
- for (const lst of inputList) {
423
- // 如果列表为空或只有一个元素,则所有元素相等。比较列表中每个元素是否与第一个元素相等
424
- if (
425
- lst.length >= 1 &&
426
- !lst.every(
427
- (x) =>
428
- Array.isArray(x) ||
429
- Array.isArray(lst[0]) ||
430
- x.rowSpan === lst[0].rowSpan
431
- )
432
- ) {
433
- operationList = lst
434
- break // 使用 break 退出整个循环
435
- } else {
436
- // 被操作的行
437
- operationIndex += 1
438
- }
439
- }
440
-
441
- let maxMergeRow = 0
442
-
443
- // 没有需要合并的行,直接返回
444
- if (operationList.length === 0) {
445
- return inputList
446
- } else {
447
- // 当前行的最大值
448
- const maxRow = Math.max(...operationList.map((item) => item.rowSpan))
449
- let mergeIndexCol = 0
450
- for (let index = 0; index < operationList.length; index++) {
451
- const row = operationList[index]
452
- let rowSpan = row.rowSpan
453
- // 需要合并的行
454
- let mergeIndexRow = operationIndex + 1
455
- // 存放合并后的行
456
- const rows = []
457
- // 添加当前行
458
- if (rowSpan < maxRow && mergeIndexRow < inputList.length) {
459
- rows.push([row])
460
- }
461
- // 当前需要被操作并且操作行没有超出列表的高度
462
- while (rowSpan < maxRow && mergeIndexRow < inputList.length) {
463
- rowSpan += inputList[mergeIndexRow][mergeIndexCol].rowSpan
464
- // 放一行到合并结果
465
- rows.push([inputList[mergeIndexRow][mergeIndexCol]])
466
- if (mergeIndexRow > maxMergeRow) {
467
- // 记录最多操作到了哪行
468
- maxMergeRow = mergeIndexRow
469
- }
470
- mergeIndexRow++
471
- }
472
- // operation_list赋值, 没有变化的,不处理
473
- if (rows.length !== 0) {
474
- operationList[index] = rows
475
- }
476
- if (row.rowSpan !== maxRow) {
477
- mergeIndexCol++ // 操作列转为下一列
478
- }
479
- }
480
- }
481
-
482
- // 组成outputlist, operation_list前部填入
483
- let putindex = 0
484
- while (operationIndex > 0) {
485
- outputList.push(inputList[putindex])
486
- putindex++
487
- operationIndex -= 1
488
- }
489
-
490
- outputList.push(operationList)
491
-
492
- // 组成outputlist, operation_list后部填入
493
- while (maxMergeRow < inputList.length - 1) {
494
- outputList.push(inputList[maxMergeRow + 1])
495
- maxMergeRow += 1
496
- }
497
-
498
- return this.transformArray(outputList)
499
- },
500
- // 根据名字从注册到组件中获取组件
501
- getComponentByName (componentName) {
502
- return this.$refs[componentName]
503
- },
504
- getGlobalData () {
505
- return this.globalData
506
- },
507
- setGlobalData (obj) {
508
- this.globalData = obj
509
- },
510
- /**
511
- * 配置彻底结束:本报表配置就绪且所有 slot 子组件配置也结束。打标、对外发 configEnd,再执行缓存的 setData。
512
- * onAllSlotsConfigEnded(有 slot 且收到 allSlotsConfigEnded)或 configInit 的 nextTick(无 slot)调用,仅执行一次。
513
- */
514
- _markConfigEnded () {
515
- if (this.configEnded) return
516
- const hasSlots = (this.config?.slotsDeclare?.length || 0) > 0
517
- if (hasSlots && !this.allSlotsConfigEnded) return
518
- this.configEnded = true
519
- this.$logger('report.configEnd').log('配置彻底结束,发送 configEnd', { configName: this.configName })
520
- this.$emit('configEnd')
521
- if (this._configEndResolve) {
522
- this._configEndResolve()
523
- this._configEndResolve = null
524
- }
525
- this.$nextTick(() => this._flushPendingSetData())
526
- },
527
- /**
528
- * 等待配置结束的异步方法。配置已结束则立即 resolve,否则在 configEnd 时 resolve。
529
- * @returns {Promise<void>}
530
- */
531
- waitConfigEnd () {
532
- if (this.configEnded) return Promise.resolve()
533
- return this._configEndPromise || Promise.resolve()
534
- },
535
- /**
536
- * 内部:直接执行 dataProcessScript。供 setData(configEnded 时)和 _flushPendingSetData 调用。
537
- */
538
- async _runDataProcessScript (data) {
539
- const script = this.config?.dataProcessScript
540
- if (!script || typeof script !== 'string') return
541
- try {
542
- const result = executeStrFunctionByContext(this, script, [data])
543
- if (result instanceof Promise) await result
544
- } catch (e) {
545
- this.$logger('report.dataProcessScript').error('dataProcessScript 执行异常:', e)
546
- }
547
- },
548
- /**
549
- * 对外数据设置:仅当配置中存在 dataProcessScript 时执行该脚本,传入 data;不修改组件自身任何数据。
550
- * 无脚本时缓存 data 并 return。有脚本但配置未彻底结束(configEnded 为 false)时也缓存,等 configEnd 后再执行。
551
- * 只有配置彻底结束后才执行 dataProcessScript。脚本内 this 为当前 XReport 实例。
552
- * @param {*} data 外部传入的数据,供脚本使用
553
- * @returns {Promise<void>}
554
- */
555
- async setData (data) {
556
- this.$logger('report.setData').log('setData 进入', { configName: this.configName, configEnded: this.configEnded, hasData: data !== undefined, dataKeys: data && typeof data === 'object' ? Object.keys(data) : [] })
557
- const script = this.config?.dataProcessScript
558
- if (!script || typeof script !== 'string') {
559
- this._pendingSetData = data
560
- this.$logger('report.setData').warn('分支: 无脚本,已缓存 data', { hasConfig: !!this.config, hasScript: !!(this.config && this.config.dataProcessScript), configName: this.configName })
561
- return
562
- }
563
- if (!this.configEnded) {
564
- this._pendingSetData = data
565
- this.$logger('report.setData').log('分支: 配置未结束,已缓存 data,等 configEnd 后执行', { configName: this.configName })
566
- return
567
- }
568
- this._pendingSetData = undefined
569
- this.$logger('report.setData').log('分支: 配置已结束,立即执行 dataProcessScript', { configName: this.configName })
570
- await this._runDataProcessScript(data)
571
- },
572
- /**
573
- * 执行缓存的 setData:取 _pendingSetData 并直接执行脚本,不经过 setData,避免 setData 再次判断 defer 导致脚本不执行。
574
- */
575
- _flushPendingSetData () {
576
- const hasPending = this._pendingSetData !== undefined
577
- this.$logger('report.flushPendingSetData').log('_flushPendingSetData 进入', { configName: this.configName, hasPending })
578
- if (!hasPending) return
579
- const d = this._pendingSetData
580
- this._pendingSetData = undefined
581
- this.$logger('report.flushPendingSetData').log('_flushPendingSetData 直接执行 dataProcessScript', { configName: this.configName })
582
- this._runDataProcessScript(d)
583
- },
584
- /**
585
- * @param configName 栅格配置名称
586
- * @param selectedId 选中得id
587
- * @param mixinData 需要混入得数据
588
- * @param outEnv 其他传递给打开窗口的数据
589
- * @param attr 传递给Modal弹框用的信息
590
- * @param manageScope 管理快捷键作用域
591
- */
592
- openDialog(configName, selectedId, mixinData, outEnv = {}, attr = {}, showButtons = true, manageScope = false) {
593
- if (manageScope) {
594
- this.openDialogWithScope(configName, selectedId, mixinData, outEnv, attr, showButtons)
595
- return
596
- }
597
-
598
- this.$refs.xAddReport.init({
599
- configName: configName,
600
- selectedId: selectedId,
601
- mixinData: mixinData,
602
- outEnv: outEnv,
603
- attr,
604
- showButtons: showButtons
605
- })
606
- },
607
- // 打开弹窗(带快捷键作用域管理)
608
- openDialogWithScope(configName, selectedId, mixinData, outEnv = {}, attr = {}, showButtons = true) {
609
- const savedScopes = new Map()
610
- if (shortcutManager.isEnabled) {
611
- shortcutManager.scopeActive.forEach((active, scopeId) => {
612
- savedScopes.set(scopeId, active)
613
- shortcutManager.setScopeActive(scopeId, false)
614
- })
615
- }
616
-
617
- const xAddReport = this.$refs.xAddReport
618
- if (xAddReport && !xAddReport._shortcutPatched) {
619
- xAddReport._shortcutPatched = true
620
- const originalClose = xAddReport.close.bind(xAddReport)
621
- xAddReport.close = () => {
622
- if (shortcutManager.isEnabled && savedScopes.size > 0) {
623
- savedScopes.forEach((wasActive, scopeId) => {
624
- if (wasActive) shortcutManager.setScopeActive(scopeId, true)
625
- })
626
- }
627
- originalClose()
628
- }
629
- }
630
-
631
- xAddReport.init({ configName, selectedId, mixinData, outEnv, attr, showButtons })
632
- },
633
-
634
- openDrawer (configName, selectedId, mixinData, outEnv = {}, attr = {}) {
635
- this.$refs.xReportDrawer.init({
636
- configName,
637
- selectedId,
638
- mixinData,
639
- outEnv,
640
- attr,
641
- })
642
- },
643
- // 向外暴露图片修改后的数据,某些外部需要自己管理图片的保存与修改
644
- updateImg (data) {
645
- this.$emit('updateImg', data)
646
- },
647
- // 导出数据,某些外部需要统一控制数据的变动
648
- exportData () {
649
- // 获取当前修改后的数据
650
- let tempData
651
- if (this.activeConfig === undefined || this.activeConfig === null) {
652
- tempData = this.originalConfig.data
653
- } else {
654
- const tempDataKeys = Object.keys(this.activeConfig.tempData)
655
- tempDataKeys.forEach((key) => {
656
- this.changeDeepObject(
657
- this.activeConfig.data,
658
- key,
659
- this.activeConfig.tempData[key]
660
- )
661
- })
662
- tempData = this.activeConfig.data
663
- }
664
- // 对比数据的差异
665
- this.diff = []
666
- this.compareProps(tempData, this.dataCache)
667
- this.diff.forEach((eachDiff) => {
668
- const arr = eachDiff.split('.')
669
- let targetData = tempData[arr[0]]
670
- if (arr.length !== 1) {
671
- for (let i = 1; i < arr.length - 1; i++) {
672
- const path = arr[i]
673
- targetData = targetData[path]
674
- }
675
- }
676
- // 将修改的数据,添加update = true属性
677
- targetData.update = true
678
- })
679
- return tempData
680
- },
681
- // 对比两个obj有哪里不同
682
- compareProps (obj1, obj2, path = '') {
683
- for (const key in obj1) {
684
- // 如果一个是undefined
685
- if (typeof obj2[key] === 'undefined') {
686
- this.diff.push(path + key)
687
- // 如果是数组长度不一样
688
- } else if (Array.isArray(obj1) && Array.isArray(obj2)) {
689
- if (obj1[key].length !== obj2[key].length) {
690
- this.diff.push(path + key)
691
- }
692
- // 如果都是对象,并且存在同样的key,递归子key
693
- } else if (
694
- typeof obj1[key] === 'object' &&
695
- typeof obj2[key] === 'object'
696
- ) {
697
- this.compareProps(obj1[key], obj2[key], path + key + '.')
698
- // 如果不是obj,对比其数据
699
- } else if (obj1[key] !== obj2[key]) {
700
- this.diff.push(path + key)
701
- }
702
- }
703
- },
704
- selectRow (selectedRowKeys, selectedRows) {
705
- this.table_selectedRowKeys = selectedRowKeys
706
- this.table_selectedRows = selectedRows
707
- this.$emit('selectRow', selectedRowKeys, selectedRows)
708
- },
709
- // 注册组件到$refs中;若传入 options.globalRegistryName 则同时注册到全局组件注册表(向后兼容:无该字段则不注册)
710
- registerComponentToRefs (componentName, component, options = {}) {
711
- this.$refs[componentName] = component
712
- const globalName = options.globalRegistryName
713
- if (globalName && this.$componentRegistry) {
714
- try {
715
- this.$componentRegistry.register(globalName, component)
716
- this._globalRegistrySlotNames.push(globalName)
717
- } catch (e) {
718
- this.$logger('report.registerComponentToRefs').warn('全局组件注册表注册失败:', e?.message || e)
719
- }
720
- }
721
- // 如果配置了 mountedLogicName,则运行该逻辑
722
- if (options.mountedLogicName) {
723
- this.runMountedLogic(component, options)
724
- }
725
- },
726
-
727
- // 提交处理,调用配置中的函数
728
- saveConfig () {
729
- const funcStr = this.config.confirmFunction
730
- executeStrFunctionByContext(this, funcStr, [this])
731
- },
732
-
733
- // 取消处理
734
- cancelConfig () {
735
- this.$emit('cancel')
736
- },
737
-
738
- // 通过@@@分割临时变量,找到对应的key,并修改它的值
739
- changeDeepObject (obj, strPath, newVal) {
740
- const arr = strPath.split('@@@')
741
- if (obj[arr[0]] === undefined) {
742
- obj = obj.images
743
- }
744
- if (arr.length === 1) {
745
- obj[arr[0]] = newVal
746
- } else {
747
- let result = obj[arr[0]]
748
- arr.shift()
749
- while (arr.length > 1) {
750
- result = result[arr[0]]
751
- arr.shift()
752
- }
753
- if (result) {
754
- result[arr[0]] = newVal
755
- }
756
- }
757
- },
758
- // 检查slot是否在配置文件中包含,如果没有包含,则视为非法获取
759
- checkSlotDefine (config) {
760
- const slotsDeclare = config.slotsDeclare
761
- const total = slotsDeclare.length
762
- let count = 0
763
- slotsDeclare.forEach((declare) => {
764
- config.columns.forEach((row) => {
765
- row.forEach((cell) => {
766
- if (cell.slotConfig === declare) {
767
- count++
768
- }
769
- })
770
- })
771
- })
772
-
773
- return count === total
774
- },
775
- // 用于分割配置中的colums,将需要处理的数组提取出来
776
- formatConfigRow () {
777
- for (let i = 0; i < this.config.columns.length; i++) {
778
- // 对原始数组进行递归,依次将该位置拆分为三个部分,当前处理位置之前的,当前处理位置,当前处理位置之后的
779
- const before = this.config.columns.slice(0, i)
780
- const after = this.config.columns.slice(
781
- i + 1,
782
- this.config.columns.length
783
- )
784
-
785
- // 将当前处理的数组交给处理的方法
786
- const x = this.checkRow(this.config.columns[i])
787
-
788
- const newArr = []
789
-
790
- // 拼接之前的数组
791
- if (before.length > 0) {
792
- if (before.length >= 1) {
793
- before.forEach((item) => {
794
- newArr.push(item)
795
- })
796
- } else {
797
- newArr.push(before)
798
- }
799
- }
800
-
801
- // 拼接不需要更改当前节点处理完成的数组
802
- newArr.push(x.old)
803
-
804
- // 如果处理了新加的数据,拼接
805
- if (x.add.length > 0) {
806
- for (let j = 0; j < x.add.length; j++) {
807
- if (x.add[j]) {
808
- newArr.push(x.add[j])
809
- i++
810
- }
811
- }
812
- }
813
-
814
- // 拼接之后的数组
815
- if (after.length > 0) {
816
- if (after.length >= 1) {
817
- after.forEach((item) => {
818
- newArr.push(item)
819
- })
820
- } else {
821
- newArr.push(after)
822
- }
823
- }
824
-
825
- this.config.columns = newArr
826
- }
827
- },
828
- // 路径中含有@@@的key,将其解析,并返回其数据
829
- getDeepObject (obj, strPath) {
830
- const arr = strPath.split('@@@')
831
- let result = obj[arr[0]]
832
- arr.shift()
833
- try {
834
- while (arr.length > 0) {
835
- result = result[arr[0]]
836
- arr.shift()
837
- }
838
- } catch (e) {
839
- result = undefined
840
- }
841
- return result
842
- },
843
- // 处理colums数组,为声明了rowspan的单元格,自动匹配格式
844
- checkRow (rowArr) {
845
- // 不需要更改的数据
846
- const original = []
847
- // 需要更改新加的数据
848
- const addArr = []
849
- // 统计rowspan出现的总和
850
- let count = 0
851
- // 统计声明列需要的rowspan总数
852
- let total = 0
853
- // 是否为声明行
854
- let titleCellFlag = false
855
- // 是否为声明行后第一行
856
- let firstSubLine = false
857
- // 需要处理的行,新的index值
858
- let subRowIndex = 0
859
- // 新生成的行,临时存储
860
- const waitForAddArr = []
861
- // 用于记录声明行的colspan避免格式错误
862
- let preColSpan = 0
863
- // 用于统计循环次数,判断是否是最后一次
864
- let forEachCount = 0
865
-
866
- // 标记所有数据
867
- rowArr.forEach((cell) => {
868
- forEachCount++
869
- // 如果该行没有rowspan则默认其为1,不要影响统计结果
870
- if (!cell.rowSpan) {
871
- cell.rowSpan = 0
872
- }
873
-
874
- if (cell.text && total !== 0) {
875
- // 如果遇到了下一个声明行,证明rowspan少了一行,需要补充一个占位格
876
- const nullObj = {
877
- type: 'placeHolderColumn',
878
- order: subRowIndex,
879
- noBoarder: true,
880
- needSplit: true,
881
- colSpan: preColSpan,
882
- dontShowRow: true,
883
- }
884
- subRowIndex++
885
- waitForAddArr.push(nullObj)
886
- total = 0
887
- count = 0
888
- titleCellFlag = false
889
- firstSubLine = false
890
- } else if (
891
- total !== count + cell.rowSpan &&
892
- forEachCount === rowArr.length
893
- ) {
894
- // 如果没有遇到了下一个声明行,但已经是当前行最后一个数据,也证明rowspan少了一行,需要补充一个占位格
895
- const nullObj = {
896
- type: 'placeHolderColumn',
897
- order: subRowIndex,
898
- noBoarder: true,
899
- needSplit: true,
900
- colSpan: preColSpan,
901
- dontShowRow: true,
902
- }
903
- subRowIndex++
904
- waitForAddArr.push(nullObj)
905
- total = 0
906
- count = 0
907
- titleCellFlag = false
908
- firstSubLine = false
909
- }
910
-
911
- // 判断是否为声明行
912
- if (cell.text && total === 0) {
913
- // 将声明行声明的rowspan作为总数,判断下方rowspan相加是否等于声明行声明的数量
914
- total = cell.rowSpan
915
- titleCellFlag = false
916
- firstSubLine = true
917
- subRowIndex = 1
918
- cell.show = true
919
- cell.showRowSpan = total
920
- } else if (cell.rowSpan > 0 && !titleCellFlag && firstSubLine) {
921
- // 判断是否为声明行后首行,因为首行不需要移动
922
- count += cell.rowSpan
923
- firstSubLine = false
924
- cell.noBoarder = true
925
- cell.show = true
926
- cell.showRowSpan = total
927
- } else if (cell.rowSpan > 0 && !titleCellFlag && !firstSubLine) {
928
- // 既非声明行,也非首行,需要移动
929
- count += cell.rowSpan
930
- // cell.type = 'notShow'
931
- cell.needSplit = true
932
- cell.order = subRowIndex
933
- cell.dontShowRow = true
934
- subRowIndex++
935
-
936
- // 如果之前添加过空行补充位置,刚好最后一位还有内容,将其互换
937
- if (
938
- forEachCount === rowArr.length &&
939
- !waitForAddArr[waitForAddArr.length - 1].dataIndex
940
- ) {
941
- waitForAddArr[waitForAddArr.length - 1].order += 1
942
- cell.order -= 1
943
- waitForAddArr.push(cell)
944
- } else {
945
- waitForAddArr.push(cell)
946
- }
947
- }
948
-
949
- // 如果count和total相等了,证明已经处理完成。将计数器还原
950
- if (count === total) {
951
- total = 0
952
- count = 0
953
- titleCellFlag = false
954
- firstSubLine = false
955
- }
956
- // 保存上一个的colspan保持生成的格子与原格式一致
957
- preColSpan = cell.colSpan
958
- })
959
-
960
- // 将所有不需要移动的放入original
961
- rowArr.forEach((cell) => {
962
- if (cell.needSplit !== true) {
963
- original.push(cell)
964
- }
965
- })
966
-
967
- // 增加新的数组
968
- waitForAddArr.forEach((cell) => {
969
- const target = cell.order
970
- // if (cell.type === 'notShow') {
971
- // cell.type = 'inputs'
972
- // }
973
- cell.noBoarder = true
974
- if (addArr[target] === undefined) {
975
- const temp = []
976
- temp.push(cell)
977
- addArr[target] = temp
978
- } else if (addArr[target].length > 0) {
979
- addArr[target].push(cell)
980
- }
981
- })
982
-
983
- // 如果没有新增,将单元格边框设置为显示
984
- if (addArr.length < 1) {
985
- original.forEach((cell) => {
986
- if (cell.type === 'input' || cell.type === 'inputs') {
987
- cell.noBoarder = false
988
- }
989
- })
990
- }
991
-
992
- return {
993
- old: original,
994
- add: addArr,
995
- }
996
- },
997
- // 扫描配置,如果有插槽则拼接插槽
998
- scanConfigSlot (config) {
999
- const columnsArr = config.columns
1000
- for (let i = 0; i < columnsArr.length; i++) {
1001
- for (let j = 0; j < columnsArr[i].length; j++) {
1002
- // 如果发现type为slot,开始匹配对应的slot配置文件
1003
- if (columnsArr[i][j].type === 'slot') {
1004
- const targetName = columnsArr[i][j].slotConfig
1005
- // 找不到目标插槽配置
1006
- if (
1007
- !this.configFromWeb[targetName] ||
1008
- !this.configFromWeb[targetName].columns
1009
- ) {
1010
- this.$logger('report.slotConfig').error('无法找到目标插槽的配置!')
1011
- return
1012
- }
1013
-
1014
- // 替换columns,合并data
1015
- config.columns[i] = []
1016
- const before = config.columns.slice(0, i)
1017
- let after = config.columns.slice(i + 1, config.columns.length)
1018
-
1019
- const addArr = []
1020
- for (
1021
- let k = 0;
1022
- k < this.configFromWeb[targetName].columns.length;
1023
- k++
1024
- ) {
1025
- const temp = []
1026
- this.configFromWeb[targetName].columns[k].forEach((cell) => {
1027
- temp.push(cell)
1028
- })
1029
- addArr.push(temp)
1030
- }
1031
-
1032
- const newArr = []
1033
- // 拼接之前的数组
1034
- if (before.length > 0) {
1035
- if (before.length >= 1) {
1036
- before.forEach((item) => {
1037
- newArr.push(item)
1038
- })
1039
- } else {
1040
- newArr.push(before)
1041
- }
1042
- }
1043
-
1044
- addArr.forEach((arr) => {
1045
- newArr.push(arr)
1046
- })
1047
-
1048
- // 拼接之后的数组
1049
- if (after.length === 1) {
1050
- if (after[0].type === 'slot' || after[0][0].type === 'slot') {
1051
- after = []
1052
- }
1053
- }
1054
- if (after.length > 0) {
1055
- if (after.length >= 1) {
1056
- after.forEach((item) => {
1057
- newArr.push(item)
1058
- })
1059
- } else {
1060
- newArr.push(after)
1061
- }
1062
- }
1063
-
1064
- config.columns = newArr
1065
- if (this.configFromWeb[targetName].slotsDeclare) {
1066
- config.slotsDeclare = this.configFromWeb[targetName].slotsDeclare
1067
- } else {
1068
- config.slotsDeclare = []
1069
- }
1070
-
1071
- if (
1072
- config.data.images &&
1073
- this.configFromWeb[targetName].data.images
1074
- ) {
1075
- config.data.images = {
1076
- ...config.data.images,
1077
- ...this.configFromWeb[targetName].data.images,
1078
- }
1079
- delete this.configFromWeb[targetName].data.images
1080
- }
1081
- config.data = {
1082
- ...config.data,
1083
- ...this.configFromWeb[targetName].data,
1084
- }
1085
- }
1086
- }
1087
- }
1088
- this.config = config
1089
- },
1090
- // 扫描所有插槽名
1091
- scanConfigName (config, resut) {
1092
- if (config.slotsDeclare) {
1093
- config.slotsDeclare.forEach((name) => {
1094
- resut.push(name)
1095
- })
1096
- }
1097
- },
1098
- // 获取插槽
1099
- getConfigAndJoin (config, outerLock) {
1100
- // 检查主配置插槽声明是否合法
1101
- const check = this.checkSlotDefine(config)
1102
- const waitForDownloadSlotName = []
1103
- if (check) {
1104
- // 扫描主配置中声明的插槽名
1105
- this.scanConfigName(config, waitForDownloadSlotName)
1106
-
1107
- const total = waitForDownloadSlotName.length
1108
- let count = 0
1109
-
1110
- // 挨个获取插槽
1111
- waitForDownloadSlotName.forEach((configName) => {
1112
- getConfigByName(
1113
- configName,
1114
- this.serverName,
1115
- (res) => {
1116
- this.configFromWeb[configName] = res
1117
- count++
1118
- },
1119
- this.env === 'dev'
1120
- )
1121
- })
1122
-
1123
- // 使用定时器循环判断锁状态,用于多个插槽,要等待统一获取完成之后,再进行下一步初始化
1124
- const timer = setInterval(() => {
1125
- if (count >= total) {
1126
- clearInterval(timer)
1127
- this.scanConfigSlot(config)
1128
- if (config.slotsDeclare.length > 0) {
1129
- const lock = { status: true }
1130
- this.getConfigAndJoin(config, lock)
1131
- const innerTimer = setInterval(() => {
1132
- if (!lock.status) {
1133
- clearInterval(innerTimer)
1134
- outerLock.status = false
1135
- }
1136
- }, 100)
1137
- } else {
1138
- outerLock.status = false
1139
- }
1140
- }
1141
- }, 100)
1142
- } else {
1143
- this.$logger('report.slotConfig').error('插槽配置有误!')
1144
- outerLock.status = false
1145
- }
1146
- },
1147
- // 获取配置之后的初始化
1148
- configInit () {
1149
- this.configEnded = false
1150
- this.allSlotsConfigEnded = false
1151
- this._configEndPromise = new Promise(resolve => { this._configEndResolve = resolve })
1152
- // 将初始化好的配置拷贝一份留存
1153
- this.originalConfig = Object.assign({}, this.config)
1154
- if (!this.dontFormat) {
1155
- // 扫描配置文件中有没有rowSpan,进行格式化调整
1156
- this.formatConfigRow(this.config)
1157
- }
1158
- this.activeConfig = this.config
1159
- this.showSkeleton = false
1160
- // 判断是否有动态Index
1161
- this.activeConfig.columns.forEach((row) => {
1162
- row.forEach((cell) => {
1163
- if (cell.dynamicDataIndex === true) {
1164
- // 如果有动态index,取其函数,运行函数得到真实index保存
1165
- // eslint-disable-next-line no-eval
1166
- const func = eval(
1167
- '(' + cell.customFunctionForDynamicDataIndex + ')'
1168
- )
1169
- cell.dataIndex = func(this.config)
1170
- }
1171
- // 处理 自定义函数的旧逻辑
1172
- if (
1173
- ['action', 'click'].includes(cell.eventType) &&
1174
- cell.customFunction &&
1175
- !cell.events
1176
- ) {
1177
- cell.events = []
1178
- cell.events.push({
1179
- type: cell.eventType,
1180
- customFunction: cell.customFunction,
1181
- })
1182
- }
1183
- })
1184
- })
1185
- // 将数据复制到临时数据中,带有@@@的数据,我们将其整体作为一个key保存,当编辑完成后,再将其解析,回填到需要的数据中
1186
- this.activeConfig.tempData = {}
1187
- // 是否有@@@深层引用
1188
- this.activeConfig.columns.forEach((row) => {
1189
- row.forEach((cell) => {
1190
- // 将@@@解析
1191
- if (
1192
- cell.dataIndex !== undefined &&
1193
- cell.dataIndex.indexOf('@@@') !== -1
1194
- ) {
1195
- this.activeConfig.tempData[cell.dataIndex] = this.getDeepObject(
1196
- this.activeConfig.data,
1197
- cell.dataIndex
1198
- )
1199
- }
1200
- })
1201
- })
1202
-
1203
- // 对配置进行转换
1204
- this.originalConfig.columns = this.transformArray(
1205
- JSON.parse(JSON.stringify(this.config.columns))
1206
- )
1207
-
1208
- this.$nextTick(() => {
1209
- this.scanFinish = true
1210
- // 无 slot 时不会触发 slotRendered,在此视为配置彻底结束
1211
- if (!this.config?.slotsDeclare?.length) {
1212
- this._markConfigEnded()
1213
- }
1214
- })
1215
- },
1216
- },
1217
- beforeMount () {
1218
- // 如果只是展示
1219
- if (this.displayOnly) {
1220
- this.onlyDisplay = true
1221
- this.type = 'display'
1222
- }
1223
- // 如果有本地配置,优先使用本地配置
1224
- if (this.localConfig) {
1225
- // 如果配置是普通渲染器
1226
- this.config = this.localConfig
1227
- if (this.configData !== undefined) {
1228
- this.config.data = this.configData
1229
- }
1230
- if (this.config.data.images === undefined) {
1231
- this.config.data.images = {}
1232
- }
1233
- this.configInit()
1234
- this.$logger('report.beforeMount').log('本地 config 就绪', { configName: this.configName })
1235
- } else {
1236
- // 如果本地配置没有值,则从琉璃中获取
1237
- getConfigByName(
1238
- this.configName,
1239
- this.serverName,
1240
- (res) => {
1241
- this.config = JSON.parse(JSON.stringify(res))
1242
- if (this.configData !== undefined) {
1243
- this.config.data = this.configData
1244
- }
1245
- if (this.config.data.images === undefined) {
1246
- this.config.data.images = {}
1247
- }
1248
- this.configInit()
1249
- this.$logger('report.beforeMount').log('远程 config 加载完成', { configName: this.configName })
1250
- },
1251
- this.env === 'dev'
1252
- )
1253
- }
1254
- },
1255
- computed: {
1256
- ...mapState('account', { currUser: 'user' }),
1257
- widget () {
1258
- return this.isWidget // 返回isWidget的值
1259
- },
1260
- },
1261
- mounted () {
1262
- // 如果外界传来了registerMap,我们将本VM对象注册到map中
1263
- if (this.registerMap !== undefined) {
1264
- this.registerMap.push(this)
1265
- }
1266
- // TAB 页签的 slotRef 由 XTab 在 onComponentMounted 中统一注册(与 Cover 普通子组件一致),此处不再自我注册
1267
- // 将原始数据备份保存
1268
- if (this.configData) {
1269
- this.dataCache = JSON.parse(JSON.stringify(this.configData))
1270
- } else {
1271
- if (this.config?.data) {
1272
- this.dataCache = JSON.parse(JSON.stringify(this.config.data))
1273
- }
1274
- }
1275
- },
1276
- }
1277
- </script>
1278
-
1279
- <style lang="less" scoped>
1280
- .tools {
1281
- text-align: center;
1282
- cursor: pointer;
1283
-
1284
- .toolsItem {
1285
- display: inline-block;
1286
- }
1287
- }
1288
- </style>
1
+ <template>
2
+ <div>
3
+ <!-- 骨架屏 -->
4
+ <a-card v-if="showSkeleton">
5
+ <a-skeleton active />
6
+ </a-card>
7
+ <!-- 主体表格 -->
8
+ <XReportDesign
9
+ @selectRow="selectRow"
10
+ @slotRendered="slotRendered"
11
+ @allSlotsConfigEnded="onAllSlotsConfigEnded"
12
+ v-if="scanFinish"
13
+ :display-only="displayOnly"
14
+ :config="originalConfig"
15
+ :slot-config-name="undefined"
16
+ :for-display="true"
17
+ ref="XReportDesign"
18
+ :server-name="serverName"
19
+ :env="env"
20
+ :show-title="showTitle"
21
+ @listClick="listClick"
22
+ >
23
+ </XReportDesign>
24
+ <a-row
25
+ type="flex"
26
+ justify="end"
27
+ v-if="showSaveButton"
28
+ >
29
+ <a-space>
30
+ <a-button @click="saveConfig">
31
+ 提交
32
+ </a-button>
33
+ <a-button @click="cancelConfig">
34
+ 取消
35
+ </a-button>
36
+ </a-space>
37
+ </a-row>
38
+ <!-- 弹出框 -->
39
+ <x-add-report
40
+ :env="env"
41
+ ref="xAddReport"
42
+ />
43
+ <!-- 弹出框 -->
44
+ <x-report-drawer
45
+ :env="env"
46
+ ref="xReportDrawer"
47
+ />
48
+ </div>
49
+ </template>
50
+ <script>
51
+ /* eslint-disable */
52
+ // 转PDF用
53
+ import { mapState } from 'vuex'
54
+ import { getConfigByName, runLogic } from '@vue2-client/services/api/common'
55
+ import XReportDesign from './XReportDesign.vue'
56
+ import { executeStrFunctionByContext } from '@vue2-client/utils/runEvalFunction'
57
+ import { shortcutManager } from '@vue2-client/base-client/utils/shortcutManager'
58
+
59
+ // import XAddReport from '@vue2-client/base-client/components/common/XAddReport'
60
+
61
+ export default {
62
+ name: 'XReport',
63
+ props: {
64
+ files: {
65
+ type: Array,
66
+ default: () => {
67
+ return []
68
+ },
69
+ },
70
+ // 控制用户权限,user和admin
71
+ authority: {
72
+ type: String,
73
+ default: 'user',
74
+ },
75
+ // 是否为编辑模式
76
+ editMode: {
77
+ type: Boolean,
78
+ default: true,
79
+ },
80
+ // 配置名
81
+ configName: {
82
+ type: String,
83
+ required: true,
84
+ },
85
+ // 插槽名
86
+ activatedSlotName: {
87
+ type: String,
88
+ default: undefined,
89
+ },
90
+ // 本地配置,调试用
91
+ localConfig: {
92
+ type: Object,
93
+ default: undefined,
94
+ },
95
+ // 兼容老版本配置
96
+ dontFormat: {
97
+ type: Boolean,
98
+ default: true,
99
+ },
100
+ showImgInCell: {
101
+ type: Boolean,
102
+ default: false,
103
+ },
104
+ // 数据
105
+ configData: {
106
+ type: Object,
107
+ default: undefined,
108
+ },
109
+ // 命名空间
110
+ serverName: {
111
+ type: String,
112
+ default: process.env.VUE_APP_SYSTEM_NAME,
113
+ },
114
+ // 环境
115
+ env: {
116
+ type: String,
117
+ default: 'prod',
118
+ },
119
+ // 只做展示
120
+ displayOnly: {
121
+ type: Boolean,
122
+ default: true,
123
+ },
124
+ // 表格没有边距
125
+ noPadding: {
126
+ type: Boolean,
127
+ default: true,
128
+ },
129
+ // 表格没有上边框,与noPadding搭配可以实现连续表格
130
+ noTopBorder: {
131
+ type: Boolean,
132
+ default: false,
133
+ },
134
+ // 是否展示标题
135
+ showTitle: {
136
+ type: Boolean,
137
+ default: true,
138
+ },
139
+ // 是否展示保存按钮
140
+ showSaveButton: {
141
+ type: Boolean,
142
+ default: false,
143
+ },
144
+ // 是否将组件注册到外层提供的容器中,方便外侧统一保存
145
+ registerMap: {
146
+ type: Array,
147
+ default: undefined,
148
+ },
149
+ // TAB 页签时由 XTab 传入,仅接收不参与逻辑(注册由 XTab onComponentMounted 完成)
150
+ slotRef: {
151
+ type: String,
152
+ default: undefined,
153
+ },
154
+ // 是否小插件模式,小插件模式不会有各种边境
155
+ isWidget: {
156
+ type: Boolean,
157
+ default: false,
158
+ },
159
+ // 图片是否使用OSS来保存
160
+ useOssForImg: {
161
+ type: Boolean,
162
+ default: true,
163
+ },
164
+ // 图片上传后添加前缀
165
+ imgPrefix: {
166
+ type: String,
167
+ default: undefined,
168
+ },
169
+ },
170
+ components: {
171
+ XAddReport: () =>
172
+ import('@vue2-client/base-client/components/common/XAddReport'),
173
+ XReportDrawer: () =>
174
+ import('@vue2-client/base-client/components/common/XReportDrawer'),
175
+ XReportDesign,
176
+ },
177
+ data () {
178
+ return {
179
+ // 控制骨架屏显隐
180
+ showSkeleton: true,
181
+ // 配置
182
+ config: undefined,
183
+ // 当前显示模式,编辑模式,预览模式
184
+ type: 'design',
185
+ // 仅供展示,不可编辑
186
+ onlyDisplay: false,
187
+ // 每行最大列数,非必要请勿更改,现在的设计器完全是基于每行12列来设计的
188
+ maxColSpan: 12,
189
+ // 定义是否完成配置的扫描,未完成不要渲染子组件
190
+ scanFinish: false,
191
+ // 当前激活的配置文件
192
+ activeConfig: null,
193
+ // 原始配置文件
194
+ // 用于展示。某些情况下“设计页”中的内容仅为“预览页”表格其中的一部分
195
+ originalConfig: null,
196
+ // 扫描到的配置
197
+ configFromWeb: {},
198
+ // 用于获取配置的锁
199
+ timer: undefined,
200
+ // 是否包含图片
201
+ hasImages: false,
202
+ // 图片列表
203
+ imageList: [],
204
+ // 保存最原始的数据,用于判断哪些数据被更改了
205
+ dataCache: undefined,
206
+ // 判断哪些数据被更改了,存储对应的key
207
+ diff: [],
208
+ globalData: {},
209
+ // setData 在 config 未就绪时传入的 data,配置彻底结束后再执行
210
+ // eslint-disable-next-line
211
+ _pendingSetData: undefined,
212
+ // 配置是否彻底结束:本报表配置就绪且所有 slot 子组件配置也结束(由 allSlotsConfigEnded 事件获知)
213
+ configEnded: false,
214
+ // 是否已收到 Design 的 allSlotsConfigEnded(所有 slot 子组件配置结束)
215
+ allSlotsConfigEnded: false,
216
+ // 本报表注册到全局组件注册表的名称(仅当配置了 globalRegistryName 时才有),destroy 时统一注销
217
+ // eslint-disable-next-line
218
+ _globalRegistrySlotNames: [],
219
+ // 等待配置结束的 Promise,供 waitConfigEnd() 使用
220
+ _configEndPromise: null,
221
+ _configEndResolve: null,
222
+ }
223
+ },
224
+ inject: {
225
+ // 从祖先获取 getGridScopePath(可能是外层 XReport 或 XTab 提供的)
226
+ upstreamGetGridScopePath: {
227
+ from: 'getGridScopePath',
228
+ default: null
229
+ }
230
+ },
231
+ beforeDestroy () {
232
+ clearInterval(this.timer)
233
+ if (this.$componentRegistry && this._globalRegistrySlotNames.length) {
234
+ this._globalRegistrySlotNames.forEach((name) => {
235
+ this.$componentRegistry.unregister(name)
236
+ })
237
+ this._globalRegistrySlotNames = []
238
+ }
239
+ },
240
+ watch: {
241
+ // 如果配置名更改了,重新获取配置
242
+ configName (val) {
243
+ if (val) {
244
+ // 先清空配置,避免异步期间使用旧配置注册快捷键
245
+ this.originalConfig = null
246
+ this.scanFinish = false
247
+ getConfigByName(
248
+ this.configName,
249
+ undefined,
250
+ (res) => {
251
+ this.config = res
252
+ this.configInit()
253
+ this.$logger('report.configLoad').log('configName 加载完成', { configName: this.configName })
254
+ },
255
+ this.env === 'dev'
256
+ )
257
+ }
258
+ },
259
+ // 如果本地配置更改了,重新初始化
260
+ localConfig: {
261
+ deep: true,
262
+ immediate: true,
263
+ handler (val) {
264
+ if (val) {
265
+ this.config = val
266
+ this.configInit()
267
+ this.$logger('report.configLoad').log('localConfig 变更完成', { configName: this.configName })
268
+ }
269
+ },
270
+ },
271
+ },
272
+ provide () {
273
+ const report = this
274
+ return {
275
+ runLogic: runLogic,
276
+ openDialog: report.openDialog.bind(report),
277
+ openDrawer: report.openDrawer.bind(report),
278
+ registerComponent: report.registerComponent.bind(report),
279
+ getComponentByName: report.getComponentByName.bind(report),
280
+ getParentComponentByName: report.getComponentByName.bind(report),
281
+ getConfigByName: getConfigByName,
282
+ isWidget: report.widget,
283
+ currUser: report.currUser,
284
+ getGlobalData: report.getGlobalData.bind(report),
285
+ setGlobalData: report.setGlobalData.bind(report),
286
+ findComponentByName: report.findComponentByName.bind(report),
287
+ closeAddReport: report.closeAddReport.bind(report),
288
+ setData: report.setData.bind(report),
289
+ getRootReport: () => report,
290
+ getGridScopePath: () => {
291
+ // 1. 优先从注入的上游函数获取父路径
292
+ let parent = ''
293
+ if (typeof report.upstreamGetGridScopePath === 'function') {
294
+ parent = report.upstreamGetGridScopePath()
295
+ }
296
+ // 2. 如果没有注入(根实例),回退到 prop(兼容旧写法)
297
+ if (!parent && report.parentScopePath) {
298
+ parent = report.parentScopePath
299
+ }
300
+ // 3. 拼上自己的 configName
301
+ const current = report.configName
302
+ return parent ? `${parent}/${current}` : current
303
+ }
304
+ }
305
+ },
306
+ methods: {
307
+ findComponentByName (instance, componentName, maxDepth, findType) {
308
+ // 基础判断:如果实例不存在或者已经超过最大深度,返回null
309
+ if (!instance || maxDepth <= 0) {
310
+ return null
311
+ }
312
+
313
+ // 检查当前实例是否有目标组件
314
+ try {
315
+ if (instance.getComponentByName && instance.getComponentByName(componentName)) {
316
+ return instance.getComponentByName(componentName)
317
+ }
318
+ } catch (e) {
319
+ this.$logger('report.findComponent').warn('查找组件时发生错误:', e)
320
+ }
321
+
322
+ // 递归查找子组件
323
+ if (findType === 'parent') {
324
+ // 向上查找父组件
325
+ if (instance.$parent) {
326
+ return this.findComponentByName(instance.$parent, componentName, maxDepth - 1, findType)
327
+ }
328
+ } else {
329
+ if (instance.$children && instance.$children.length) {
330
+ for (const child of instance.$children) {
331
+ const result = this.findComponentByName(child, componentName, maxDepth - 1, findType)
332
+ if (result) {
333
+ return result
334
+ }
335
+ }
336
+ }
337
+ }
338
+ return null
339
+ },
340
+ listClick (data) {
341
+ this.$emit('listClick', data)
342
+ },
343
+ closeAddReport () {
344
+ if (this.$refs.xAddReport) {
345
+ this.$refs.xAddReport.close()
346
+ }
347
+ let parent = this.$parent
348
+ while (parent) {
349
+ if (parent.$refs && parent.$refs.xAddReport) {
350
+ const addReportRefs = parent.$refs.xAddReport
351
+ if (Array.isArray(addReportRefs)) {
352
+ addReportRefs.forEach(ref => {
353
+ if (ref && typeof ref.close === 'function') {
354
+ ref.close()
355
+ }
356
+ })
357
+ } else if (typeof addReportRefs.close === 'function') {
358
+ addReportRefs.close()
359
+ }
360
+ }
361
+ parent = parent.$parent
362
+ }
363
+ },
364
+ slotRendered () {
365
+ this.$logger('report.slotRendered').log('slotRendered 进入', { configName: this.configName, hasPendingSetData: this._pendingSetData !== undefined })
366
+ if (this.config?.mountedFunction) {
367
+ let func = this.config.mountedFunction
368
+ if (func && func.startsWith('function')) {
369
+ func = func.replace('function', 'async function')
370
+ executeStrFunctionByContext(this, func, [this])
371
+ }
372
+ }
373
+ },
374
+ onAllSlotsConfigEnded () {
375
+ this.allSlotsConfigEnded = true
376
+ this._markConfigEnded()
377
+ },
378
+ // 把组件注册到refs中,方便调用;若传入 options.globalRegistryName 则同时注册到全局组件注册表(向后兼容:无该字段则不注册)
379
+ registerComponent (componentName, component, options = {}) {
380
+ this.$refs[componentName] = component
381
+ const globalName = options.globalRegistryName
382
+ if (globalName && this.$componentRegistry) {
383
+ try {
384
+ this.$componentRegistry.register(globalName, component)
385
+ this._globalRegistrySlotNames.push(globalName)
386
+ } catch (e) {
387
+ this.$logger('report.registerComponent').warn('全局组件注册表注册失败:', e?.message || e)
388
+ }
389
+ }
390
+ // 如果配置了 mountedLogicName,则运行该逻辑
391
+ if (options.mountedLogicName) {
392
+ this.runMountedLogic(component, options)
393
+ }
394
+ },
395
+
396
+ // 运行组件挂载后的逻辑
397
+ async runMountedLogic (component, options = {}) {
398
+ const { mountedLogicName, mountedLogicMode, mountedLogicParam } = options
399
+ if (!mountedLogicName) return
400
+ try {
401
+ const param = {
402
+ proxy: component,
403
+ ...(mountedLogicParam || {})
404
+ }
405
+ await this.$runFrontLogic(mountedLogicName, param, mountedLogicMode)
406
+ } catch (e) {
407
+ const cause = e?.cause || e
408
+ if (cause?.name === 'AbortError' || cause?.name === 'CanceledError' || cause?.code === 'ERR_CANCELED') {
409
+ return
410
+ }
411
+ this.$logger('report.runMountedLogic').warn('mountedLogicName 执行失败:', e?.message || e)
412
+ }
413
+ },
414
+
415
+ // 把设计的table布局转换成可显示的珊格布局
416
+ transformArray (inputList) {
417
+ let operationIndex = 0
418
+ let operationList = []
419
+ const outputList = []
420
+ for (const lst of inputList) {
421
+ // 如果列表为空或只有一个元素,则所有元素相等。比较列表中每个元素是否与第一个元素相等
422
+ if (
423
+ lst.length >= 1 &&
424
+ !lst.every(
425
+ (x) =>
426
+ Array.isArray(x) ||
427
+ Array.isArray(lst[0]) ||
428
+ x.rowSpan === lst[0].rowSpan
429
+ )
430
+ ) {
431
+ operationList = lst
432
+ break // 使用 break 退出整个循环
433
+ } else {
434
+ // 被操作的行
435
+ operationIndex += 1
436
+ }
437
+ }
438
+
439
+ let maxMergeRow = 0
440
+
441
+ // 没有需要合并的行,直接返回
442
+ if (operationList.length === 0) {
443
+ return inputList
444
+ } else {
445
+ // 当前行的最大值
446
+ const maxRow = Math.max(...operationList.map((item) => item.rowSpan))
447
+ let mergeIndexCol = 0
448
+ for (let index = 0; index < operationList.length; index++) {
449
+ const row = operationList[index]
450
+ let rowSpan = row.rowSpan
451
+ // 需要合并的行
452
+ let mergeIndexRow = operationIndex + 1
453
+ // 存放合并后的行
454
+ const rows = []
455
+ // 添加当前行
456
+ if (rowSpan < maxRow && mergeIndexRow < inputList.length) {
457
+ rows.push([row])
458
+ }
459
+ // 当前需要被操作并且操作行没有超出列表的高度
460
+ while (rowSpan < maxRow && mergeIndexRow < inputList.length) {
461
+ rowSpan += inputList[mergeIndexRow][mergeIndexCol].rowSpan
462
+ // 放一行到合并结果
463
+ rows.push([inputList[mergeIndexRow][mergeIndexCol]])
464
+ if (mergeIndexRow > maxMergeRow) {
465
+ // 记录最多操作到了哪行
466
+ maxMergeRow = mergeIndexRow
467
+ }
468
+ mergeIndexRow++
469
+ }
470
+ // operation_list赋值, 没有变化的,不处理
471
+ if (rows.length !== 0) {
472
+ operationList[index] = rows
473
+ }
474
+ if (row.rowSpan !== maxRow) {
475
+ mergeIndexCol++ // 操作列转为下一列
476
+ }
477
+ }
478
+ }
479
+
480
+ // 组成outputlist, operation_list前部填入
481
+ let putindex = 0
482
+ while (operationIndex > 0) {
483
+ outputList.push(inputList[putindex])
484
+ putindex++
485
+ operationIndex -= 1
486
+ }
487
+
488
+ outputList.push(operationList)
489
+
490
+ // 组成outputlist, operation_list后部填入
491
+ while (maxMergeRow < inputList.length - 1) {
492
+ outputList.push(inputList[maxMergeRow + 1])
493
+ maxMergeRow += 1
494
+ }
495
+
496
+ return this.transformArray(outputList)
497
+ },
498
+ // 根据名字从注册到组件中获取组件
499
+ getComponentByName (componentName) {
500
+ return this.$refs[componentName]
501
+ },
502
+ getGlobalData () {
503
+ return this.globalData
504
+ },
505
+ setGlobalData (obj) {
506
+ this.globalData = obj
507
+ },
508
+ /**
509
+ * 配置彻底结束:本报表配置就绪且所有 slot 子组件配置也结束。打标、对外发 configEnd,再执行缓存的 setData。
510
+ * 由 onAllSlotsConfigEnded(有 slot 且收到 allSlotsConfigEnded)或 configInit 的 nextTick(无 slot)调用,仅执行一次。
511
+ */
512
+ _markConfigEnded () {
513
+ if (this.configEnded) return
514
+ const hasSlots = (this.config?.slotsDeclare?.length || 0) > 0
515
+ if (hasSlots && !this.allSlotsConfigEnded) return
516
+ this.configEnded = true
517
+ this.$logger('report.configEnd').log('配置彻底结束,发送 configEnd', { configName: this.configName })
518
+ this.$emit('configEnd')
519
+ if (this._configEndResolve) {
520
+ this._configEndResolve()
521
+ this._configEndResolve = null
522
+ }
523
+ this.$nextTick(() => this._flushPendingSetData())
524
+ },
525
+ /**
526
+ * 等待配置结束的异步方法。配置已结束则立即 resolve,否则在 configEnd 时 resolve。
527
+ * @returns {Promise<void>}
528
+ */
529
+ waitConfigEnd () {
530
+ if (this.configEnded) return Promise.resolve()
531
+ return this._configEndPromise || Promise.resolve()
532
+ },
533
+ /**
534
+ * 内部:直接执行 dataProcessScript。供 setData(configEnded 时)和 _flushPendingSetData 调用。
535
+ */
536
+ async _runDataProcessScript (data) {
537
+ const script = this.config?.dataProcessScript
538
+ if (!script || typeof script !== 'string') return
539
+ try {
540
+ const result = executeStrFunctionByContext(this, script, [data])
541
+ if (result instanceof Promise) await result
542
+ } catch (e) {
543
+ this.$logger('report.dataProcessScript').error('dataProcessScript 执行异常:', e)
544
+ }
545
+ },
546
+ /**
547
+ * 对外数据设置:仅当配置中存在 dataProcessScript 时执行该脚本,传入 data;不修改组件自身任何数据。
548
+ * 无脚本时缓存 data 并 return。有脚本但配置未彻底结束(configEnded 为 false)时也缓存,等 configEnd 后再执行。
549
+ * 只有配置彻底结束后才执行 dataProcessScript。脚本内 this 为当前 XReport 实例。
550
+ * @param {*} data 外部传入的数据,供脚本使用
551
+ * @returns {Promise<void>}
552
+ */
553
+ async setData (data) {
554
+ this.$logger('report.setData').log('setData 进入', { configName: this.configName, configEnded: this.configEnded, hasData: data !== undefined, dataKeys: data && typeof data === 'object' ? Object.keys(data) : [] })
555
+ const script = this.config?.dataProcessScript
556
+ if (!script || typeof script !== 'string') {
557
+ this._pendingSetData = data
558
+ this.$logger('report.setData').warn('分支: 无脚本,已缓存 data', { hasConfig: !!this.config, hasScript: !!(this.config && this.config.dataProcessScript), configName: this.configName })
559
+ return
560
+ }
561
+ if (!this.configEnded) {
562
+ this._pendingSetData = data
563
+ this.$logger('report.setData').log('分支: 配置未结束,已缓存 data,等 configEnd 后执行', { configName: this.configName })
564
+ return
565
+ }
566
+ this._pendingSetData = undefined
567
+ this.$logger('report.setData').log('分支: 配置已结束,立即执行 dataProcessScript', { configName: this.configName })
568
+ await this._runDataProcessScript(data)
569
+ },
570
+ /**
571
+ * 执行缓存的 setData:取 _pendingSetData 并直接执行脚本,不经过 setData,避免 setData 再次判断 defer 导致脚本不执行。
572
+ */
573
+ _flushPendingSetData () {
574
+ const hasPending = this._pendingSetData !== undefined
575
+ this.$logger('report.flushPendingSetData').log('_flushPendingSetData 进入', { configName: this.configName, hasPending })
576
+ if (!hasPending) return
577
+ const d = this._pendingSetData
578
+ this._pendingSetData = undefined
579
+ this.$logger('report.flushPendingSetData').log('_flushPendingSetData 直接执行 dataProcessScript', { configName: this.configName })
580
+ this._runDataProcessScript(d)
581
+ },
582
+ /**
583
+ * @param configName 栅格配置名称
584
+ * @param selectedId 选中得id
585
+ * @param mixinData 需要混入得数据
586
+ * @param outEnv 其他传递给打开窗口的数据
587
+ * @param attr 传递给Modal弹框用的信息
588
+ * @param manageScope 管理快捷键作用域
589
+ */
590
+ openDialog(configName, selectedId, mixinData, outEnv = {}, attr = {}, showButtons = true, manageScope = false) {
591
+ if (manageScope) {
592
+ this.openDialogWithScope(configName, selectedId, mixinData, outEnv, attr, showButtons)
593
+ return
594
+ }
595
+
596
+ this.$refs.xAddReport.init({
597
+ configName: configName,
598
+ selectedId: selectedId,
599
+ mixinData: mixinData,
600
+ outEnv: outEnv,
601
+ attr,
602
+ showButtons: showButtons
603
+ })
604
+ },
605
+ // 打开弹窗(带快捷键作用域管理)
606
+ openDialogWithScope(configName, selectedId, mixinData, outEnv = {}, attr = {}, showButtons = true) {
607
+ const savedScopes = new Map()
608
+ if (shortcutManager.isEnabled) {
609
+ shortcutManager.scopeActive.forEach((active, scopeId) => {
610
+ savedScopes.set(scopeId, active)
611
+ shortcutManager.setScopeActive(scopeId, false)
612
+ })
613
+ }
614
+
615
+ const xAddReport = this.$refs.xAddReport
616
+ if (xAddReport && !xAddReport._shortcutPatched) {
617
+ xAddReport._shortcutPatched = true
618
+ const originalClose = xAddReport.close.bind(xAddReport)
619
+ xAddReport.close = () => {
620
+ if (shortcutManager.isEnabled && savedScopes.size > 0) {
621
+ savedScopes.forEach((wasActive, scopeId) => {
622
+ if (wasActive) shortcutManager.setScopeActive(scopeId, true)
623
+ })
624
+ }
625
+ originalClose()
626
+ }
627
+ }
628
+
629
+ xAddReport.init({ configName, selectedId, mixinData, outEnv, attr, showButtons })
630
+ },
631
+
632
+ openDrawer (configName, selectedId, mixinData, outEnv = {}, attr = {}) {
633
+ this.$refs.xReportDrawer.init({
634
+ configName,
635
+ selectedId,
636
+ mixinData,
637
+ outEnv,
638
+ attr,
639
+ })
640
+ },
641
+ // 向外暴露图片修改后的数据,某些外部需要自己管理图片的保存与修改
642
+ updateImg (data) {
643
+ this.$emit('updateImg', data)
644
+ },
645
+ // 导出数据,某些外部需要统一控制数据的变动
646
+ exportData () {
647
+ // 获取当前修改后的数据
648
+ let tempData
649
+ if (this.activeConfig === undefined || this.activeConfig === null) {
650
+ tempData = this.originalConfig.data
651
+ } else {
652
+ const tempDataKeys = Object.keys(this.activeConfig.tempData)
653
+ tempDataKeys.forEach((key) => {
654
+ this.changeDeepObject(
655
+ this.activeConfig.data,
656
+ key,
657
+ this.activeConfig.tempData[key]
658
+ )
659
+ })
660
+ tempData = this.activeConfig.data
661
+ }
662
+ // 对比数据的差异
663
+ this.diff = []
664
+ this.compareProps(tempData, this.dataCache)
665
+ this.diff.forEach((eachDiff) => {
666
+ const arr = eachDiff.split('.')
667
+ let targetData = tempData[arr[0]]
668
+ if (arr.length !== 1) {
669
+ for (let i = 1; i < arr.length - 1; i++) {
670
+ const path = arr[i]
671
+ targetData = targetData[path]
672
+ }
673
+ }
674
+ // 将修改的数据,添加update = true属性
675
+ targetData.update = true
676
+ })
677
+ return tempData
678
+ },
679
+ // 对比两个obj有哪里不同
680
+ compareProps (obj1, obj2, path = '') {
681
+ for (const key in obj1) {
682
+ // 如果一个是undefined
683
+ if (typeof obj2[key] === 'undefined') {
684
+ this.diff.push(path + key)
685
+ // 如果是数组长度不一样
686
+ } else if (Array.isArray(obj1) && Array.isArray(obj2)) {
687
+ if (obj1[key].length !== obj2[key].length) {
688
+ this.diff.push(path + key)
689
+ }
690
+ // 如果都是对象,并且存在同样的key,递归子key
691
+ } else if (
692
+ typeof obj1[key] === 'object' &&
693
+ typeof obj2[key] === 'object'
694
+ ) {
695
+ this.compareProps(obj1[key], obj2[key], path + key + '.')
696
+ // 如果不是obj,对比其数据
697
+ } else if (obj1[key] !== obj2[key]) {
698
+ this.diff.push(path + key)
699
+ }
700
+ }
701
+ },
702
+ selectRow (selectedRowKeys, selectedRows) {
703
+ this.table_selectedRowKeys = selectedRowKeys
704
+ this.table_selectedRows = selectedRows
705
+ this.$emit('selectRow', selectedRowKeys, selectedRows)
706
+ },
707
+ // 注册组件到$refs中;若传入 options.globalRegistryName 则同时注册到全局组件注册表(向后兼容:无该字段则不注册)
708
+ registerComponentToRefs (componentName, component, options = {}) {
709
+ this.$refs[componentName] = component
710
+ const globalName = options.globalRegistryName
711
+ if (globalName && this.$componentRegistry) {
712
+ try {
713
+ this.$componentRegistry.register(globalName, component)
714
+ this._globalRegistrySlotNames.push(globalName)
715
+ } catch (e) {
716
+ this.$logger('report.registerComponentToRefs').warn('全局组件注册表注册失败:', e?.message || e)
717
+ }
718
+ }
719
+ // 如果配置了 mountedLogicName,则运行该逻辑
720
+ if (options.mountedLogicName) {
721
+ this.runMountedLogic(component, options)
722
+ }
723
+ },
724
+
725
+ // 提交处理,调用配置中的函数
726
+ saveConfig () {
727
+ const funcStr = this.config.confirmFunction
728
+ executeStrFunctionByContext(this, funcStr, [this])
729
+ },
730
+
731
+ // 取消处理
732
+ cancelConfig () {
733
+ this.$emit('cancel')
734
+ },
735
+
736
+ // 通过@@@分割临时变量,找到对应的key,并修改它的值
737
+ changeDeepObject (obj, strPath, newVal) {
738
+ const arr = strPath.split('@@@')
739
+ if (obj[arr[0]] === undefined) {
740
+ obj = obj.images
741
+ }
742
+ if (arr.length === 1) {
743
+ obj[arr[0]] = newVal
744
+ } else {
745
+ let result = obj[arr[0]]
746
+ arr.shift()
747
+ while (arr.length > 1) {
748
+ result = result[arr[0]]
749
+ arr.shift()
750
+ }
751
+ if (result) {
752
+ result[arr[0]] = newVal
753
+ }
754
+ }
755
+ },
756
+ // 检查slot是否在配置文件中包含,如果没有包含,则视为非法获取
757
+ checkSlotDefine (config) {
758
+ const slotsDeclare = config.slotsDeclare
759
+ const total = slotsDeclare.length
760
+ let count = 0
761
+ slotsDeclare.forEach((declare) => {
762
+ config.columns.forEach((row) => {
763
+ row.forEach((cell) => {
764
+ if (cell.slotConfig === declare) {
765
+ count++
766
+ }
767
+ })
768
+ })
769
+ })
770
+
771
+ return count === total
772
+ },
773
+ // 用于分割配置中的colums,将需要处理的数组提取出来
774
+ formatConfigRow () {
775
+ for (let i = 0; i < this.config.columns.length; i++) {
776
+ // 对原始数组进行递归,依次将该位置拆分为三个部分,当前处理位置之前的,当前处理位置,当前处理位置之后的
777
+ const before = this.config.columns.slice(0, i)
778
+ const after = this.config.columns.slice(
779
+ i + 1,
780
+ this.config.columns.length
781
+ )
782
+
783
+ // 将当前处理的数组交给处理的方法
784
+ const x = this.checkRow(this.config.columns[i])
785
+
786
+ const newArr = []
787
+
788
+ // 拼接之前的数组
789
+ if (before.length > 0) {
790
+ if (before.length >= 1) {
791
+ before.forEach((item) => {
792
+ newArr.push(item)
793
+ })
794
+ } else {
795
+ newArr.push(before)
796
+ }
797
+ }
798
+
799
+ // 拼接不需要更改当前节点处理完成的数组
800
+ newArr.push(x.old)
801
+
802
+ // 如果处理了新加的数据,拼接
803
+ if (x.add.length > 0) {
804
+ for (let j = 0; j < x.add.length; j++) {
805
+ if (x.add[j]) {
806
+ newArr.push(x.add[j])
807
+ i++
808
+ }
809
+ }
810
+ }
811
+
812
+ // 拼接之后的数组
813
+ if (after.length > 0) {
814
+ if (after.length >= 1) {
815
+ after.forEach((item) => {
816
+ newArr.push(item)
817
+ })
818
+ } else {
819
+ newArr.push(after)
820
+ }
821
+ }
822
+
823
+ this.config.columns = newArr
824
+ }
825
+ },
826
+ // 路径中含有@@@的key,将其解析,并返回其数据
827
+ getDeepObject (obj, strPath) {
828
+ const arr = strPath.split('@@@')
829
+ let result = obj[arr[0]]
830
+ arr.shift()
831
+ try {
832
+ while (arr.length > 0) {
833
+ result = result[arr[0]]
834
+ arr.shift()
835
+ }
836
+ } catch (e) {
837
+ result = undefined
838
+ }
839
+ return result
840
+ },
841
+ // 处理colums数组,为声明了rowspan的单元格,自动匹配格式
842
+ checkRow (rowArr) {
843
+ // 不需要更改的数据
844
+ const original = []
845
+ // 需要更改新加的数据
846
+ const addArr = []
847
+ // 统计rowspan出现的总和
848
+ let count = 0
849
+ // 统计声明列需要的rowspan总数
850
+ let total = 0
851
+ // 是否为声明行
852
+ let titleCellFlag = false
853
+ // 是否为声明行后第一行
854
+ let firstSubLine = false
855
+ // 需要处理的行,新的index值
856
+ let subRowIndex = 0
857
+ // 新生成的行,临时存储
858
+ const waitForAddArr = []
859
+ // 用于记录声明行的colspan避免格式错误
860
+ let preColSpan = 0
861
+ // 用于统计循环次数,判断是否是最后一次
862
+ let forEachCount = 0
863
+
864
+ // 标记所有数据
865
+ rowArr.forEach((cell) => {
866
+ forEachCount++
867
+ // 如果该行没有rowspan则默认其为1,不要影响统计结果
868
+ if (!cell.rowSpan) {
869
+ cell.rowSpan = 0
870
+ }
871
+
872
+ if (cell.text && total !== 0) {
873
+ // 如果遇到了下一个声明行,证明rowspan少了一行,需要补充一个占位格
874
+ const nullObj = {
875
+ type: 'placeHolderColumn',
876
+ order: subRowIndex,
877
+ noBoarder: true,
878
+ needSplit: true,
879
+ colSpan: preColSpan,
880
+ dontShowRow: true,
881
+ }
882
+ subRowIndex++
883
+ waitForAddArr.push(nullObj)
884
+ total = 0
885
+ count = 0
886
+ titleCellFlag = false
887
+ firstSubLine = false
888
+ } else if (
889
+ total !== count + cell.rowSpan &&
890
+ forEachCount === rowArr.length
891
+ ) {
892
+ // 如果没有遇到了下一个声明行,但已经是当前行最后一个数据,也证明rowspan少了一行,需要补充一个占位格
893
+ const nullObj = {
894
+ type: 'placeHolderColumn',
895
+ order: subRowIndex,
896
+ noBoarder: true,
897
+ needSplit: true,
898
+ colSpan: preColSpan,
899
+ dontShowRow: true,
900
+ }
901
+ subRowIndex++
902
+ waitForAddArr.push(nullObj)
903
+ total = 0
904
+ count = 0
905
+ titleCellFlag = false
906
+ firstSubLine = false
907
+ }
908
+
909
+ // 判断是否为声明行
910
+ if (cell.text && total === 0) {
911
+ // 将声明行声明的rowspan作为总数,判断下方rowspan相加是否等于声明行声明的数量
912
+ total = cell.rowSpan
913
+ titleCellFlag = false
914
+ firstSubLine = true
915
+ subRowIndex = 1
916
+ cell.show = true
917
+ cell.showRowSpan = total
918
+ } else if (cell.rowSpan > 0 && !titleCellFlag && firstSubLine) {
919
+ // 判断是否为声明行后首行,因为首行不需要移动
920
+ count += cell.rowSpan
921
+ firstSubLine = false
922
+ cell.noBoarder = true
923
+ cell.show = true
924
+ cell.showRowSpan = total
925
+ } else if (cell.rowSpan > 0 && !titleCellFlag && !firstSubLine) {
926
+ // 既非声明行,也非首行,需要移动
927
+ count += cell.rowSpan
928
+ // cell.type = 'notShow'
929
+ cell.needSplit = true
930
+ cell.order = subRowIndex
931
+ cell.dontShowRow = true
932
+ subRowIndex++
933
+
934
+ // 如果之前添加过空行补充位置,刚好最后一位还有内容,将其互换
935
+ if (
936
+ forEachCount === rowArr.length &&
937
+ !waitForAddArr[waitForAddArr.length - 1].dataIndex
938
+ ) {
939
+ waitForAddArr[waitForAddArr.length - 1].order += 1
940
+ cell.order -= 1
941
+ waitForAddArr.push(cell)
942
+ } else {
943
+ waitForAddArr.push(cell)
944
+ }
945
+ }
946
+
947
+ // 如果count和total相等了,证明已经处理完成。将计数器还原
948
+ if (count === total) {
949
+ total = 0
950
+ count = 0
951
+ titleCellFlag = false
952
+ firstSubLine = false
953
+ }
954
+ // 保存上一个的colspan保持生成的格子与原格式一致
955
+ preColSpan = cell.colSpan
956
+ })
957
+
958
+ // 将所有不需要移动的放入original
959
+ rowArr.forEach((cell) => {
960
+ if (cell.needSplit !== true) {
961
+ original.push(cell)
962
+ }
963
+ })
964
+
965
+ // 增加新的数组
966
+ waitForAddArr.forEach((cell) => {
967
+ const target = cell.order
968
+ // if (cell.type === 'notShow') {
969
+ // cell.type = 'inputs'
970
+ // }
971
+ cell.noBoarder = true
972
+ if (addArr[target] === undefined) {
973
+ const temp = []
974
+ temp.push(cell)
975
+ addArr[target] = temp
976
+ } else if (addArr[target].length > 0) {
977
+ addArr[target].push(cell)
978
+ }
979
+ })
980
+
981
+ // 如果没有新增,将单元格边框设置为显示
982
+ if (addArr.length < 1) {
983
+ original.forEach((cell) => {
984
+ if (cell.type === 'input' || cell.type === 'inputs') {
985
+ cell.noBoarder = false
986
+ }
987
+ })
988
+ }
989
+
990
+ return {
991
+ old: original,
992
+ add: addArr,
993
+ }
994
+ },
995
+ // 扫描配置,如果有插槽则拼接插槽
996
+ scanConfigSlot (config) {
997
+ const columnsArr = config.columns
998
+ for (let i = 0; i < columnsArr.length; i++) {
999
+ for (let j = 0; j < columnsArr[i].length; j++) {
1000
+ // 如果发现type为slot,开始匹配对应的slot配置文件
1001
+ if (columnsArr[i][j].type === 'slot') {
1002
+ const targetName = columnsArr[i][j].slotConfig
1003
+ // 找不到目标插槽配置
1004
+ if (
1005
+ !this.configFromWeb[targetName] ||
1006
+ !this.configFromWeb[targetName].columns
1007
+ ) {
1008
+ this.$logger('report.slotConfig').error('无法找到目标插槽的配置!')
1009
+ return
1010
+ }
1011
+
1012
+ // 替换columns,合并data
1013
+ config.columns[i] = []
1014
+ const before = config.columns.slice(0, i)
1015
+ let after = config.columns.slice(i + 1, config.columns.length)
1016
+
1017
+ const addArr = []
1018
+ for (
1019
+ let k = 0;
1020
+ k < this.configFromWeb[targetName].columns.length;
1021
+ k++
1022
+ ) {
1023
+ const temp = []
1024
+ this.configFromWeb[targetName].columns[k].forEach((cell) => {
1025
+ temp.push(cell)
1026
+ })
1027
+ addArr.push(temp)
1028
+ }
1029
+
1030
+ const newArr = []
1031
+ // 拼接之前的数组
1032
+ if (before.length > 0) {
1033
+ if (before.length >= 1) {
1034
+ before.forEach((item) => {
1035
+ newArr.push(item)
1036
+ })
1037
+ } else {
1038
+ newArr.push(before)
1039
+ }
1040
+ }
1041
+
1042
+ addArr.forEach((arr) => {
1043
+ newArr.push(arr)
1044
+ })
1045
+
1046
+ // 拼接之后的数组
1047
+ if (after.length === 1) {
1048
+ if (after[0].type === 'slot' || after[0][0].type === 'slot') {
1049
+ after = []
1050
+ }
1051
+ }
1052
+ if (after.length > 0) {
1053
+ if (after.length >= 1) {
1054
+ after.forEach((item) => {
1055
+ newArr.push(item)
1056
+ })
1057
+ } else {
1058
+ newArr.push(after)
1059
+ }
1060
+ }
1061
+
1062
+ config.columns = newArr
1063
+ if (this.configFromWeb[targetName].slotsDeclare) {
1064
+ config.slotsDeclare = this.configFromWeb[targetName].slotsDeclare
1065
+ } else {
1066
+ config.slotsDeclare = []
1067
+ }
1068
+
1069
+ if (
1070
+ config.data.images &&
1071
+ this.configFromWeb[targetName].data.images
1072
+ ) {
1073
+ config.data.images = {
1074
+ ...config.data.images,
1075
+ ...this.configFromWeb[targetName].data.images,
1076
+ }
1077
+ delete this.configFromWeb[targetName].data.images
1078
+ }
1079
+ config.data = {
1080
+ ...config.data,
1081
+ ...this.configFromWeb[targetName].data,
1082
+ }
1083
+ }
1084
+ }
1085
+ }
1086
+ this.config = config
1087
+ },
1088
+ // 扫描所有插槽名
1089
+ scanConfigName (config, resut) {
1090
+ if (config.slotsDeclare) {
1091
+ config.slotsDeclare.forEach((name) => {
1092
+ resut.push(name)
1093
+ })
1094
+ }
1095
+ },
1096
+ // 获取插槽
1097
+ getConfigAndJoin (config, outerLock) {
1098
+ // 检查主配置插槽声明是否合法
1099
+ const check = this.checkSlotDefine(config)
1100
+ const waitForDownloadSlotName = []
1101
+ if (check) {
1102
+ // 扫描主配置中声明的插槽名
1103
+ this.scanConfigName(config, waitForDownloadSlotName)
1104
+
1105
+ const total = waitForDownloadSlotName.length
1106
+ let count = 0
1107
+
1108
+ // 挨个获取插槽
1109
+ waitForDownloadSlotName.forEach((configName) => {
1110
+ getConfigByName(
1111
+ configName,
1112
+ this.serverName,
1113
+ (res) => {
1114
+ this.configFromWeb[configName] = res
1115
+ count++
1116
+ },
1117
+ this.env === 'dev'
1118
+ )
1119
+ })
1120
+
1121
+ // 使用定时器循环判断锁状态,用于多个插槽,要等待统一获取完成之后,再进行下一步初始化
1122
+ const timer = setInterval(() => {
1123
+ if (count >= total) {
1124
+ clearInterval(timer)
1125
+ this.scanConfigSlot(config)
1126
+ if (config.slotsDeclare.length > 0) {
1127
+ const lock = { status: true }
1128
+ this.getConfigAndJoin(config, lock)
1129
+ const innerTimer = setInterval(() => {
1130
+ if (!lock.status) {
1131
+ clearInterval(innerTimer)
1132
+ outerLock.status = false
1133
+ }
1134
+ }, 100)
1135
+ } else {
1136
+ outerLock.status = false
1137
+ }
1138
+ }
1139
+ }, 100)
1140
+ } else {
1141
+ this.$logger('report.slotConfig').error('插槽配置有误!')
1142
+ outerLock.status = false
1143
+ }
1144
+ },
1145
+ // 获取配置之后的初始化
1146
+ configInit () {
1147
+ this.configEnded = false
1148
+ this.allSlotsConfigEnded = false
1149
+ this._configEndPromise = new Promise(resolve => { this._configEndResolve = resolve })
1150
+ // 将初始化好的配置拷贝一份留存
1151
+ this.originalConfig = Object.assign({}, this.config)
1152
+ if (!this.dontFormat) {
1153
+ // 扫描配置文件中有没有rowSpan,进行格式化调整
1154
+ this.formatConfigRow(this.config)
1155
+ }
1156
+ this.activeConfig = this.config
1157
+ this.showSkeleton = false
1158
+ // 判断是否有动态Index
1159
+ this.activeConfig.columns.forEach((row) => {
1160
+ row.forEach((cell) => {
1161
+ if (cell.dynamicDataIndex === true) {
1162
+ // 如果有动态index,取其函数,运行函数得到真实index保存
1163
+ // eslint-disable-next-line no-eval
1164
+ const func = eval(
1165
+ '(' + cell.customFunctionForDynamicDataIndex + ')'
1166
+ )
1167
+ cell.dataIndex = func(this.config)
1168
+ }
1169
+ // 处理 自定义函数的旧逻辑
1170
+ if (
1171
+ ['action', 'click'].includes(cell.eventType) &&
1172
+ cell.customFunction &&
1173
+ !cell.events
1174
+ ) {
1175
+ cell.events = []
1176
+ cell.events.push({
1177
+ type: cell.eventType,
1178
+ customFunction: cell.customFunction,
1179
+ })
1180
+ }
1181
+ })
1182
+ })
1183
+ // 将数据复制到临时数据中,带有@@@的数据,我们将其整体作为一个key保存,当编辑完成后,再将其解析,回填到需要的数据中
1184
+ this.activeConfig.tempData = {}
1185
+ // 是否有@@@深层引用
1186
+ this.activeConfig.columns.forEach((row) => {
1187
+ row.forEach((cell) => {
1188
+ // 将@@@解析
1189
+ if (
1190
+ cell.dataIndex !== undefined &&
1191
+ cell.dataIndex.indexOf('@@@') !== -1
1192
+ ) {
1193
+ this.activeConfig.tempData[cell.dataIndex] = this.getDeepObject(
1194
+ this.activeConfig.data,
1195
+ cell.dataIndex
1196
+ )
1197
+ }
1198
+ })
1199
+ })
1200
+
1201
+ // 对配置进行转换
1202
+ this.originalConfig.columns = this.transformArray(
1203
+ JSON.parse(JSON.stringify(this.config.columns))
1204
+ )
1205
+
1206
+ this.$nextTick(() => {
1207
+ this.scanFinish = true
1208
+ // slot 时不会触发 slotRendered,在此视为配置彻底结束
1209
+ if (!this.config?.slotsDeclare?.length) {
1210
+ this._markConfigEnded()
1211
+ }
1212
+ })
1213
+ },
1214
+ },
1215
+ beforeMount () {
1216
+ // 如果只是展示
1217
+ if (this.displayOnly) {
1218
+ this.onlyDisplay = true
1219
+ this.type = 'display'
1220
+ }
1221
+ // 如果有本地配置,优先使用本地配置
1222
+ if (this.localConfig) {
1223
+ // 如果配置是普通渲染器
1224
+ this.config = this.localConfig
1225
+ if (this.configData !== undefined) {
1226
+ this.config.data = this.configData
1227
+ }
1228
+ if (this.config.data.images === undefined) {
1229
+ this.config.data.images = {}
1230
+ }
1231
+ this.configInit()
1232
+ this.$logger('report.beforeMount').log('本地 config 就绪', { configName: this.configName })
1233
+ } else {
1234
+ // 如果本地配置没有值,则从琉璃中获取
1235
+ getConfigByName(
1236
+ this.configName,
1237
+ this.serverName,
1238
+ (res) => {
1239
+ this.config = JSON.parse(JSON.stringify(res))
1240
+ if (this.configData !== undefined) {
1241
+ this.config.data = this.configData
1242
+ }
1243
+ if (this.config.data.images === undefined) {
1244
+ this.config.data.images = {}
1245
+ }
1246
+ this.configInit()
1247
+ this.$logger('report.beforeMount').log('远程 config 加载完成', { configName: this.configName })
1248
+ },
1249
+ this.env === 'dev'
1250
+ )
1251
+ }
1252
+ },
1253
+ computed: {
1254
+ ...mapState('account', { currUser: 'user' }),
1255
+ widget () {
1256
+ return this.isWidget // 返回isWidget的值
1257
+ },
1258
+ },
1259
+ mounted () {
1260
+ // 如果外界传来了registerMap,我们将本VM对象注册到map中
1261
+ if (this.registerMap !== undefined) {
1262
+ this.registerMap.push(this)
1263
+ }
1264
+ // TAB 页签的 slotRef 由 XTab 在 onComponentMounted 中统一注册(与 Cover 普通子组件一致),此处不再自我注册
1265
+ // 将原始数据备份保存
1266
+ if (this.configData) {
1267
+ this.dataCache = JSON.parse(JSON.stringify(this.configData))
1268
+ } else {
1269
+ if (this.config?.data) {
1270
+ this.dataCache = JSON.parse(JSON.stringify(this.config.data))
1271
+ }
1272
+ }
1273
+ },
1274
+ }
1275
+ </script>
1276
+
1277
+ <style lang="less" scoped>
1278
+ .tools {
1279
+ text-align: center;
1280
+ cursor: pointer;
1281
+
1282
+ .toolsItem {
1283
+ display: inline-block;
1284
+ }
1285
+ }
1286
+ </style>