yueyang 685ff90b36 hotfix: 修复OA任务时间与页面刷新入口 (#790)
恢复钉钉北京时间显示,并将页面刷新请求接到实际控制器。
2026-10-30 15:43:09 +08:00

92 lines
2.2 KiB
PHP

<?php
namespace Modules\EcommerceFinance\Support;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Throwable;
final class OaProcessTime
{
public static function timezone(): string
{
$timezone = trim((string) config('app.platform_timezone', ''));
return $timezone !== '' ? $timezone : 'Asia/Shanghai';
}
public static function now(): CarbonImmutable
{
return CarbonImmutable::now(self::timezone());
}
public static function parse(mixed $value): ?CarbonImmutable
{
$text = trim((string) $value);
if ($text === '') {
return null;
}
try {
return CarbonImmutable::parse($text, self::timezone())
->setTimezone(self::timezone());
} catch (Throwable) {
return null;
}
}
public static function parseDingTalkTask(mixed $value): ?CarbonImmutable
{
$text = trim((string) $value);
if ($text === '') {
return null;
}
try {
return CarbonImmutable::parse(
str_replace('T', ' ', preg_replace('/Z$/', '', $text)),
self::timezone(),
);
} catch (Throwable) {
return null;
}
}
public static function formatDateTime(mixed $value): string
{
return self::parse($value)?->format('Y-m-d H:i') ?? '-';
}
public static function formatWaiting(mixed $value, ?CarbonInterface $now = null): string
{
$startedAt = self::parse($value);
if ($startedAt === null) {
return '-';
}
$nowAt = $now === null
? self::now()
: CarbonImmutable::instance($now)->setTimezone(self::timezone());
if ($startedAt->greaterThan($nowAt)) {
return '-';
}
$hours = intdiv($startedAt->diffInSeconds($nowAt), 3600);
if ($hours < 1) {
return '不足1小时';
}
$days = intdiv($hours, 24);
$remainingHours = $hours % 24;
$parts = [];
if ($days > 0) {
$parts[] = $days . '天';
}
if ($remainingHours > 0) {
$parts[] = $remainingHours . '小时';
}
return implode('', $parts);
}
}