59e51671
“wangming”
1
|
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
const OFFSET_UNITS = new Set([
'Minutes',
'Hours',
'Days',
'Weeks',
'Months (30 Day)',
'Years',
])
export function applyOffsetToDate (base: Date, amount: number, unit: string): Date {
const d = new Date(base.getTime())
const u = String(unit ?? '').trim()
switch (u) {
case 'Minutes':
d.setMinutes(d.getMinutes() + amount)
break
case 'Hours':
d.setHours(d.getHours() + amount)
break
case 'Days':
d.setDate(d.getDate() + amount)
break
case 'Weeks':
d.setDate(d.getDate() + amount * 7)
break
case 'Months (30 Day)':
d.setDate(d.getDate() + amount * 30)
break
case 'Years':
d.setFullYear(d.getFullYear() + amount)
break
default:
d.setDate(d.getDate() + amount)
}
return d
}
function formatDateByPreset (format: string, date: Date): string {
const yyyy = String(date.getFullYear())
const yy = yyyy.slice(-2)
const mm = String(date.getMonth() + 1).padStart(2, '0')
const dd = String(date.getDate()).padStart(2, '0')
const hh = String(date.getHours()).padStart(2, '0')
const min = String(date.getMinutes()).padStart(2, '0')
|
fdce24a6
杨鑫
修改bug
|
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/** 已算好的日期/时间展示串(非 format 预设名、非 JSON 偏移) */
export function isLikelyResolvedDateTimeLiteral (raw: string | null | undefined): boolean {
const t = String(raw ?? '').trim()
if (!t || t.startsWith('{')) return false
if (OFFSET_UNITS.has(t)) return false
if (/^\d{4}-\d{2}-\d{2}/.test(t)) return true
if (/^\d{4}-\d{2}-\d{2}\s+\d{1,2}:\d{2}/.test(t)) return true
if (/^\d{1,2}\/\d{1,2}\/\d{2,4}/.test(t)) return true
if (/^\d{1,2}:\d{2}(:\d{2})?$/.test(t)) return true
return false
}
function applyElementPrefix (cfg: Record<string, unknown>, body: string): string {
const prefix = String(cfg.prefix ?? cfg.Prefix ?? '')
if (!prefix) return body
if (body.startsWith(prefix)) return body
return `${prefix}${body}`
}
function normalizeOffsetAmount (valueRaw: string): number | null {
const t = String(valueRaw ?? '').trim()
if (t === '') return 0
const n = Number(t)
return Number.isFinite(n) ? n : null
}
function dateFormatPatternForElement (
el: SystemTemplateElementBase,
cfg: Record<string, unknown>,
): string {
const type = String(el.type || '').toUpperCase()
if (type === 'TIME') return 'HH:mm'
const it = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase()
const raw =
(typeof cfg.format === 'string' && cfg.format.trim()
? cfg.format
: typeof cfg.Format === 'string' && cfg.Format.trim()
? cfg.Format
: it === 'datetime'
? 'YYYY-MM-DD HH:mm'
: 'DD/MM/YYYY') || 'DD/MM/YYYY'
if (isLikelyResolvedDateTimeLiteral(raw) || OFFSET_UNITS.has(raw)) {
return it === 'datetime' ? 'YYYY-MM-DD HH:mm' : 'DD/MM/YYYY'
}
return raw
}
function readOffsetFromElementConfig (
el: SystemTemplateElementBase,
): { amount: number; unit: string } | null {
const type = String(el.type || '').toUpperCase()
const cfg = (el.config || {}) as Record<string, unknown>
if (type === 'DURATION') {
const unit = String(cfg.format ?? cfg.Format ?? 'Days').trim() || 'Days'
const u = OFFSET_UNITS.has(unit) ? unit : 'Days'
const rawV =
cfg.durationValue ??
cfg.value ??
cfg.offsetDays ??
cfg.DurationValue ??
cfg.Value ??
cfg.OffsetDays
const val = Number(rawV)
return { amount: Number.isFinite(val) ? val : 0, unit: u }
}
const od = cfg.offsetDays ?? cfg.OffsetDays
if (od != null && String(od).trim() !== '') {
const n = Number(od)
if (Number.isFinite(n)) return { amount: n, unit: 'Days' }
}
const fmt = String(cfg.format ?? cfg.Format ?? '').trim()
if (fmt && OFFSET_UNITS.has(fmt)) {
const rawV = cfg.durationValue ?? cfg.value ?? cfg.DurationValue ?? cfg.Value ?? 0
const val = Number(rawV)
return { amount: Number.isFinite(val) ? val : 0, unit: fmt }
}
return null
}
function resolveFromOffsetAmount (
el: SystemTemplateElementBase,
amount: number,
unit: string,
base: Date,
): string {
const type = String(el.type || '').toUpperCase()
const cfg = (el.config || {}) as Record<string, unknown>
const d = applyOffsetToDate(base, amount, unit)
if (type === 'DATE') {
return applyElementPrefix(cfg, formatDateByPreset(dateFormatPatternForElement(el, cfg), d))
}
if (type === 'TIME') {
return applyElementPrefix(cfg, formatDateByPreset('HH:mm', d))
}
return applyElementPrefix(cfg, `${amount} ${unit}`.trim())
}
/**
* 画布预览 / 原生打印:按基准时刻解析 DATE/TIME/DURATION,忽略 AUTO_DB 上已过期的 text/format。
*/
export function resolveElementDateTimeDisplay (
el: SystemTemplateElementBase,
base: Date = new Date(),
): string | null {
const type = String(el.type || '').toUpperCase()
if (type !== 'DATE' && type !== 'TIME' && type !== 'DURATION') return null
const cfg = (el.config || {}) as Record<string, unknown>
const vst = String(el.valueSourceType ?? '').toUpperCase()
const inputType = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase()
const rawText = String(cfg.text ?? cfg.Text ?? '').trim()
const fromOffsetJson = (raw: string): string | null => {
const p = tryParsePrintInputOffsetStored(raw)
if (!p) return null
const amount = normalizeOffsetAmount(p.value)
if (amount === null) return null
return resolveFromOffsetAmount(el, amount, p.unit || 'Days', base)
}
const jsonResolved = (rawText ? fromOffsetJson(rawText) : null)
?? fromOffsetJson(String(cfg.format ?? cfg.Format ?? ''))
if (jsonResolved) return jsonResolved
if (vst === 'PRINT_INPUT') {
/** 保质期/Prep 等相对偏移字段:须按 base 重算,勿用接口首屏写入的过期 literal */
if (
rawText &&
!tryParsePrintInputOffsetStored(rawText) &&
!isUniAppDateTimeOffsetField(el)
) {
return applyElementPrefix(cfg, rawText)
}
if (
isUniAppDateTimeOffsetField(el) ||
inputType === 'date' ||
inputType === 'datetime' ||
type === 'TIME'
) {
const off = readOffsetFromElementConfig(el)
if (off) return resolveFromOffsetAmount(el, off.amount, off.unit, base)
/** 默认值已展开为字面日期时仍保留,避免丢失 durationdate 等 +N Days */
if (
rawText &&
!tryParsePrintInputOffsetStored(rawText) &&
isLikelyResolvedDateTimeLiteral(rawText) &&
!isUniAppDateTimeOffsetField(el)
) {
return applyElementPrefix(cfg, rawText)
}
return resolveFromOffsetAmount(el, 0, 'Days', base)
}
return null
}
/** FIXED 的 durationdate/durationtime 等:config.text 可能已是平台录入的偏移 JSON */
if (vst === 'FIXED' && isUniAppDateTimeOffsetField(el)) {
const off = readOffsetFromElementConfig(el)
if (off && (off.amount !== 0 || rawText)) {
return resolveFromOffsetAmount(el, off.amount, off.unit, base)
}
if (
rawText &&
!tryParsePrintInputOffsetStored(rawText) &&
isLikelyResolvedDateTimeLiteral(rawText)
) {
return applyElementPrefix(cfg, rawText)
}
return resolveFromOffsetAmount(el, 0, 'Days', base)
}
const off = readOffsetFromElementConfig(el)
if (off) return resolveFromOffsetAmount(el, off.amount, off.unit, base)
if (
rawText &&
!tryParsePrintInputOffsetStored(rawText) &&
isLikelyResolvedDateTimeLiteral(rawText) &&
isUniAppDateTimeOffsetField(el)
) {
return applyElementPrefix(cfg, rawText)
}
return resolveFromOffsetAmount(el, 0, 'Days', base)
}
|
fdce24a6
杨鑫
修改bug
|
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
export function applyLiveDateTimeFieldsToTemplate (
template: SystemLabelTemplate,
base: Date = new Date(),
): SystemLabelTemplate {
const elements = (template.elements || []).map((el) => {
const type = String(el.type || '').toUpperCase()
const needsLive =
type === 'DATE' ||
type === 'TIME' ||
type === 'DURATION' ||
isUniAppDateTimeOffsetField(el)
if (!needsLive) return el
const live = resolveElementDateTimeDisplay(el, base)
if (live == null || !String(live).trim()) return el
const cfg = { ...(el.config || {}) } as Record<string, unknown>
cfg.text = live
cfg.Text = live
return { ...el, config: cfg }
})
return { ...template, elements }
}
|