接入前端P0页面真实接口
This commit is contained in:
123
client/src/utils/reservationFieldRules.ts
Normal file
123
client/src/utils/reservationFieldRules.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import type { ReservationRecord, ReservationTaskFieldResult } from '@/types/reservation'
|
||||
|
||||
const truthyRuleValues = new Set(['Y', 'YES', 'TRUE', '1', '是', '可编辑', 'ENABLED'])
|
||||
const falsyRuleValues = new Set(['N', 'NO', 'FALSE', '0', '否', '不可编辑', 'DISABLED', '隐藏'])
|
||||
|
||||
export interface FieldAreaGroup {
|
||||
area: string
|
||||
fields: ReservationTaskFieldResult[]
|
||||
}
|
||||
|
||||
export interface EnumOption {
|
||||
value: string
|
||||
label: string
|
||||
}
|
||||
|
||||
export function groupReservationFields(fields: ReservationTaskFieldResult[], fallbackArea: string): FieldAreaGroup[] {
|
||||
const grouped = new Map<string, ReservationTaskFieldResult[]>()
|
||||
fields.filter(isReservationFieldVisible).forEach((field) => {
|
||||
const area = field.display_area?.trim() || fallbackArea
|
||||
grouped.set(area, [...(grouped.get(area) ?? []), field])
|
||||
})
|
||||
|
||||
return Array.from(grouped.entries()).map(([area, areaFields]) => ({
|
||||
area,
|
||||
fields: areaFields.sort((left, right) => (left.row_number ?? 0) - (right.row_number ?? 0)),
|
||||
}))
|
||||
}
|
||||
|
||||
export function isReservationFieldVisible(field: ReservationTaskFieldResult): boolean {
|
||||
return !isRuleFalsy(field.visible)
|
||||
}
|
||||
|
||||
export function isReservationFieldRequired(field: ReservationTaskFieldResult): boolean {
|
||||
return isRuleTruthy(field.required_rule)
|
||||
}
|
||||
|
||||
export function isReservationFieldEditable(field: ReservationTaskFieldResult, readOnly: boolean): boolean {
|
||||
if (readOnly) {
|
||||
return false
|
||||
}
|
||||
return (
|
||||
isRuleTruthy(field.editable) ||
|
||||
isRuleTruthy(field.input_editable) ||
|
||||
isRuleTruthy(field.select_editable) ||
|
||||
isRuleTruthy(field.date_picker) ||
|
||||
isRuleTruthy(field.number_input) ||
|
||||
isRuleTruthy(field.table_editable)
|
||||
)
|
||||
}
|
||||
|
||||
export function isSelectField(field: ReservationTaskFieldResult): boolean {
|
||||
return isRuleTruthy(field.select_editable) && parseEnumOptions(field.enum_options).length > 0
|
||||
}
|
||||
|
||||
export function isDateField(field: ReservationTaskFieldResult): boolean {
|
||||
return isRuleTruthy(field.date_picker)
|
||||
}
|
||||
|
||||
export function isNumberField(field: ReservationTaskFieldResult): boolean {
|
||||
return isRuleTruthy(field.number_input)
|
||||
}
|
||||
|
||||
export function isFileField(field: ReservationTaskFieldResult): boolean {
|
||||
return isRuleTruthy(field.file_display)
|
||||
}
|
||||
|
||||
export function isTableField(field: ReservationTaskFieldResult): boolean {
|
||||
return isRuleTruthy(field.table_editable)
|
||||
}
|
||||
|
||||
export function parseEnumOptions(enumOptions: string | null): EnumOption[] {
|
||||
if (!enumOptions?.trim()) {
|
||||
return []
|
||||
}
|
||||
|
||||
return enumOptions
|
||||
.split(/[,,;;/]/)
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean)
|
||||
.map((item) => {
|
||||
const separatorIndex = item.indexOf(':')
|
||||
const value = separatorIndex >= 0 ? item.slice(0, separatorIndex) : item
|
||||
const label = separatorIndex >= 0 ? item.slice(separatorIndex + 1) : value
|
||||
return {
|
||||
value: value.trim(),
|
||||
label: label.trim(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function buildInitialFieldValues(fields: ReservationTaskFieldResult[], payload: ReservationRecord | null): ReservationRecord {
|
||||
return fields.reduce<ReservationRecord>((values, field) => {
|
||||
values[field.field_path] = payload?.[field.field_path] ?? field.value ?? ''
|
||||
return values
|
||||
}, {})
|
||||
}
|
||||
|
||||
export function stringifyReservationValue(value: unknown, emptyLabel: string): string {
|
||||
if (value === null || value === undefined || value === '') {
|
||||
return emptyLabel
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((item) => stringifyReservationValue(item, emptyLabel)).join(', ')
|
||||
}
|
||||
if (typeof value === 'object') {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
return String(value)
|
||||
}
|
||||
|
||||
function isRuleTruthy(value: string | null): boolean {
|
||||
if (!value) {
|
||||
return false
|
||||
}
|
||||
return truthyRuleValues.has(value.trim().toUpperCase()) || truthyRuleValues.has(value.trim())
|
||||
}
|
||||
|
||||
function isRuleFalsy(value: string | null): boolean {
|
||||
if (!value) {
|
||||
return false
|
||||
}
|
||||
return falsyRuleValues.has(value.trim().toUpperCase()) || falsyRuleValues.has(value.trim())
|
||||
}
|
||||
18
client/src/utils/reservationFormat.ts
Normal file
18
client/src/utils/reservationFormat.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export function formatReservationDateTime(value: string | null | undefined): string {
|
||||
if (!value) {
|
||||
return '-'
|
||||
}
|
||||
return value.replace('T', ' ').replace(/\+.*$/, '')
|
||||
}
|
||||
|
||||
export function formatReservationDateRange(arrivalDate: string, departureDate: string): string {
|
||||
return `${arrivalDate} / ${departureDate}`
|
||||
}
|
||||
|
||||
export function formatReservationCurrency(amount: number, currencyCode: string): string {
|
||||
return new Intl.NumberFormat('zh-CN', {
|
||||
style: 'currency',
|
||||
currency: currencyCode,
|
||||
maximumFractionDigits: 0,
|
||||
}).format(amount)
|
||||
}
|
||||
Reference in New Issue
Block a user