85 lines
2.5 KiB
Plaintext
85 lines
2.5 KiB
Plaintext
import { BookingDetail, Day, Slot } from './types.uts'
|
|
|
|
export function formatDateText(date: string): string {
|
|
if (date.length == 0) return ''
|
|
const parts = date.split('-')
|
|
if (parts.length < 3) return date
|
|
return parseInt(parts[1]).toString() + '月' + parseInt(parts[2]).toString() + '日'
|
|
}
|
|
|
|
export function maskPhone(phone: string): string {
|
|
if (phone.length < 7) return phone
|
|
return phone.slice(0, 3) + ' **** ' + phone.slice(phone.length - 4)
|
|
}
|
|
|
|
export function pad(value: number): string {
|
|
if (value < 10) return '0' + value.toString()
|
|
return value.toString()
|
|
}
|
|
|
|
export function formatMonth(date: Date): string {
|
|
return date.getFullYear().toString() + '-' + pad(date.getMonth() + 1)
|
|
}
|
|
|
|
export function formatDate(date: Date): string {
|
|
return date.getFullYear().toString() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate())
|
|
}
|
|
|
|
export function timestampFromDate(dateText: string): number {
|
|
if (dateText.length == 0) return 0
|
|
const parts = dateText.split('-')
|
|
if (parts.length < 3) return 0
|
|
return new Date(parseInt(parts[0]), parseInt(parts[1]) - 1, parseInt(parts[2])).getTime()
|
|
}
|
|
|
|
export function normalizeSlots(slots: Slot[]): Slot[] {
|
|
const result: Slot[] = []
|
|
for (let i = 0; i < slots.length; i += 1) {
|
|
const slot = slots[i]
|
|
result.push({
|
|
id: slot.id,
|
|
start_time: slot.start_time,
|
|
status: 'available',
|
|
statusText: '可约'
|
|
} as Slot)
|
|
}
|
|
return result
|
|
}
|
|
|
|
export function bookingStatusText(status: string): string {
|
|
if (status == 'pending') return '待确认'
|
|
if (status == 'confirmed') return '已预约'
|
|
if (status == 'cancelled_by_user') return '已取消'
|
|
if (status == 'cancelled_by_operator') return '已取消'
|
|
if (status == 'completed') return '已完成'
|
|
if (status == 'expired') return '已失效'
|
|
return status
|
|
}
|
|
|
|
export function normalizeBookings(bookings: BookingDetail[]): BookingDetail[] {
|
|
const result: BookingDetail[] = []
|
|
for (let i = 0; i < bookings.length; i += 1) {
|
|
const item = bookings[i]
|
|
const status = item.booking.status
|
|
result.push({
|
|
booking: item.booking,
|
|
project: item.project,
|
|
day: item.day,
|
|
slot: item.slot,
|
|
key: item.booking.id,
|
|
dateText: formatDateText(item.day.date),
|
|
statusText: bookingStatusText(status),
|
|
canCancel: status == 'pending' || status == 'confirmed'
|
|
} as BookingDetail)
|
|
}
|
|
return result
|
|
}
|
|
|
|
export function emptyDay(): Day {
|
|
return { date: '', status: '', slots: [] } as Day
|
|
}
|
|
|
|
export function emptySlot(): Slot {
|
|
return { id: '', start_time: '', status: '' } as Slot
|
|
}
|