来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
236 lines
7.3 KiB
TypeScript
236 lines
7.3 KiB
TypeScript
import { addChinaCalendarDays, getChinaDateString } from './datetime';
|
|
|
|
export type DashboardPeriodType =
|
|
| 'yesterday'
|
|
| 'day'
|
|
| 'week'
|
|
| 'first_half_month'
|
|
| 'second_half_month'
|
|
| 'month'
|
|
| 'quarter'
|
|
| 'half_year'
|
|
| 'year';
|
|
|
|
export const DEFAULT_DASHBOARD_PERIOD: DashboardPeriodType = 'day';
|
|
|
|
/** 页头快捷:当天 / 本周 / 本月(默认当天) */
|
|
export const DASHBOARD_PERIOD_QUICK_OPTIONS: Array<{
|
|
value: DashboardPeriodType;
|
|
label: string;
|
|
}> = [
|
|
{ value: 'day', label: '当天' },
|
|
{ value: 'week', label: '本周' },
|
|
{ value: 'month', label: '本月' },
|
|
];
|
|
|
|
export const DASHBOARD_PERIOD_OPTIONS: Array<{ value: DashboardPeriodType; label: string }> = [
|
|
{ value: 'yesterday', label: '昨天' },
|
|
{ value: 'day', label: '当天' },
|
|
{ value: 'week', label: '本周' },
|
|
{ value: 'first_half_month', label: '上半月' },
|
|
{ value: 'second_half_month', label: '下半月' },
|
|
{ value: 'month', label: '本月' },
|
|
{ value: 'quarter', label: '一季度' },
|
|
{ value: 'half_year', label: '半年' },
|
|
{ value: 'year', label: '一年' },
|
|
];
|
|
|
|
export interface DashboardPeriodBounds {
|
|
start: Date;
|
|
end: Date;
|
|
label: string;
|
|
}
|
|
|
|
export function isDashboardPeriodType(value: string): value is DashboardPeriodType {
|
|
return DASHBOARD_PERIOD_OPTIONS.some((option) => option.value === value);
|
|
}
|
|
|
|
/** 看板基准日输入框用 YYYY-MM-DD(中国时区) */
|
|
export function formatDashboardAsOfInput(date: Date = new Date()): string {
|
|
return getChinaDateString(date);
|
|
}
|
|
|
|
const MAX_CUSTOM_RANGE_DAYS = 366;
|
|
|
|
/** 自定义闭区间预览(中国日历,与后端口径一致) */
|
|
export function getDashboardCustomPeriodBounds(startInput: Date, endInput: Date): DashboardPeriodBounds {
|
|
let dateFrom = getChinaDateString(startInput);
|
|
let dateTo = getChinaDateString(endInput);
|
|
if (dateFrom > dateTo) {
|
|
const tmp = dateFrom;
|
|
dateFrom = dateTo;
|
|
dateTo = tmp;
|
|
}
|
|
const days =
|
|
Math.floor(
|
|
(new Date(`${dateTo}T12:00:00+08:00`).getTime() -
|
|
new Date(`${dateFrom}T12:00:00+08:00`).getTime()) /
|
|
86400000
|
|
) + 1;
|
|
if (days > MAX_CUSTOM_RANGE_DAYS) {
|
|
throw new Error(`自定义区间不能超过 ${MAX_CUSTOM_RANGE_DAYS} 天`);
|
|
}
|
|
return {
|
|
start: new Date(`${dateFrom}T00:00:00+08:00`),
|
|
end: new Date(`${dateTo}T23:59:59.999+08:00`),
|
|
label: `${dateFrom} — ${dateTo}`,
|
|
};
|
|
}
|
|
|
|
export function parseDashboardDateOnly(value: string): Date | null {
|
|
const raw = String(value || '').trim();
|
|
if (!/^\d{4}-\d{2}-\d{2}$/.test(raw)) return null;
|
|
const parsed = new Date(`${raw}T12:00:00+08:00`);
|
|
return Number.isNaN(parsed.getTime()) ? null : parsed;
|
|
}
|
|
|
|
function chinaParts(ymd: string): { y: number; m: number; d: number } {
|
|
const [y, m, d] = ymd.split('-').map(Number);
|
|
return { y, m, d };
|
|
}
|
|
|
|
function formatYmdLabel(ymd: string): string {
|
|
const { y, m, d } = chinaParts(ymd);
|
|
return `${y}年${m}月${d}日`;
|
|
}
|
|
|
|
function formatRangeLabelYmd(from: string, to: string): string {
|
|
const a = chinaParts(from);
|
|
const b = chinaParts(to);
|
|
const left = `${a.y}年${a.m}月${a.d}日`;
|
|
const right = a.y === b.y ? `${b.m}月${b.d}日` : `${b.y}年${b.m}月${b.d}日`;
|
|
return `${left} — ${right}`;
|
|
}
|
|
|
|
function getDashboardPeriodBoundsNatural(
|
|
period: DashboardPeriodType,
|
|
ref: Date = new Date()
|
|
): { dateFrom: string; dateTo: string; label: string } {
|
|
const today = getChinaDateString(ref);
|
|
const { y, m } = chinaParts(today);
|
|
let dateFrom: string;
|
|
let dateTo: string;
|
|
let label: string;
|
|
|
|
switch (period) {
|
|
case 'yesterday': {
|
|
const ymd = addChinaCalendarDays(today, -1);
|
|
dateFrom = ymd;
|
|
dateTo = ymd;
|
|
label = formatYmdLabel(ymd);
|
|
break;
|
|
}
|
|
case 'day': {
|
|
dateFrom = today;
|
|
dateTo = today;
|
|
label = formatYmdLabel(today);
|
|
break;
|
|
}
|
|
case 'week': {
|
|
const weekday = new Date(`${today}T12:00:00+08:00`).getUTCDay();
|
|
const diffToMonday = weekday === 0 ? 6 : weekday - 1;
|
|
dateFrom = addChinaCalendarDays(today, -diffToMonday);
|
|
dateTo = addChinaCalendarDays(dateFrom, 6);
|
|
const a = chinaParts(dateFrom);
|
|
const b = chinaParts(dateTo);
|
|
label = `${a.y}年${a.m}/${a.d}-${b.m}/${b.d}`;
|
|
break;
|
|
}
|
|
case 'first_half_month':
|
|
dateFrom = `${y}-${String(m).padStart(2, '0')}-01`;
|
|
dateTo = `${y}-${String(m).padStart(2, '0')}-15`;
|
|
label = `${y}年${m}月上半月`;
|
|
break;
|
|
case 'second_half_month': {
|
|
dateFrom = `${y}-${String(m).padStart(2, '0')}-16`;
|
|
const next = m === 12 ? { y: y + 1, m: 1 } : { y, m: m + 1 };
|
|
dateTo = addChinaCalendarDays(`${next.y}-${String(next.m).padStart(2, '0')}-01`, -1);
|
|
label = `${y}年${m}月下半月`;
|
|
break;
|
|
}
|
|
case 'month': {
|
|
dateFrom = `${y}-${String(m).padStart(2, '0')}-01`;
|
|
const next = m === 12 ? { y: y + 1, m: 1 } : { y, m: m + 1 };
|
|
dateTo = addChinaCalendarDays(`${next.y}-${String(next.m).padStart(2, '0')}-01`, -1);
|
|
label = `${y}年${m}月`;
|
|
break;
|
|
}
|
|
case 'quarter': {
|
|
const qStartM = Math.floor((m - 1) / 3) * 3 + 1;
|
|
const qEndM = qStartM + 2;
|
|
dateFrom = `${y}-${String(qStartM).padStart(2, '0')}-01`;
|
|
const next = qEndM === 12 ? { y: y + 1, m: 1 } : { y, m: qEndM + 1 };
|
|
dateTo = addChinaCalendarDays(`${next.y}-${String(next.m).padStart(2, '0')}-01`, -1);
|
|
label = `${y}年第${Math.floor((m - 1) / 3) + 1}季度`;
|
|
break;
|
|
}
|
|
case 'half_year': {
|
|
const half = m <= 6 ? 0 : 1;
|
|
dateFrom = `${y}-${half === 0 ? '01' : '07'}-01`;
|
|
dateTo = half === 0 ? `${y}-06-30` : `${y}-12-31`;
|
|
label = `${y}年${half === 0 ? '上半年' : '下半年'}`;
|
|
break;
|
|
}
|
|
case 'year':
|
|
dateFrom = `${y}-01-01`;
|
|
dateTo = `${y}-12-31`;
|
|
label = `${y}年`;
|
|
break;
|
|
default: {
|
|
dateFrom = `${y}-${String(m).padStart(2, '0')}-01`;
|
|
const next = m === 12 ? { y: y + 1, m: 1 } : { y, m: m + 1 };
|
|
dateTo = addChinaCalendarDays(`${next.y}-${String(next.m).padStart(2, '0')}-01`, -1);
|
|
label = `${y}年${m}月`;
|
|
}
|
|
}
|
|
|
|
return { dateFrom, dateTo, label };
|
|
}
|
|
|
|
function clipPeriodEndToAsOf(
|
|
dateFrom: string,
|
|
dateTo: string,
|
|
asOfYmd: string,
|
|
naturalLabel: string
|
|
): { dateFrom: string; dateTo: string; label: string } {
|
|
let to = dateTo;
|
|
const from = dateFrom;
|
|
if (asOfYmd < from) {
|
|
to = from;
|
|
} else if (to > asOfYmd) {
|
|
to = asOfYmd;
|
|
}
|
|
const clipped = to < dateTo || asOfYmd < dateFrom;
|
|
const label = clipped ? formatRangeLabelYmd(from, to) : naturalLabel;
|
|
return { dateFrom: from, dateTo: to, label };
|
|
}
|
|
|
|
export function getDashboardPeriodBounds(
|
|
period: DashboardPeriodType,
|
|
ref: Date = new Date()
|
|
): DashboardPeriodBounds {
|
|
const asOfYmd = getChinaDateString(ref);
|
|
const natural = getDashboardPeriodBoundsNatural(period, ref);
|
|
const clipped = clipPeriodEndToAsOf(natural.dateFrom, natural.dateTo, asOfYmd, natural.label);
|
|
return {
|
|
start: new Date(`${clipped.dateFrom}T00:00:00+08:00`),
|
|
end: new Date(`${clipped.dateTo}T23:59:59.999+08:00`),
|
|
label: clipped.label,
|
|
};
|
|
}
|
|
|
|
export function getDashboardPeriodStatPrefix(period: DashboardPeriodType): string {
|
|
const map: Record<DashboardPeriodType, string> = {
|
|
yesterday: '昨日',
|
|
day: '当日',
|
|
week: '本周',
|
|
first_half_month: '上半月',
|
|
second_half_month: '下半月',
|
|
month: '本月',
|
|
quarter: '本季度',
|
|
half_year: '本期',
|
|
year: '本年',
|
|
};
|
|
return map[period];
|
|
}
|