来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
207 lines
7.1 KiB
TypeScript
207 lines
7.1 KiB
TypeScript
const CHINA_TZ = 'Asia/Shanghai';
|
||
|
||
export type DateRangePreset = 'today' | 'week' | 'month' | 'quarter' | 'year' | 'day' | 'all' | 'custom';
|
||
|
||
/** 中国时区 YYYY-MM-DD */
|
||
export function getChinaDateString(date = new Date()): string {
|
||
return new Intl.DateTimeFormat('en-CA', { timeZone: CHINA_TZ }).format(date);
|
||
}
|
||
|
||
function getChinaDateParts(date = new Date()) {
|
||
const [y, m, d] = getChinaDateString(date).split('-').map(Number);
|
||
return { y, m, d };
|
||
}
|
||
|
||
function addChinaDays(dateStr: string, days: number): string {
|
||
const anchor = new Date(`${dateStr}T12:00:00+08:00`);
|
||
anchor.setUTCDate(anchor.getUTCDate() + days);
|
||
return getChinaDateString(anchor);
|
||
}
|
||
|
||
/** 中国日历日期加减天数 */
|
||
export function addChinaCalendarDays(dateStr: string, days: number): string {
|
||
return addChinaDays(normalizeDateInput(dateStr) || getChinaDateString(), days);
|
||
}
|
||
|
||
export function compareDateOnly(a: string, b: string): number {
|
||
const left = normalizeDateInput(a);
|
||
const right = normalizeDateInput(b);
|
||
if (!left || !right) return 0;
|
||
return left.localeCompare(right);
|
||
}
|
||
|
||
/** 下单日:优先取 created_at 的中国日历日期,否则为今天 */
|
||
export function getOrderDateFromCreatedAt(createdAt?: string | null): string {
|
||
if (!createdAt) return getChinaDateString();
|
||
const date = new Date(createdAt);
|
||
if (Number.isNaN(date.getTime())) return getChinaDateString();
|
||
return getChinaDateString(date);
|
||
}
|
||
|
||
export function getDefaultDeliveryDate(orderDate?: string | null): string {
|
||
return addChinaCalendarDays(orderDate || getChinaDateString(), 1);
|
||
}
|
||
|
||
export function assertDeliveryDateNotBefore(deliveryDate: string, orderDate: string): void {
|
||
const delivery = normalizeDateInput(deliveryDate);
|
||
const order = normalizeDateInput(orderDate);
|
||
if (!delivery || !order) return;
|
||
if (compareDateOnly(delivery, order) < 0) {
|
||
throw new Error(`配送日期不能早于下单日期(${formatChinaDate(order)})`);
|
||
}
|
||
}
|
||
|
||
/** 某下单日(中国日历)对应的 UTC 起止,用于 created_at 筛选 */
|
||
export function getChinaDayRange(dateStr: string): { start: string; end: string } {
|
||
const start = new Date(`${dateStr}T00:00:00+08:00`);
|
||
const end = new Date(`${dateStr}T23:59:59.999+08:00`);
|
||
return { start: start.toISOString(), end: end.toISOString() };
|
||
}
|
||
|
||
/** 起止日期(中国日历 YYYY-MM-DD)对应的 UTC 范围 */
|
||
export function getChinaRangeBetween(startDate: string, endDate: string): { start: string; end: string } {
|
||
return {
|
||
start: getChinaDayRange(startDate).start,
|
||
end: getChinaDayRange(endDate).end,
|
||
};
|
||
}
|
||
|
||
/** 今天 / 本周 / 本月 / 本季度 / 本年(中国时区,周一起算) */
|
||
export function getChinaPresetRange(
|
||
preset: Exclude<DateRangePreset, 'day' | 'all' | 'custom'>,
|
||
ref = new Date()
|
||
): { start: string; end: string } {
|
||
const bounds = getChinaPresetDayBounds(preset, ref);
|
||
return getChinaRangeBetween(bounds.start, bounds.end);
|
||
}
|
||
|
||
/** 预设对应的中国日历起止日(YYYY-MM-DD) */
|
||
export function getChinaPresetDayBounds(
|
||
preset: Exclude<DateRangePreset, 'day' | 'all' | 'custom'>,
|
||
ref = new Date()
|
||
): { start: string; end: string } {
|
||
const today = getChinaDateString(ref);
|
||
if (preset === 'today') {
|
||
return { start: today, end: today };
|
||
}
|
||
|
||
const { y, m } = getChinaDateParts(ref);
|
||
|
||
if (preset === 'week') {
|
||
const weekday = new Date(`${today}T12:00:00+08:00`).getUTCDay();
|
||
const diffToMonday = weekday === 0 ? 6 : weekday - 1;
|
||
const weekStart = addChinaDays(today, -diffToMonday);
|
||
const weekEnd = addChinaDays(weekStart, 6);
|
||
return { start: weekStart, end: weekEnd };
|
||
}
|
||
|
||
if (preset === 'month') {
|
||
const monthStart = `${y}-${String(m).padStart(2, '0')}-01`;
|
||
const nextMonth = m === 12 ? { y: y + 1, m: 1 } : { y, m: m + 1 };
|
||
const monthEnd = addChinaDays(
|
||
`${nextMonth.y}-${String(nextMonth.m).padStart(2, '0')}-01`,
|
||
-1
|
||
);
|
||
return { start: monthStart, end: monthEnd };
|
||
}
|
||
|
||
if (preset === 'quarter') {
|
||
const quarterStartMonth = Math.floor((m - 1) / 3) * 3 + 1;
|
||
const quarterEndMonth = quarterStartMonth + 2;
|
||
const quarterStart = `${y}-${String(quarterStartMonth).padStart(2, '0')}-01`;
|
||
const nextAfterQuarter =
|
||
quarterEndMonth === 12
|
||
? { y: y + 1, m: 1 }
|
||
: { y, m: quarterEndMonth + 1 };
|
||
const quarterEnd = addChinaDays(
|
||
`${nextAfterQuarter.y}-${String(nextAfterQuarter.m).padStart(2, '0')}-01`,
|
||
-1
|
||
);
|
||
return { start: quarterStart, end: quarterEnd };
|
||
}
|
||
|
||
const yearStart = `${y}-01-01`;
|
||
const yearEnd = addChinaDays(`${y + 1}-01-01`, -1);
|
||
return { start: yearStart, end: yearEnd };
|
||
}
|
||
|
||
/**
|
||
* 预设起点~今天(结束日不超过今天,也不早于起点)。
|
||
* 用于「指定日期」展开时按当前今天/本周/本月预填。
|
||
*/
|
||
export function getChinaPresetRangeToToday(
|
||
preset: Exclude<DateRangePreset, 'day' | 'all' | 'custom'>,
|
||
ref = new Date()
|
||
): { start: string; end: string } {
|
||
const today = getChinaDateString(ref);
|
||
const bounds = getChinaPresetDayBounds(preset, ref);
|
||
const end = bounds.end < today ? bounds.end : today;
|
||
const start = bounds.start > end ? end : bounds.start;
|
||
return { start, end };
|
||
}
|
||
|
||
export function resolveChinaCreatedAtRange(
|
||
preset: DateRangePreset,
|
||
dayValue: string
|
||
): { start: string; end: string } {
|
||
if (preset === 'all') {
|
||
return { start: '', end: '' };
|
||
}
|
||
if (preset === 'day') {
|
||
return getChinaDayRange(dayValue);
|
||
}
|
||
if (preset === 'custom') {
|
||
return { start: '', end: '' };
|
||
}
|
||
return getChinaPresetRange(preset);
|
||
}
|
||
|
||
export function formatChinaDateTime(value: string | null | undefined): string {
|
||
if (!value) return '-';
|
||
const date = new Date(value);
|
||
if (Number.isNaN(date.getTime())) return '-';
|
||
return new Intl.DateTimeFormat('zh-CN', {
|
||
timeZone: CHINA_TZ,
|
||
year: 'numeric',
|
||
month: '2-digit',
|
||
day: '2-digit',
|
||
hour: '2-digit',
|
||
minute: '2-digit',
|
||
second: '2-digit',
|
||
hour12: false,
|
||
}).format(date);
|
||
}
|
||
|
||
export function normalizeDateInput(value: string | null | undefined): string {
|
||
if (!value) return '';
|
||
const trimmed = String(value).trim();
|
||
// 纯日期:按日历日原样使用
|
||
if (/^\d{4}-\d{2}-\d{2}$/.test(trimmed)) return trimmed;
|
||
// ISO/时间戳:按中国时区日历日,禁止截 UTC 前缀(否则会少一天)
|
||
const date = new Date(trimmed);
|
||
if (Number.isNaN(date.getTime())) return '';
|
||
return getChinaDateString(date);
|
||
}
|
||
|
||
/** HH:MM 或 HH:MM:SS → HH:MM;非法返回 '' */
|
||
export function normalizeTimeInput(value: string | null | undefined): string {
|
||
if (!value) return '';
|
||
const trimmed = String(value).trim();
|
||
const m = /^(\d{1,2}):(\d{2})(?::\d{2})?$/.exec(trimmed);
|
||
if (!m) return '';
|
||
const h = Number(m[1]);
|
||
const min = Number(m[2]);
|
||
if (!Number.isFinite(h) || !Number.isFinite(min) || h < 0 || h > 23 || min < 0 || min > 59) {
|
||
return '';
|
||
}
|
||
return `${String(h).padStart(2, '0')}:${String(min).padStart(2, '0')}`;
|
||
}
|
||
|
||
export function formatChinaDate(value: string | null | undefined): string {
|
||
if (!value) return '-';
|
||
const normalized = normalizeDateInput(value);
|
||
if (!normalized) return '-';
|
||
const [y, m, d] = normalized.split('-').map(Number);
|
||
return `${y}年${m}月${d}日`;
|
||
}
|