mockDynamicDetail.js
4.11 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const { BOUTIQUE_PAGE_CONTEXT } = require('../fixtures/mock-meta.js')
const detailStore = new Map()
const DEFAULT_REPORT_TEMPLATE_ID = 10001
function formatDate(date) {
const y = date.getFullYear()
const m = `${date.getMonth() + 1}`.padStart(2, '0')
const d = `${date.getDate()}`.padStart(2, '0')
return `${y}-${m}-${d}`
}
function getStoreKey({ merchantId, staffUuid, reportDate }) {
return `${merchantId || '0'}:${staffUuid || '0'}:${reportDate}`
}
function getBoutiqueSampleValues() {
return {
'F1-1': '1',
'F1-4': 'mock 精品回显备注',
'F1-5-1': 2,
'F1-5-2': 3,
'F1-5-3': 1,
'F1-6-1': 4,
'F1-6-2': 5,
'F1-6-3': 6,
'F1-7-1': 7,
'F1-7-2': 8,
'F1-7-3': 9,
'F1-8-1': 10,
'F1-8-2': 11,
'F1-8-3': 12,
'F1-9-1': 13,
'F1-9-2': 14,
'F1-9-3': 15,
'F1-10-1': 16,
'F1-10-2': 17
}
}
function valuesToFieldDataList(values = {}, schemaLabels = {}) {
return Object.keys(values).map(fieldKey => ({
fieldKey,
fieldName: schemaLabels[fieldKey] || fieldKey,
fieldType: fieldKey === 'F1-1' ? 'radio' : fieldKey === 'F1-4' ? 'textarea' : 'numberInput',
fieldValue: values[fieldKey]
}))
}
function fieldDataListToValues(fieldDataList = []) {
const values = {}
fieldDataList.forEach(item => {
if (item?.fieldKey !== undefined && item?.fieldKey !== null && item.fieldKey !== '') {
values[item.fieldKey] = item.fieldValue
}
})
return values
}
function initSampleDetail() {
const today = new Date()
const day = today.getDay()
const mondayOffset = day === 0 ? -6 : 1 - day
const monday = new Date(today)
monday.setDate(today.getDate() + mondayOffset)
const mondayStr = formatDate(monday)
const sampleKey = getStoreKey({
merchantId: 'mock',
staffUuid: 'mock',
reportDate: mondayStr
})
if (!detailStore.has(sampleKey)) {
const values = getBoutiqueSampleValues()
detailStore.set(sampleKey, {
reportTemplateId: DEFAULT_REPORT_TEMPLATE_ID,
dailyReportRecordId: 90001,
baseSceneCode: BOUTIQUE_PAGE_CONTEXT.baseSceneCode,
sceneType: BOUTIQUE_PAGE_CONTEXT.sceneType,
fieldDataList: valuesToFieldDataList(values)
})
}
}
function delay(data, ms = 80) {
return new Promise(resolve => {
setTimeout(() => resolve(data), ms)
})
}
initSampleDetail()
function fetchMockDynamicDetail(params = {}) {
const { merchantId, staffUuid, reportDate, reportTemplateId } = params
const storeKey = getStoreKey({ merchantId, staffUuid, reportDate })
const record = detailStore.get(storeKey)
if (!record) {
return delay({
code: 0,
message: 'ok',
data: {
reportTemplateId: reportTemplateId || DEFAULT_REPORT_TEMPLATE_ID,
dailyReportRecordId: null,
baseSceneCode: BOUTIQUE_PAGE_CONTEXT.baseSceneCode,
sceneType: BOUTIQUE_PAGE_CONTEXT.sceneType,
fieldDataList: [],
jsonContent: ''
}
})
}
return delay({
code: 0,
message: 'ok',
data: {
reportTemplateId: record.reportTemplateId || reportTemplateId || DEFAULT_REPORT_TEMPLATE_ID,
dailyReportRecordId: record.dailyReportRecordId || null,
baseSceneCode: record.baseSceneCode ?? BOUTIQUE_PAGE_CONTEXT.baseSceneCode,
sceneType: record.sceneType ?? BOUTIQUE_PAGE_CONTEXT.sceneType,
fieldDataList: record.fieldDataList || [],
jsonContent: record.jsonContent || ''
}
})
}
function saveMockDynamicDetail(params = {}) {
const { merchantId, staffUuid, reportDate, reportTemplateId, dailyReportRecordId, fieldDataList, jsonContent } =
params
if (!reportDate) return null
const storeKey = getStoreKey({ merchantId, staffUuid, reportDate })
const prev = detailStore.get(storeKey) || {}
const nextRecord = {
reportTemplateId: reportTemplateId || prev.reportTemplateId || DEFAULT_REPORT_TEMPLATE_ID,
dailyReportRecordId: dailyReportRecordId || prev.dailyReportRecordId || Date.now(),
baseSceneCode: prev.baseSceneCode ?? BOUTIQUE_PAGE_CONTEXT.baseSceneCode,
sceneType: prev.sceneType ?? BOUTIQUE_PAGE_CONTEXT.sceneType,
fieldDataList: fieldDataList || prev.fieldDataList || [],
jsonContent: jsonContent || prev.jsonContent || ''
}
detailStore.set(storeKey, nextRecord)
return nextRecord
}
module.exports = {
fetchMockDynamicDetail,
saveMockDynamicDetail,
DEFAULT_REPORT_TEMPLATE_ID,
fieldDataListToValues
}