wui-components-v2 1.2.37 → 1.2.38

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/api/page.ts CHANGED
@@ -36,6 +36,13 @@ export function listData(queryKey: string, pageNo: number, pageSize: number): Pr
36
36
  })
37
37
  }
38
38
 
39
+ // 获取报表页面数据
40
+ export function reportData(queryKey: string, pageNo: number, pageSize: number): Promise<PageData> {
41
+ return req({
42
+ url: `/v3/ltmpl/report-data?queryKey=${queryKey}&pageNo=${pageNo}&pageSize=${pageSize}`,
43
+ })
44
+ }
45
+
39
46
  // 获取新增面配置
40
47
  export function addPageConfig(sourceId: string) {
41
48
  return req({
@@ -0,0 +1,411 @@
1
+ <script lang="ts" setup>
2
+ import { computed, onMounted, onUnmounted, ref } from 'vue'
3
+ import { onLoad } from '@dcloudio/uni-app'
4
+ import { reportData, pageConfig, pageKey } from '../../api/page'
5
+ import type { Columns, Config, Entities, Fields } from '../../type'
6
+ import Search from '../search/search.vue'
7
+ import ListTopButtons from '../list-top-buttons/list-top-buttons.vue'
8
+ import { generateHighResolutionID, formatItemData } from '../../utils/index'
9
+ import tabSearch from '../tab-search/tab-search.vue'
10
+ import { useEnums } from '../../composables/useEnumes'
11
+ import ControlTypeSupportor from '../../utils/control-type-supportor'
12
+ import dayjs from 'dayjs/esm/index'
13
+ import formControl from '../form-control/form-control.vue'
14
+
15
+ defineOptions({
16
+ name: 'WuiReportTable',
17
+ })
18
+
19
+ const props = withDefaults(
20
+ defineProps<{
21
+ /** 数据源 ID */
22
+ sourceId?: string
23
+ /** 查询条件 */
24
+ query?: string
25
+ /** 顶部操作按钮区是否显示 */
26
+ showTopButtons?: boolean
27
+ /** 搜索区域是否显示 */
28
+ showSearch?: boolean
29
+ }>(),
30
+ {
31
+ showTopButtons: true,
32
+ showSearch: true,
33
+ sourceId: '',
34
+ query: '',
35
+ }
36
+ )
37
+
38
+ const config = ref<Config>({
39
+ buttons: [],
40
+ columns: [],
41
+ primaryColumn: {
42
+ sourceId: '',
43
+ controlType: '',
44
+ mstrucId: '',
45
+ extControlType: '',
46
+ },
47
+ primaryCriterion: {
48
+ hidden: false,
49
+ disabled: false,
50
+ defaultValue: '',
51
+ transDefaultValue: '',
52
+ required: false,
53
+ controlType: '',
54
+ sourceId: '',
55
+ mstrucId: '',
56
+ extControlType: '',
57
+ },
58
+ secondColumn: {
59
+ sourceId: '',
60
+ controlType: '',
61
+ mstrucId: '',
62
+ extControlType: '',
63
+ },
64
+ criteria: [],
65
+ id: '',
66
+ fields: [],
67
+ title: '',
68
+ type: '',
69
+ pointSourceId: '',
70
+ mstrucId: '',
71
+ relationNames: [],
72
+ readOnly: false,
73
+ displayConfig: [],
74
+ showType: 'table',
75
+ })
76
+ const { getEnums, enumColumn } = useEnums({ config })
77
+ const datas = ref<Entities[]>([])
78
+ const pageTitle = ref('')
79
+ const searchData = ref('')
80
+ const tabSearchData = ref('')
81
+ const pageType = ref('')
82
+ const mainCode = ref('')
83
+ const queryKey = ref('')
84
+ const rowGrids = ref<Record<string, any>>({})
85
+ const addEvent = generateHighResolutionID()
86
+ const loading = ref(false)
87
+
88
+ // 时间相关的控件类型
89
+ const TIME_TYPES = ['date', 'daterange', 'datetime', 'datetimerange', 'time', 'timerange']
90
+
91
+ // 从 criteria 中筛选出时间类型的字段
92
+ const timeCriteria = computed<Fields[]>(() => {
93
+ return config.value.criteria.filter(item => {
94
+ const type = ControlTypeSupportor.getSupportControlType(item as any)
95
+ return TIME_TYPES.some(t => type.startsWith(t))
96
+ })
97
+ })
98
+
99
+ // 从 criteria 中排除时间类型字段,传给 Search
100
+ const nonTimeCriteria = computed<Fields[]>(() => {
101
+ return config.value.criteria.filter(item => {
102
+ const type = ControlTypeSupportor.getSupportControlType(item as any)
103
+ return !TIME_TYPES.some(t => type.startsWith(t))
104
+ })
105
+ })
106
+
107
+ // 过滤掉「操作」列
108
+ const tableColumns = computed<Columns[]>(() => {
109
+ return config.value.columns.filter(item => item.title !== '操作')
110
+ })
111
+
112
+ // 将 Entities[] 转成 wd-table 需要的 Record<string, any>[]
113
+ const tableData = computed<Record<string, any>[]>(() => {
114
+ return datas.value.map(row => {
115
+ const record: Record<string, any> = { code: row.code }
116
+ tableColumns.value.forEach(col => {
117
+ const raw = row.fieldMap[col.sourceId]
118
+ if (raw !== undefined && raw !== null) {
119
+ const type = ControlTypeSupportor.getControlType(col as any, raw)
120
+ record[col.sourceId] = formatItemData(raw, type)
121
+ } else {
122
+ record[col.sourceId] = ''
123
+ }
124
+ })
125
+ return record
126
+ })
127
+ })
128
+
129
+ // 根据内容计算每列宽度(中文字符约16px,英文/数字约8px,最大宽度300px)
130
+ // 无数据时用 1fr 让列均分占满整行
131
+ const columnWidths = computed<Record<string, string>>(() => {
132
+ const widths: Record<string, string> = {}
133
+ const MAX_WIDTH = 300
134
+ const MIN_WIDTH = 80
135
+ const CHAR_WIDTH_CN = 16
136
+ const CHAR_WIDTH_EN = 8
137
+ const PADDING = 32
138
+
139
+ // 无数据时,所有列按百分比均分占满整行
140
+ // 注意:不能用 1fr,因为 wd-table 的 contentWidthStyle 会用 calc() 求和,
141
+ // fr 单位在 calc() 中是非法值,这里改用百分比
142
+ if (datas.value.length === 0 && tableColumns.value.length > 0) {
143
+ const colCount = tableColumns.value.length
144
+ const percent = (100 / colCount).toFixed(4) + '%'
145
+ tableColumns.value.forEach(col => {
146
+ widths[col.sourceId] = percent
147
+ })
148
+ return widths
149
+ }
150
+
151
+ tableColumns.value.forEach(col => {
152
+ let maxLen = col.title?.length || 0
153
+ // 遍历所有行找该列最长内容
154
+ tableData.value.forEach(row => {
155
+ const val = String(row[col.sourceId] || '')
156
+ // 计算字符宽度:中文算2个单位,英文算1个单位
157
+ let charCount = 0
158
+ for (const ch of val) {
159
+ charCount += /[一-鿿]/.test(ch) ? 2 : 1
160
+ }
161
+ maxLen = Math.max(maxLen, charCount)
162
+ })
163
+ const px = Math.min(MAX_WIDTH, Math.max(MIN_WIDTH, maxLen * CHAR_WIDTH_EN + PADDING))
164
+ widths[col.sourceId] = `${px}px`
165
+ })
166
+ return widths
167
+ })
168
+
169
+ const internalSourceId = ref(props.sourceId)
170
+
171
+ onLoad((option: any) => {
172
+ if (!internalSourceId.value) {
173
+ internalSourceId.value = option.sourceId
174
+ }
175
+ pageTitle.value = option.title
176
+ pageType.value = option.pageType
177
+ mainCode.value = option.mainCode || ''
178
+ uni.setNavigationBarTitle({
179
+ title: option.title,
180
+ })
181
+ getPageConfig()
182
+ })
183
+
184
+ onMounted(async () => {
185
+ uni.$on(addEvent, async () => {
186
+ fetchData()
187
+ })
188
+ })
189
+
190
+ onUnmounted(() => {
191
+ uni.$off(addEvent)
192
+ })
193
+
194
+ async function getPageConfig() {
195
+ try {
196
+ const res = await pageConfig(internalSourceId.value)
197
+ if (res.ltmplConfig) {
198
+ config.value = res.ltmplConfig
199
+ getEnums()
200
+ fetchData()
201
+ }
202
+ } catch (error) {
203
+ console.log(error)
204
+ }
205
+ }
206
+
207
+ // 一次性请求所有数据(pageSize 设一个足够大的值)
208
+ async function fetchData() {
209
+ // 首次加载时,从字段默认值构建时间查询条件
210
+ if (!timeInited.value) {
211
+ timeInited.value = true
212
+ let defaultTimeData = ''
213
+ timeCriteria.value.forEach(item => {
214
+ const defaultVal = item.transDefaultValue || item.defaultValue
215
+ if (defaultVal && defaultVal !== '') {
216
+ const formatted = /^\d+$/.test(String(defaultVal)) ? dayjs(Number(defaultVal)).format('YYYY-MM-DD') : defaultVal
217
+ defaultTimeData = defaultTimeData
218
+ ? `${defaultTimeData}&c_${item.sourceId}=${formatted}`
219
+ : `c_${item.sourceId}=${formatted}`
220
+ }
221
+ })
222
+ timeSearchData.value = defaultTimeData
223
+ }
224
+ // 合并时间筛选和其他搜索条件
225
+ const parts = [searchData.value, timeSearchData.value].filter(Boolean)
226
+ let qre = parts.join('&') || tabSearchData.value
227
+ if (props.query) {
228
+ qre = qre ? `${qre}&${props.query}` : props.query
229
+ }
230
+
231
+ loading.value = true
232
+ try {
233
+ const res = await pageKey(internalSourceId.value, mainCode.value, qre)
234
+ const data: any = await reportData(res.key, 1, 9999)
235
+ queryKey.value = res.key || data?.key || ''
236
+ rowGrids.value = data?.rowGrids || {}
237
+ datas.value = data.entities || []
238
+ } catch (error) {
239
+ console.error(error)
240
+ } finally {
241
+ loading.value = false
242
+ }
243
+ }
244
+
245
+ // 单元格合并:根据 rowGrids 配置返回 { rowspan, colspan }
246
+ function spanMethod({ row, columnIndex }: any) {
247
+ const code = row?.code
248
+ if (!code || !rowGrids.value[code]) return { rowspan: 1, colspan: 1 }
249
+
250
+ const cellMap = rowGrids.value[code]?.cellMap
251
+ if (!cellMap) return { rowspan: 1, colspan: 1 }
252
+
253
+ const col = tableColumns.value[columnIndex]
254
+ if (!col) return { rowspan: 1, colspan: 1 }
255
+
256
+ const cell = cellMap[col.sourceId]
257
+ if (!cell) return { rowspan: 1, colspan: 1 }
258
+
259
+ const rowspan = cell.rowSpan ?? 1
260
+ const colspan = cell.colSpan ?? 1
261
+
262
+ if (rowspan === 0 || colspan === 0) return { rowspan: 0, colspan: 0 }
263
+
264
+ return { rowspan, colspan }
265
+ }
266
+
267
+ function submitSearch(data: any) {
268
+ searchData.value = data
269
+ // 同时收集时间筛选条件
270
+ if (timeFormRef.value) {
271
+ timeFormRef.value.submit().then((fdata: any) => {
272
+ let timeData = ''
273
+ timeCriteria.value.forEach(item => {
274
+ const val = fdata[item.sourceId]
275
+ if (val === undefined || val === null || val === '') return
276
+ if (Array.isArray(val) && val.length === 0) return
277
+ if (item.transDefaultValue && String(val) === String(item.transDefaultValue)) return
278
+ // 时间戳转日期字符串
279
+ const formatted = typeof val === 'number' ? dayjs(val).format('YYYY-MM-DD') : val
280
+ timeData = timeData ? `${timeData}&c_${item.sourceId}=${formatted}` : `c_${item.sourceId}=${formatted}`
281
+ })
282
+ timeSearchData.value = timeData
283
+ fetchData()
284
+ })
285
+ } else {
286
+ fetchData()
287
+ }
288
+ }
289
+
290
+ // 时间筛选
291
+ const timeFormEntity = ref({})
292
+ const timeFormRef = ref()
293
+ const timeInited = ref(false)
294
+
295
+ // 区分时间筛选和其他搜索条件
296
+ const timeSearchData = ref('')
297
+ function submitTimeFilter() {
298
+ if (!timeFormRef.value) return
299
+ timeFormRef.value.submit().then((fdata: any) => {
300
+ let data = ''
301
+ timeCriteria.value.forEach(item => {
302
+ const val = fdata[item.sourceId]
303
+ if (val === undefined || val === null || val === '') return
304
+ if (Array.isArray(val) && val.length === 0) return
305
+ if (item.transDefaultValue && String(val) === String(item.transDefaultValue)) return
306
+ // 时间戳转日期字符串
307
+ const formatted = typeof val === 'number' ? dayjs(val).format('YYYY-MM-DD') : val
308
+ data = data ? `${data}&c_${item.sourceId}=${formatted}` : `c_${item.sourceId}=${formatted}`
309
+ })
310
+ timeSearchData.value = data
311
+ fetchData()
312
+ })
313
+ }
314
+
315
+ function tabSearchClick(data: any) {
316
+ if (searchData.value) {
317
+ tabSearchData.value = `${searchData.value}&${data}`
318
+ } else {
319
+ tabSearchData.value = data
320
+ }
321
+ fetchData()
322
+ }
323
+ </script>
324
+
325
+ <template>
326
+ <view>
327
+ <slot name="top" />
328
+ <!-- 时间筛选区域 -->
329
+ <view v-if="timeCriteria.length" class="time-filter-bar">
330
+ <formControl
331
+ ref="timeFormRef"
332
+ :field-group="{
333
+ fields: timeCriteria,
334
+ readOnly: false,
335
+ displayConfig: [],
336
+ id: '',
337
+ buttons: [],
338
+ title: '',
339
+ type: '',
340
+ pointSourceId: '',
341
+ mstrucId: '',
342
+ relationNames: [],
343
+ }"
344
+ :entity="timeFormEntity"
345
+ :enum-column="enumColumn"
346
+ />
347
+ </view>
348
+ <view class="px-[16px]">
349
+ <Search
350
+ :main-code="mainCode"
351
+ :enum-column="enumColumn"
352
+ :criteria="nonTimeCriteria"
353
+ :primary-criterion="config.primaryCriterion"
354
+ :split2-tab-criterias="config.split2TabCriterias"
355
+ @submit="submitSearch"
356
+ />
357
+ </view>
358
+
359
+ <view class="mt-2 px-[16px]">
360
+ <ListTopButtons
361
+ :add-event="addEvent"
362
+ :main-code="mainCode"
363
+ :buttons="config.buttons"
364
+ :source-id="internalSourceId"
365
+ :page-title="pageTitle"
366
+ :page-type="pageType"
367
+ :config="config"
368
+ :show-buttons="showTopButtons"
369
+ :query-key="queryKey"
370
+ />
371
+ </view>
372
+ <view v-if="config.split2TabCriterias" class="mt-2">
373
+ <tabSearch :split2-tab-criterias="config.split2TabCriterias" :enums="enumColumn" @click="tabSearchClick" />
374
+ </view>
375
+
376
+ <!-- wot-ui Table -->
377
+ <wd-table :data="tableData" :border="false" :span-method="spanMethod" :stripe="false" center>
378
+ <wd-table-column
379
+ v-for="col in tableColumns"
380
+ :key="col.sourceId"
381
+ :prop="col.sourceId"
382
+ :label="col.title || col.sourceId"
383
+ :width="columnWidths[col.sourceId]"
384
+ align="left"
385
+ />
386
+ </wd-table>
387
+
388
+ <slot name="bottom" />
389
+ </view>
390
+ </template>
391
+
392
+ <style scoped lang="scss">
393
+ :deep(.wd-table) {
394
+ margin: 16rpx;
395
+ width: auto;
396
+ }
397
+
398
+ // 修复合并单元格后内容不垂直居中
399
+ ::deep(.wd-table__value) {
400
+ display: flex !important;
401
+ align-items: center !important;
402
+ line-height: 1.5 !important;
403
+ }
404
+
405
+ .time-filter-bar {
406
+ padding: 16rpx;
407
+ }
408
+ :deep(.wd-table__cell) {
409
+ border: 0.5px solid #e5e5e5;
410
+ }
411
+ </style>
@@ -68,9 +68,7 @@ export function useMenus(props?: Props, pagingRef?: Ref<any>) {
68
68
  router.push(`/pages/tree-page/index?sourceId=${item.id}&title=${item.title}`)
69
69
  return true
70
70
  case '报表':
71
- // uni.navigateTo({
72
- // url: `/pages/report-table/report-table?sourceId=${item.id}&title=${item.title}`,
73
- // })
71
+ router.push(`/pages/report-table/index?sourceId=${item.id}&title=${item.title}`)
74
72
  return true
75
73
 
76
74
  default:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wui-components-v2",
3
- "version": "1.2.37",
3
+ "version": "1.2.38",
4
4
  "description": "wui 组件库",
5
5
  "author": "wgxshh",
6
6
  "license": "MIT",