后台页面优化及bug修复

This commit is contained in:
Dev
2026-09-24 22:02:06 +08:00
parent 36fb0ab1ee
commit 543d52bffa
20 changed files with 127 additions and 105 deletions
+70 -2
View File
@@ -29,9 +29,77 @@ class Logs extends Model
];
protected $type = [
'before_data' => 'array',
'after_data' => 'array',
];
// JSON 字段统一处理:读取时输出 JSON 字符串,写入时接受数组或 JSON 字符串
public function getBeforeDataAttr($value)
{
return $this->stringifyJsonData($value);
}
public function getAfterDataAttr($value)
{
return $this->stringifyJsonData($value);
}
public function setBeforeDataAttr($value)
{
return $this->normalizeJsonData($value);
}
public function setAfterDataAttr($value)
{
return $this->normalizeJsonData($value);
}
/**
* 将数据库值转换为可展示的 JSON 字符串
*/
protected function stringifyJsonData($value)
{
$flag = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT;
if (is_array($value)) {
return empty($value) ? '' : json_encode($value, $flag);
}
$value = $value === null ? '' : trim((string)$value);
if ($value === '') {
return '';
}
$decoded = json_decode($value, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return $value;
}
return json_encode($decoded, $flag);
}
/**
* 将输入(数组或 JSON 字符串)规范化为存储的 JSON 字符串
*/
protected function normalizeJsonData($value)
{
$flag = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
if (is_array($value)) {
return json_encode($value, $flag);
}
$value = $value === null ? '' : trim((string)$value);
if ($value === '') {
return '';
}
$decoded = json_decode($value, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return json_encode([$value], $flag);
}
return json_encode($decoded, $flag);
}