plan-import-date-value.js
3.2 KB
1
2
3
4
5
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
/**
* 计划到店导入洽谈日期标量包装 — F3-4-1 未来日期可提交,确认选日期后恢复 maxDate 校验
* @file 纯值模型,无外部 import(避免与 datetime-picker-mode 循环依赖)
*/
/** 与 car-brand-value 共用字面量,不 import 以免小程序 require 环 */
const PARTIAL_FROM_PLAN_IMPORT_KEY = 'partialFromPlanImport'
/** 洽谈明细 F3-4 洽谈日期字段 key(回显还原标记用) */
export const PLAN_IMPORT_DATE_FIELD_KEY = 'F3-4-1'
/**
* @param {string} value
* @returns {Date|null}
*/
function parsePlanImportDateLocal(value) {
if (value == null || value === '') {
return null
}
const str = String(value).trim()
const match = str.match(/^(\d{4})-(\d{2})-(\d{2})(?:\s+(\d{2}):(\d{2}))?/)
if (!match) {
const date = new Date(value)
return Number.isNaN(date.getTime()) ? null : date
}
return new Date(
Number(match[1]),
Number(match[2]) - 1,
Number(match[3]),
Number(match[4] || 0),
Number(match[5] || 0),
0,
0
)
}
/**
* 包装导入日期:保留邀约未来日期,提交时仅校验必填
* @param {string} dateStr YYYY-MM-DD 或 YYYY-MM-DD HH:mm
* @returns {{ value: string, partialFromPlanImport: true }}
*/
export function wrapPlanImportDate(dateStr) {
const value = dateStr == null ? '' : String(dateStr).trim()
return {
value,
[PARTIAL_FROM_PLAN_IMPORT_KEY]: true
}
}
/**
* 标量或包装值 → 日期字符串
* @param {*} raw
* @returns {string}
*/
export function unwrapPlanImportDate(raw) {
if (raw == null || raw === '') {
return ''
}
if (typeof raw === 'string') {
return raw.trim()
}
if (typeof raw === 'object' && !Array.isArray(raw) && raw.value != null) {
return String(raw.value).trim()
}
return String(raw).trim()
}
/**
* 是否为计划到店导入的洽谈日期(未确认选日期前可豁免 maxDate)
* @param {*} raw
* @returns {boolean}
*/
export function hasPlanImportDateMarker(raw) {
return (
raw != null &&
typeof raw === 'object' &&
!Array.isArray(raw) &&
raw[PARTIAL_FROM_PLAN_IMPORT_KEY] === true &&
raw.value != null
)
}
/**
* 清除导入标记(用户确认选日期后调用,此后走 maxDate: today)
* @param {*} raw
* @returns {string}
*/
export function clearPlanImportDateMarker(raw) {
if (!hasPlanImportDateMarker(raw)) {
return unwrapPlanImportDate(raw)
}
return unwrapPlanImportDate(raw)
}
/**
* 详情回显:已保存的未来 F3-4-1 自动还原导入豁免标记
* @param {string} fieldKey
* @param {*} raw
* @returns {*}
*/
export function maybeRestorePlanImportDateMarker(fieldKey, raw) {
if (fieldKey !== PLAN_IMPORT_DATE_FIELD_KEY) {
return raw
}
if (hasPlanImportDateMarker(raw)) {
return raw
}
const scalar = unwrapPlanImportDate(raw)
if (!scalar) {
return raw
}
const picked = parsePlanImportDateLocal(scalar)
if (!picked) {
return raw
}
const today = new Date()
today.setHours(23, 59, 59, 999)
if (picked.getTime() > today.getTime()) {
return wrapPlanImportDate(scalar)
}
return raw
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
PLAN_IMPORT_DATE_FIELD_KEY,
wrapPlanImportDate,
unwrapPlanImportDate,
hasPlanImportDateMarker,
clearPlanImportDateMarker,
maybeRestorePlanImportDateMarker
}
}