wui-components-v2 1.2.76 → 1.2.77

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.
@@ -13,6 +13,8 @@ const props = defineProps({
13
13
  type: { type: String, default: 'date' },
14
14
  min: String,
15
15
  max: String,
16
+ // 时间选择是否到秒(form-control 里 time 字段会传 use-second,之前组件没声明,属性被静默丢弃)
17
+ useSecond: { type: Boolean, default: false },
16
18
  readonly: { type: Boolean, default: false },
17
19
  })
18
20
 
@@ -27,10 +29,27 @@ interface SelectOption {
27
29
  }
28
30
  const open = ref(false)
29
31
  const currentValue = ref<any>(props.modelValue)
32
+ // 传给 wd-calendar 的值:该组件只认 13 位时间戳,而表单里存的是 'YYYY-MM-DD HH:mm:ss' 之类的字符串
33
+ const calendarValue = ref<number | null>(null)
30
34
  function clear() {
31
35
  currentValue.value = ''
32
36
  }
33
37
 
38
+ /** 字符串统一按 ISO 解析(iOS 对 'YYYY-MM-DD HH:mm:ss' 解析不稳定),时间戳/Date 直接交给 dayjs */
39
+ function toDayjs(value: any) {
40
+ return typeof value === 'string' ? dayjs(value.trim().replace(' ', 'T')) : dayjs(value)
41
+ }
42
+
43
+ /** 表单值(字符串/时间戳)→ 13 位时间戳,无法解析时返回 null */
44
+ function toTimestamp(value: any): number | null {
45
+ if (value === undefined || value === null || value === '') return null
46
+ if (typeof value === 'number') return value
47
+ const str = String(value).trim()
48
+ if (/^\d{10,13}$/.test(str)) return Number(str)
49
+ const date = toDayjs(str)
50
+ return date.isValid() ? date.valueOf() : null
51
+ }
52
+
34
53
  watch(
35
54
  () => props.modelValue,
36
55
  val => {
@@ -39,10 +58,37 @@ watch(
39
58
  )
40
59
 
41
60
  watch(currentValue, val => {
42
- const newVal = ['year', 'month'].includes(props.type) ? dayjs(val).format('YYYY-MM') : val
61
+ if (val === '' || val === undefined || val === null) {
62
+ emit('update:modelValue', '')
63
+ return
64
+ }
65
+ const newVal = ['year', 'month'].includes(props.type) ? toDayjs(val).format('YYYY-MM') : val
43
66
  emit('update:modelValue', newVal)
44
67
  })
45
68
 
69
+ // datetime 类型下「选中某个日期」时使用的当日内时刻
70
+ const defaultTime = ref<string | undefined>(undefined)
71
+
72
+ /**
73
+ * 打开弹窗时同步面板值:
74
+ * - 表单已有值:转成时间戳,让面板定位并高亮到该日期
75
+ * - 表单为空:默认选中「现在」(datetime 会带上当前时分秒),直接点确定即可用当前时间
76
+ * 这里只设置面板值、不写回表单,用户取消关闭不会污染空字段
77
+ */
78
+ watch(open, val => {
79
+ if (!val) return
80
+ const now = dayjs()
81
+ // datetime 选中日期时若不给默认时刻会落到 00:00:00,这里统一用当前时分秒
82
+ defaultTime.value = props.type === 'datetime' ? now.format('HH:mm:ss') : undefined
83
+ calendarValue.value = toTimestamp(currentValue.value) ?? now.valueOf()
84
+ })
85
+
86
+ /** 日历确认后才写回表单,避免「打开即赋值」 */
87
+ function handleCalendarConfirm({ value }: { value: number | number[] | null }) {
88
+ if (value === undefined || value === null) return
89
+ currentValue.value = value
90
+ }
91
+
46
92
  const labelText = computed(() => {
47
93
  const formatMap: {
48
94
  date: string
@@ -50,12 +96,14 @@ const labelText = computed(() => {
50
96
  [key: string]: string // 索引签名:允许任意字符串作为键
51
97
  } = {
52
98
  date: 'YYYY-MM-DD',
53
- datetime: 'YYYY-MM-DD HH:mm',
99
+ // 回显到秒,避免选完时间看不到秒
100
+ datetime: 'YYYY-MM-DD HH:mm:ss',
54
101
  year: 'YYYY',
55
102
  }
56
103
 
57
104
  if (['date', 'datetime', 'year'].includes(props.type)) {
58
- return currentValue.value ? dayjs(currentValue.value).format(formatMap[props.type] as string) : ''
105
+ const date = currentValue.value ? toDayjs(currentValue.value) : null
106
+ return date && date.isValid() ? date.format(formatMap[props.type] as string) : ''
59
107
  } else {
60
108
  return currentValue.value || ''
61
109
  }
@@ -72,13 +120,15 @@ console.log('labelText', props)
72
120
 
73
121
  <wd-calendar
74
122
  v-if="['date', 'datetime', 'month'].includes(type)"
75
- v-model="currentValue"
123
+ v-model="calendarValue"
76
124
  v-model:visible="open"
77
125
  label-key="label"
78
126
  value-key="value"
79
127
  class="custom-date-picker"
80
128
  :type="type as any"
81
129
  :min-date="props.min === '$$current' ? dayjs().startOf('day').valueOf() : undefined"
130
+ :default-time="defaultTime"
131
+ @confirm="handleCalendarConfirm"
82
132
  />
83
133
  <wd-datetime-picker
84
134
  v-else
@@ -88,6 +138,7 @@ console.log('labelText', props)
88
138
  value-key="value"
89
139
  class="custom-date-picker"
90
140
  :type="type as any"
141
+ :use-second="useSecond"
91
142
  />
92
143
  </template>
93
144
 
@@ -619,6 +619,20 @@ function handleFileChange(e: any, item: Fields) {
619
619
  model.value[item.sourceId] = e.fileList
620
620
  }
621
621
 
622
+ /**
623
+ * 时间字段的值是 'HH:mm' / 'HH:mm:ss' 字符串,dayjs 直接解析会得到 Invalid Date,
624
+ * 这里补一个日期前缀(用 ISO 格式,iOS 也能稳定解析)
625
+ */
626
+ function formatTimeValue(value: any): string {
627
+ if (!value) return ''
628
+ if (typeof value === 'number') return dayjs(value).format('HH:mm:ss')
629
+ const str = String(value).trim()
630
+ if (!str.includes(':')) return str
631
+ const time = str.length === 5 ? `${str}:00` : str
632
+ const parsed = dayjs(`2000-01-01T${time}`)
633
+ return parsed.isValid() ? parsed.format('HH:mm:ss') : str
634
+ }
635
+
622
636
  function submit() {
623
637
  return new Promise((resolve, reject) => {
624
638
  form.value
@@ -662,7 +676,7 @@ function submit() {
662
676
  } else if (ControlTypeSupportor.getControlType(item) === 'datetime') {
663
677
  data[key] = model.value[key] ? dayjs(model.value[key]).format('YYYY-MM-DD HH:mm:ss') : ''
664
678
  } else if (ControlTypeSupportor.getControlType(item) === 'time') {
665
- data[key] = model.value[key] ? dayjs(model.value[key]).format('HH:mm:ss') : ''
679
+ data[key] = formatTimeValue(model.value[key])
666
680
  } else if (ControlTypeSupportor.getControlType(item) === 'date-YY') {
667
681
  data[key] = model.value[key] ? dayjs(model.value[key]).format('YYYY') : ''
668
682
  } else if (ControlTypeSupportor.getControlType(item) === 'date-M1') {
@@ -1,11 +1,12 @@
1
1
  <script setup lang="ts">
2
- import { defineOptions, ref } from 'vue'
2
+ import { defineOptions, onMounted, onUnmounted, ref } from 'vue'
3
3
  import { onLoad } from '@dcloudio/uni-app'
4
4
  import { detailPageConfig, detailPageData } from '../../api/page'
5
5
  import type { Entities, Groups, dtmplConfig } from '../../type'
6
6
  import CardBotomButtons from '../card-botom-buttons/card-botom-buttons.vue'
7
7
  import foldCard from '../fold-card/fold-card.vue'
8
8
  import LabelValue from '../label-value/label-value.vue'
9
+ import { generateHighResolutionID } from '../../utils/index'
9
10
 
10
11
  defineOptions({
11
12
  name: 'WuiDetailsPage',
@@ -56,6 +57,10 @@ const groups = ref<Groups>({
56
57
  displayConfig: [],
57
58
  })
58
59
  const loading = ref(false)
60
+ // 全局事件名:本页新增/编辑关联记录保存后,用于接收刷新信号
61
+ const addEvent = generateHighResolutionID()
62
+ // CardBotomButtons 需要 zpaging 对象(删除/复制后刷新),详情页用自身数据重载模拟
63
+ const detailZpaging = { reload: () => getDetailData() }
59
64
  // 折叠面板
60
65
  const collapses = ref()
61
66
  /**
@@ -144,6 +149,14 @@ async function getDetailData() {
144
149
  console.log(error)
145
150
  }
146
151
  }
152
+
153
+ onMounted(() => {
154
+ // 详情页内的关联记录新增/编辑保存后,重新拉取详情数据
155
+ uni.$on(addEvent, () => getDetailData())
156
+ })
157
+ onUnmounted(() => {
158
+ uni.$off(addEvent)
159
+ })
147
160
  </script>
148
161
 
149
162
  <template>
@@ -190,6 +203,8 @@ async function getDetailData() {
190
203
  :item="groups"
191
204
  :code="field.code"
192
205
  :data="data"
206
+ :add-event="addEvent"
207
+ :zpaging="detailZpaging"
193
208
  :detail-button-handle="props.detailButtonHandle"
194
209
  />
195
210
  </template>
@@ -91,17 +91,31 @@ function isActionDisabled(item: rowActions): boolean {
91
91
  return false
92
92
  }
93
93
  console.log(pageActions.value, 'pageActions.value')
94
+ /**
95
+ * 兼容调用方漏传参数的情况:URL 拼接会把 undefined/null 变成字面量字符串,
96
+ * 原样赋值会让 uni.$emit('undefined') 发到一个不存在的事件名上,上一页永远收不到刷新信号。
97
+ */
98
+ function normalizeParam(value?: any): string {
99
+ if (value === undefined || value === null || value === 'undefined' || value === 'null') return ''
100
+ return String(value)
101
+ }
102
+
103
+ /** 从各类错误对象(接口错误/Error/小程序 errMsg)里提取可展示的提示文案 */
104
+ function errorMessage(error: any, fallback = '操作失败'): string {
105
+ return error?.data?.message || error?.data?.msg || error?.errMsg || error?.message || fallback
106
+ }
107
+
94
108
  onLoad((option: any) => {
95
109
  sourceId.value = option.sourceId
96
- mainCode.value = option.mainCode || ''
97
- id.value = option.id || ''
110
+ mainCode.value = normalizeParam(option.mainCode)
111
+ id.value = normalizeParam(option.id)
98
112
  pageTitle.value = option.title
99
- addEvent.value = option.addEvent
100
- editEvent.value = option.editEvent
101
- pageType.value = option.pageType || ''
102
- rowAddEvent.value = option.rowAddEvent
103
- sendRowAddEvent.value = option.sendRowAddEvent
104
- copyEvent.value = option.copyEvent
113
+ addEvent.value = normalizeParam(option.addEvent)
114
+ editEvent.value = normalizeParam(option.editEvent)
115
+ pageType.value = normalizeParam(option.pageType)
116
+ rowAddEvent.value = normalizeParam(option.rowAddEvent)
117
+ sendRowAddEvent.value = normalizeParam(option.sendRowAddEvent)
118
+ copyEvent.value = normalizeParam(option.copyEvent)
105
119
  // 复制场景:仅当 URL 带 copyEvent 标识(即"复制新增")才读取预填数据;
106
120
  // 普通新增不带该标识,绝不读取存储,避免复制残留数据污染普通新增
107
121
  if (copyEvent.value) {
@@ -259,8 +273,10 @@ async function getEnums() {
259
273
  // 行添加保存
260
274
  async function rowAddSave(all: any) {
261
275
  console.log('行数据', all)
262
- router.back()
276
+ // 先发送数据再返回:返回会触发本页 onUnmounted 里的 uni.$off(rowAddEvent),
277
+ // 放在 back() 之后发送有概率被父页面的监听错过
263
278
  uni.$emit(rowAddEvent.value, all)
279
+ router.back()
264
280
  }
265
281
 
266
282
  // 保存数据并返回保存后的实体编码;行添加场景会在内部直接处理跳转
@@ -268,7 +284,9 @@ async function saveData(): Promise<{ entityCode: string; data: Record<string, an
268
284
  // 判断是否为树添加
269
285
  if (pageType.value === '树' && !tree.value.value) {
270
286
  globalToast.warning('请选择分组')
271
- throw new Error('请选择分组')
287
+ const err: any = new Error('请选择分组')
288
+ err.handled = true // 已提示过,save() 的 catch 不再重复弹提示
289
+ throw err
272
290
  }
273
291
 
274
292
  const formArray: any[] = []
@@ -371,27 +389,38 @@ async function saveData(): Promise<{ entityCode: string; data: Record<string, an
371
389
  return { entityCode: saveRes.entityCode, data: newAll }
372
390
  }
373
391
 
392
+ /**
393
+ * 统一广播刷新信号,通知上一页/弹窗重新取数:
394
+ * - addEvent / editEvent:调用方(列表、树页面、选人组件)注册的事件
395
+ * - selectPopup:refresh:选择弹窗列表
396
+ */
397
+ function emitRefresh(entityCode = '', data: Record<string, any> = {}) {
398
+ if (addEvent.value) uni.$emit(addEvent.value, { entityCode, data })
399
+ if (editEvent.value) uni.$emit(editEvent.value, entityCode)
400
+ uni.$emit('selectPopup:refresh', sourceId.value)
401
+ }
402
+
374
403
  // 保存
375
404
  async function save() {
376
405
  try {
377
406
  const { entityCode, data } = await saveData()
378
407
  // 行添加场景已在 saveData 内完成跳转
379
408
  if (rowAddEvent.value) return
380
- // 发送事件
381
- if (addEvent.value) uni.$emit(addEvent.value, { entityCode, data })
382
- if (editEvent.value) uni.$emit(editEvent.value, entityCode)
383
-
384
- // ★ 通知 wui-select-popup 刷新列表数据
385
- uni.$emit('selectPopup:refresh', sourceId.value)
409
+ // 发送事件,通知上一页/弹窗刷新
410
+ emitRefresh(entityCode, data)
386
411
 
387
412
  // 复制场景:保存成功后清除本次复制数据(按 copyEvent key),避免残留
388
413
  if (copyEvent.value) uni.removeStorageSync(copyEvent.value)
389
414
 
390
415
  subLoading.value = false
391
416
  router.back()
392
- } catch (error) {
417
+ } catch (error: any) {
393
418
  subLoading.value = false
394
419
  console.log('error:', error)
420
+ // 之前只打日志:保存失败(校验不通过、401、网络异常等)界面上完全没有反馈
421
+ if (!error?.handled) {
422
+ globalToast.error(errorMessage(error, '保存失败'))
423
+ }
395
424
  }
396
425
  }
397
426
 
@@ -399,8 +428,13 @@ async function save() {
399
428
  const actionItem = ref<Groups>()
400
429
  const actionItemShow = ref(false)
401
430
  const actionCode = ref('')
402
- // ActionPopup 成功后的刷新处理(编辑页无 zpaging,直接返回上一页)
403
- const actionZpaging = { reload: () => router.back() }
431
+ // ActionPopup 成功后的刷新处理(编辑页无 zpaging:先广播刷新信号,再返回上一页)
432
+ const actionZpaging = {
433
+ reload: () => {
434
+ emitRefresh(id.value)
435
+ router.back()
436
+ },
437
+ }
404
438
 
405
439
  // 执行底部动态操作按钮:直接走 action 接口(不提交表单)
406
440
  async function executeAction(subitem: rowActions) {
@@ -456,12 +490,13 @@ async function doAction(subitem: rowActions) {
456
490
  await actionDataSave(subitem.id, actionCode.value, {})
457
491
  subLoading.value = false
458
492
  globalToast.success({ msg: '操作成功!' })
459
- // 通知列表/弹窗刷新
460
- uni.$emit('selectPopup:refresh', sourceId.value)
493
+ // 通知列表/弹窗刷新后返回:只发 selectPopup:refresh 时列表页收不到
494
+ emitRefresh(id.value)
461
495
  router.back()
462
496
  } catch (error: any) {
463
497
  subLoading.value = false
464
- globalToast.error(error)
498
+ // 传错误对象当 ToastOptions 是显示不出文案的,这里取出 message
499
+ globalToast.error(errorMessage(error))
465
500
  }
466
501
  }
467
502
 
@@ -1,6 +1,6 @@
1
1
  <script lang="ts" setup>
2
2
  import { computed, defineOptions, onMounted, onUnmounted, ref } from 'vue'
3
- import { onLoad } from '@dcloudio/uni-app'
3
+ import { onHide, onLoad, onShow } from '@dcloudio/uni-app'
4
4
  import { actionDataSave, deleteData, listData, pageConfig, pageKey } from '../../api/page'
5
5
  import foldCard2 from '../fold-card2/fold-card2.vue'
6
6
  import type { Config, EditButtonConfig, Entities, rowActions } from '../../type'
@@ -121,6 +121,10 @@ const props = withDefaults(
121
121
  )
122
122
 
123
123
  const Zpaging = ref<any>(null)
124
+ /** 是否已经历过首次显示:首次显示由 z-paging 自己加载,避免重复请求 */
125
+ const hasShown = ref(false)
126
+ /** 本次隐藏期间是否已通过刷新事件取过数:避免返回时 onShow 再重复请求一次 */
127
+ let refreshedWhileHidden = false
124
128
  const config = ref<Config>({
125
129
  buttons: [],
126
130
  columns: [],
@@ -430,9 +434,28 @@ onMounted(async () => {
430
434
  console.log('mounted')
431
435
  // 监听添加成功的事件
432
436
  uni.$on(addEvent, async () => {
437
+ refreshedWhileHidden = true
433
438
  Zpaging.value.reload()
434
439
  })
435
440
  })
441
+ // 页面隐藏时复位标记,保证标记只对"紧接着的那一次显示"生效
442
+ onHide(() => {
443
+ refreshedWhileHidden = false
444
+ })
445
+ // 页面重新显示时刷新列表:不再只依赖上一页的刷新事件
446
+ // (编辑页未保存/保存接口失败/事件名不匹配时,事件链会失效,这里做兜底)
447
+ onShow(() => {
448
+ if (!hasShown.value) {
449
+ hasShown.value = true
450
+ return
451
+ }
452
+ // 隐藏期间已经收到过刷新事件的话,这次显示就不用再请求一遍
453
+ if (refreshedWhileHidden) {
454
+ refreshedWhileHidden = false
455
+ return
456
+ }
457
+ Zpaging.value?.reload()
458
+ })
436
459
  // 在组件卸载时解绑全局事件
437
460
  onUnmounted(() => {
438
461
  console.log(`${pageTitle.value}页面卸载`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wui-components-v2",
3
- "version": "1.2.76",
3
+ "version": "1.2.77",
4
4
  "description": "wui 组件库",
5
5
  "author": "wgxshh",
6
6
  "license": "MIT",