Files
go_blog/ARTICLE_UPDATE_TIME_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

91 lines
2.7 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/pages/article.html`
- 修改了文章元信息显示部分(第24-33行)
- 将原来的单一时间戳改为显示两个时间:
- **发布时间**:文章首次发布的时间(`PublishedAt`
- **最后更新**:文章最后修改的时间(`UpdatedAt`
- 使用条件判断确保只在有值时显示
显示格式:
```
作者名 · 发布时间: 2024-01-15 10:30 · 最后更新: 2024-01-20 14:25
```
### 2. 后端处理器
#### `/handlers/home.go`
- 修改 `renderArticleDetail()` 函数(第189-201行):
- 添加 `data["UpdatedAt"]` 传递更新时间到模板
- 添加 `formatUpdateTime()` 函数(第213-216行):
- 格式化 `UpdatedAt` 字段为可读的时间字符串
- 格式:`2006-01-02 15:04`(年-月-日 时:分)
### 3. 国际化文本
#### `/i18n/i18n.go`
添加了翻译键:
- `article_last_updated`: "Last Updated" / "最后更新"
## 功能特性
1. **双时间戳显示**
- 发布时间(PublishedAt):文章首次发布的时间
- 最后更新(UpdatedAt):文章最后一次修改的时间
2. **自动更新**
- `UpdatedAt` 字段由 GORM 自动维护
- 每次调用 `Updates()``Save()` 时自动更新
3. **智能显示**
- 如果文章没有发布时间,不显示发布时间
- 始终显示最后更新时间
4. **时间格式**
- 统一使用 `YYYY-MM-DD HH:MM` 格式
- 清晰易读
## 显示效果
文章详情页顶部会显示:
### 英文界面
```
John Doe · Published: 2024-01-15 10:30 · Last Updated: 2024-01-20 14:25
```
### 中文界面
```
张三 · 发布时间: 2024-01-15 10:30 · 最后更新: 2024-01-20 14:25
```
## 技术实现
- **数据库字段**`updated_at` 字段是 GORM 的标准字段,类型为 `time.Time`
- **自动维护**:GORM 在每次更新记录时自动设置 `updated_at`
- **格式化**:使用 Go 的标准时间格式 `2006-01-02 15:04`
## 与发布时间编辑功能的配合
这个功能与之前实现的"自定义发布时间"功能完美配合:
- **发布时间**PublishedAt):可以由用户手动设置或自动生成
- **更新时间**(UpdatedAt):始终由系统自动维护,反映真实的修改时间
这样读者可以清楚地知道:
1. 文章最初是什么时候发布的
2. 文章最后一次修改是什么时候
## 用户价值
1. **内容时效性**:读者可以判断文章内容是否及时更新
2. **信息透明**:清楚显示文章的发布和修改历史
3. **信任度提升**:显示更新时间表明作者在持续维护内容