From 7f3aeb233e7c862a7f6cf1d93f7b96b65f243cb5 Mon Sep 17 00:00:00 2001 From: yueyang Date: Tue, 29 Sep 2026 16:10:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=91=E7=9B=AE=E6=98=A0=E5=B0=84=E8=A1=A8?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E8=AE=B0=E5=BD=95=EF=BC=9A=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=9B=BA=E5=AE=9A=E6=8C=89=E5=B9=B3=E5=8F=B0=E6=97=B6=E5=8C=BA?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E9=A1=B5=E9=9D=A2=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=8C=89=E5=85=AC=E5=8F=B8=E6=97=B6=E5=8C=BA=E5=86=99=E5=85=A5?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=B7=AE=207=20=E5=B0=8F=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/app.php | 12 +++++++ .../Services/AccountMappingStore.php | 29 ++++++++++++++-- .../financial-rules/account-mapping.blade.php | 1 + .../Business/AccountMappingPageTest.php | 33 +++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) diff --git a/config/app.php b/config/app.php index 71ba6c27..d935a57c 100644 --- a/config/app.php +++ b/config/app.php @@ -92,6 +92,18 @@ return [ 'timezone' => env('APP_TIMEZONE', 'UTC'), + /* + |-------------------------------------------------------------------------- + | 平台固定时区 + |-------------------------------------------------------------------------- + | + | app.timezone 会被 App\Utilities\Overrider 按公司设置 localisation.timezone 覆盖, + | 没有该设置时回落到 Europe/London,导致页面请求写入的时间比北京时间晚 7 小时。 + | 需要稳定时间的地方(如科目映射表的操作记录)用这个值,不随公司设置变化。 + | + */ + 'platform_timezone' => env('APP_TIMEZONE', 'UTC'), + /* |-------------------------------------------------------------------------- | Application Locale Configuration diff --git a/modules/EcommerceFinance/Services/AccountMappingStore.php b/modules/EcommerceFinance/Services/AccountMappingStore.php index 9e8e505f..8f2ce45c 100644 --- a/modules/EcommerceFinance/Services/AccountMappingStore.php +++ b/modules/EcommerceFinance/Services/AccountMappingStore.php @@ -3,6 +3,7 @@ namespace Modules\EcommerceFinance\Services; use Illuminate\Support\Facades\File; +use Illuminate\Support\Carbon; use RuntimeException; /** @@ -15,6 +16,10 @@ use RuntimeException; * 当前落盘在 storage/app/account-mapping/rules.json(持久卷,重启不丢), * 首次访问会用 resources/data/account-mapping.json 作为初始 112 条规则灌入。 * 若要变成正式的数据表,只需替换本类的读写实现,页面与接口不用动。 + * + * 时间统一按 config('app.platform_timezone')(= APP_TIMEZONE)落盘,不能用 now(): + * 页面请求会被 App\Utilities\Overrider 按公司设置覆盖 app.timezone,ak_settings 里没有 + * localisation.timezone 时回落成 Europe/London,会让记录时间比北京时间晚 7 小时。 */ class AccountMappingStore { @@ -341,6 +346,26 @@ class AccountMappingStore return array_slice(array_reverse($events), 0, $limit); } + /** + * 记录时间用的“现在”:固定按平台时区,不受页面请求里公司设置覆盖的影响。 + * + * 直接用 now() 的话,页面操作写进日志的时间会差 7 小时(Europe/London)。 + */ + private function logNow(): Carbon + { + $timezone = trim((string) config('app.platform_timezone', '')); + + if ($timezone === '') { + $timezone = (string) config('app.timezone', 'UTC'); + } + + try { + return Carbon::now($timezone); + } catch (\Throwable) { + return Carbon::now(config('app.timezone', 'UTC')); + } + } + private function isEmptyRow(array $values): bool { return trim((string) ($values['platform'] ?? '')) === '' @@ -364,7 +389,7 @@ class AccountMappingStore } $json = json_encode( - ['updated_at' => now()->toDateTimeString(), 'rules' => array_values($rules)], + ['updated_at' => $this->logNow()->toDateTimeString(), 'rules' => array_values($rules)], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ); @@ -381,7 +406,7 @@ class AccountMappingStore $events = is_array($payload['events'] ?? null) ? $payload['events'] : []; $events[] = array_merge([ - 'at' => now()->toDateTimeString(), + 'at' => $this->logNow()->toDateTimeString(), 'action' => $action, 'id' => $id, 'before' => $before, diff --git a/resources/views/business/pages/financial-rules/account-mapping.blade.php b/resources/views/business/pages/financial-rules/account-mapping.blade.php index d51e0c75..16d25f40 100644 --- a/resources/views/business/pages/financial-rules/account-mapping.blade.php +++ b/resources/views/business/pages/financial-rules/account-mapping.blade.php @@ -1036,6 +1036,7 @@ + '' + '' + '' + + '时间为北京时间' + '' + '
' + '' diff --git a/tests/Feature/Business/AccountMappingPageTest.php b/tests/Feature/Business/AccountMappingPageTest.php index 55d8d093..83a43b81 100644 --- a/tests/Feature/Business/AccountMappingPageTest.php +++ b/tests/Feature/Business/AccountMappingPageTest.php @@ -294,6 +294,39 @@ class AccountMappingPageTest extends FeatureTestCase $this->assertSame('自测备注', $store->find($id)['memo']); } + /** 恢复:记录已经变化(位置对不上)时要拒绝,避免恢复错行 */ + public function test_history_timestamps_use_platform_timezone(): void + { + $this->grantAdminPermission(); + + // 页面请求会被 App\Utilities\Overrider 按公司设置覆盖 app.timezone(实例上回落成 + // Europe/London),记录时间不能跟着跑偏,否则日志会差 7 小时。 + $originalAppTimezone = config('app.timezone'); + $originalPhpTimezone = date_default_timezone_get(); + + try { + config(['app.timezone' => 'Europe/London']); + date_default_timezone_set('Europe/London'); + + $this->seedHistoryRows(false); + + $event = $this->loginAs()->getJson($this->actionUrl('history'))->json('events')[0]; + + $platformTimezone = (string) config('app.platform_timezone', 'UTC'); + $recorded = \Illuminate\Support\Carbon::parse($event['at'], $platformTimezone); + $expected = \Illuminate\Support\Carbon::now($platformTimezone); + + $this->assertLessThan( + 180, + abs($recorded->getTimestamp() - $expected->getTimestamp()), + '操作记录的时间没有按平台时区落盘(页面请求的时区覆盖会把它带偏)' + ); + } finally { + config(['app.timezone' => $originalAppTimezone]); + date_default_timezone_set($originalPhpTimezone); + } + } + /** 恢复:记录已经变化(位置对不上)时要拒绝,避免恢复错行 */ public function test_history_restore_rejects_stale_target(): void {
时间操作