mirror of
https://gitee.com/ulthon/ulthon_information.git
synced 2026-07-01 18:02:47 +08:00
fix(API): 修复文章发布时 publish_time 自动填充逻辑
统一处理 publish_time 参数,空字符串按未传处理。在创建和更新文章时,当文章状态为发布且未提供 publish_time 时,自动填充当前时间。同时修复更新文章时,从草稿首次发布且原 publish_time 为 0 时自动填充时间的逻辑。
This commit is contained in:
@@ -116,11 +116,21 @@ class Articles extends BaseController
|
||||
$post_data['content_type'] = 'html';
|
||||
}
|
||||
|
||||
// 校验 publish_time 格式(必须是 Y-m-d H:i:s)
|
||||
if (isset($post_data['publish_time']) && !empty($post_data['publish_time'])) {
|
||||
// 统一处理 publish_time:空字符串按未传处理;发布状态未传时自动补当前时间
|
||||
if (array_key_exists('publish_time', $post_data)) {
|
||||
if (is_string($post_data['publish_time'])) {
|
||||
$post_data['publish_time'] = trim($post_data['publish_time']);
|
||||
}
|
||||
if ($post_data['publish_time'] === '') {
|
||||
unset($post_data['publish_time']);
|
||||
}
|
||||
}
|
||||
if (isset($post_data['publish_time'])) {
|
||||
if (!preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $post_data['publish_time'])) {
|
||||
return json_message('publish_time 格式错误,应为 Y-m-d H:i:s,如 2024-01-15 14:30:00');
|
||||
}
|
||||
} elseif ((string) $post_data['status'] === '1') {
|
||||
$post_data['publish_time'] = date('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
// content_type 白名单校验
|
||||
@@ -208,11 +218,26 @@ class Articles extends BaseController
|
||||
// 禁止客户端设置的时间字段
|
||||
unset($post_data['create_time'], $post_data['update_time']);
|
||||
|
||||
// 校验 publish_time 格式(必须是 Y-m-d H:i:s)
|
||||
if (isset($post_data['publish_time']) && !empty($post_data['publish_time'])) {
|
||||
// publish_time 处理:未传则不更新;仅草稿首次发布且原值为 0 时自动补当前时间
|
||||
$has_publish_time = array_key_exists('publish_time', $post_data);
|
||||
if ($has_publish_time && is_string($post_data['publish_time'])) {
|
||||
$post_data['publish_time'] = trim($post_data['publish_time']);
|
||||
}
|
||||
if ($has_publish_time && $post_data['publish_time'] === '') {
|
||||
unset($post_data['publish_time']);
|
||||
$has_publish_time = false;
|
||||
}
|
||||
if ($has_publish_time) {
|
||||
if (!preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $post_data['publish_time'])) {
|
||||
return json_message('publish_time 格式错误,应为 Y-m-d H:i:s,如 2024-01-15 14:30:00');
|
||||
}
|
||||
} elseif (
|
||||
array_key_exists('status', $post_data)
|
||||
&& (string) $model_post->getData('status') === '0'
|
||||
&& (string) $post_data['status'] === '1'
|
||||
&& (int) $model_post->getData('publish_time') === 0
|
||||
) {
|
||||
$post_data['publish_time'] = date('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
// categorys diff update
|
||||
|
||||
Reference in New Issue
Block a user