Files
go_blog/PUBLISHED_AT_FEATURE.md
T
kevinandClaude Fable 5 b0ca76e8dc feat: add custom publish time and last updated time display
- Add custom publish time field to article create/edit forms
  - Support datetime-local input for manual time setting
  - Auto-set on first publish if left blank
  - Works for both admin and user article forms

- Display last updated time on article detail page
  - Show both published time and last updated time
  - Auto-maintained by GORM on each update
  - Format: YYYY-MM-DD HH:MM

- Add i18n support for new fields
  - article_published_at: Published Time / 发布时间
  - article_published_at_hint: Leave blank to auto-set on publish / 留空则在发布时自动设置
  - article_last_updated: Last Updated / 最后更新

- Update handlers and templates
  - handlers/article.go: Add parsePublishedAt/formatPublishedAt functions
  - handlers/home.go: Add formatUpdateTime function
  - handlers/my_articles.go: Support custom publish time in user articles
  - templates: Add datetime-local inputs and time display

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-22 20:20:19 +08:00

94 lines
3.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 文章发布时间编辑功能
## 功能概述
为博客系统的文章编辑页面添加了自定义发布时间的功能,允许管理员和普通用户在创建或编辑文章时手动设置发布时间。
## 修改的文件
### 1. 前端模板
#### `/templates/admin/article_create.html`
- 在"置顶"选项前添加了发布时间输入框
- 使用 `datetime-local` 类型的输入框,支持选择日期和时间
- 添加提示文本:留空则在发布时自动设置
#### `/templates/user/my_article_form.html`
- 在状态选择框前添加了发布时间输入框
- 使用相同的 `datetime-local` 输入框
- 添加相应的提示文本
### 2. 后端处理器
#### `/handlers/article.go`
- 修改 `articleForm` 结构体,添加 `PublishedAt string` 字段
- 修改 `parseArticleForm()` 函数,解析表单中的 `published_at` 字段
- 修改 `applyFormToData()` 函数,将 `PublishedAt` 传递给模板
- 添加 `parsePublishedAt()` 函数:解析 datetime-local 格式的时间字符串
- 添加 `formatPublishedAt()` 函数:将时间格式化为 datetime-local 格式用于表单回显
- 修改 `ArticleEditPage()` 函数:在编辑页面回显发布时间
- 修改 `ArticleUpdate()` 函数:
- 如果用户提供了自定义发布时间,则使用该时间
- 否则保持原有逻辑(首次发布时自动设置当前时间)
- 修改 `ArticleCreate()` 函数:
- 如果用户提供了自定义发布时间,则使用该时间
- 否则在发布时自动设置当前时间
#### `/handlers/my_articles.go`
- 修改 `MyArticleEditPage()` 函数:在编辑页面回显发布时间
- 修改 `MyArticleUpdate()` 函数:
- 如果用户提供了自定义发布时间,则使用该时间
- 否则保持原有逻辑(首次发布时自动设置当前时间)
### 3. 国际化文本
#### `/i18n/i18n.go`
添加了以下翻译键:
- `article_published_at`: "Published Time" / "发布时间"
- `article_published_at_hint`: "Leave blank to auto-set on publish" / "留空则在发布时自动设置"
## 功能特性
1. **自定义发布时间**:用户可以手动设置文章的发布时间,适用于:
- 导入历史文章时保留原始发布时间
- 预设未来的发布时间(虽然文章会立即可见)
- 修正错误的发布时间
2. **自动时间戳**:如果用户不填写发布时间:
- 保存为草稿:不设置发布时间
- 首次发布:自动设置为当前时间
- 已发布文章再次编辑:保持原发布时间不变
3. **向后兼容**
- 现有的自动时间戳逻辑完全保留
- 只有在用户明确输入时间时才会覆盖自动时间
4. **双语支持**:中英文界面均已适配
## 使用方法
### 管理员编辑页面
1. 访问 `/admin/articles/:id/edit``/admin/articles/new`
2. 在"发布时间"字段中选择日期和时间
3. 留空则使用自动时间戳
### 普通用户编辑页面
1. 访问 `/my/articles/:id/edit``/my/articles/new`
2. 在"发布时间"字段中选择日期和时间
3. 留空则使用自动时间戳
## 技术实现
- **时间格式**:使用 HTML5 `datetime-local` 输入类型,格式为 `2006-01-02T15:04`
- **时区处理**:使用服务器的本地时区 (`time.Local`) 进行解析和格式化
- **数据库**`published_at` 字段类型为 `*time.Time`(可为空)
## 测试建议
1. 创建新文章时设置自定义发布时间
2. 创建新文章时留空发布时间(应自动设置)
3. 编辑已发布文章并修改发布时间
4. 编辑已发布文章但不修改发布时间(应保持原时间)
5. 将草稿改为发布状态(应自动设置发布时间或使用自定义时间)
6. 测试中英文界面的显示