mirror of
https://gitee.com/ulthon/ulthon_information.git
synced 2026-07-01 16:12: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
|
||||
|
||||
@@ -122,6 +122,7 @@
|
||||
var vditor = new Vditor('editor-text-area', {
|
||||
height: 'auto',
|
||||
cache: { enable: false },
|
||||
cdn: '/static/lib/vditor',
|
||||
mode: 'wysiwyg',
|
||||
toolbar: [
|
||||
'headings', 'bold', 'italic', 'strike', '|',
|
||||
|
||||
@@ -273,7 +273,7 @@ X-API-Key: {api_key}</div>
|
||||
<tr><td>content_type</td><td>string</td><td>否</td><td>内容类型: "html"(默认) 或 "markdown"<br/>当为 "markdown" 时,系统会自动将 content 转换为 HTML 存储到 content_html 字段</td></tr>
|
||||
<tr><td>categorys</td><td>array</td><td>否</td><td>分类 ID 数组,如 [1, 2]</td></tr>
|
||||
<tr><td>tags</td><td>array</td><td>否</td><td>标签 ID 数组,如 [1, 2]</td></tr>
|
||||
<tr><td>publish_time</td><td>string</td><td>否</td><td>发布时间,格式为 Y-m-d H:i:s,如 2024-01-15 14:30:00</td></tr>
|
||||
<tr><td>publish_time</td><td>string</td><td>否</td><td>发布时间,格式为 Y-m-d H:i:s,如 2024-01-15 14:30:00;当 status=1 且未传或传空字符串时,系统自动使用当前时间</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p style="color:#888;margin-top:8px;"><strong>注:</strong><code>create_time</code> 和 <code>update_time</code> 由系统自动生成,客户端无需设置也无法修改。</p>
|
||||
@@ -287,6 +287,7 @@ X-API-Key: {api_key}</div>
|
||||
"title": "新文章标题",
|
||||
"content": "文章内容",
|
||||
"status": 1,
|
||||
"publish_time": "2024-01-15 14:30:00",
|
||||
"categorys": [1, 2],
|
||||
"tags": [3]
|
||||
}'</div>
|
||||
|
||||
Reference in New Issue
Block a user