科目映射表操作记录:时间固定按平台时区,避免页面请求按公司时区写入导致差 7 小时
This commit is contained in:
parent
c7a8baea76
commit
7f3aeb233e
@ -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
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -1036,6 +1036,7 @@
|
||||
+ '<input type="search" id="amHistKeyword" placeholder="搜平台 / 账单项目 / 操作人">'
|
||||
+ '<button type="button" class="am-btn" id="amHistExport">导出记录 CSV</button>'
|
||||
+ '<span class="am-hist-meta" id="amHistMeta"></span>'
|
||||
+ '<span class="am-hist-meta">时间为北京时间</span>'
|
||||
+ '</div>'
|
||||
+ '<div class="am-hist-scroll"><table class="am-hist-table">'
|
||||
+ '<thead><tr><th style="width:9.5rem;">时间</th><th style="width:4.5rem;">操作</th>'
|
||||
|
||||
@ -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
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user