230 lines
5.9 KiB
Go
230 lines
5.9 KiB
Go
package playletser
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func historyPageURL(pageNum int) string {
|
|
if pageNum <= 1 {
|
|
return "/history"
|
|
}
|
|
return "/history/page/" + strconv.Itoa(pageNum)
|
|
}
|
|
|
|
func HistoryCanonicalPath(q HistoryListQuery) string {
|
|
q.Normalize()
|
|
return historyPageURL(q.PageNum)
|
|
}
|
|
|
|
// BuildHistoryPagination 观看历史分页。
|
|
func BuildHistoryPagination(q HistoryListQuery, totalCount int64) PaginationVM {
|
|
q.Normalize()
|
|
return BuildPagination(func(p int) string {
|
|
return historyPageURL(p)
|
|
}, q.PageNum, q.PageSize, totalCount)
|
|
}
|
|
|
|
// ParseHistoryListQuery 从 query 解析分页。
|
|
func ParseHistoryListQuery(pageStr string) HistoryListQuery {
|
|
q := HistoryListQuery{PageSize: defaultHistoryPageSize}
|
|
if n, err := strconv.Atoi(strings.TrimSpace(pageStr)); err == nil {
|
|
q.PageNum = n
|
|
}
|
|
q.Normalize()
|
|
return q
|
|
}
|
|
|
|
func historyFinished(item PlayletHistoryData) bool {
|
|
if item.IsFinished {
|
|
return true
|
|
}
|
|
return item.TotalEpisode > 0 && item.UnwatchedCount <= 0 && item.WatchedCount > 0
|
|
}
|
|
|
|
// historyStatus 卡片筛选状态:finished / unfinished。
|
|
func historyStatus(item PlayletHistoryData) string {
|
|
if historyFinished(item) {
|
|
return "finished"
|
|
}
|
|
return "unfinished"
|
|
}
|
|
|
|
// historyProgressText PC 卡片未看文案,使用接口 unwatchedCount / isFinished。
|
|
func historyProgressText(item PlayletHistoryData) string {
|
|
if historyFinished(item) {
|
|
return "已看完"
|
|
}
|
|
if item.UnwatchedCount > 0 {
|
|
return fmt.Sprintf("%d集未看", item.UnwatchedCount)
|
|
}
|
|
if item.LastEpisode > 0 {
|
|
return fmt.Sprintf("看到第%d集", item.LastEpisode)
|
|
}
|
|
return "未看完"
|
|
}
|
|
|
|
// historyWatchLine H5 进度行。
|
|
func historyWatchLine(item PlayletHistoryData) string {
|
|
if historyFinished(item) {
|
|
if item.LastEpisode > 0 {
|
|
return fmt.Sprintf("第%d集 · 已看完", item.LastEpisode)
|
|
}
|
|
return "已看完"
|
|
}
|
|
ep := "未开始"
|
|
if item.LastEpisode > 0 {
|
|
ep = fmt.Sprintf("第%d集", item.LastEpisode)
|
|
}
|
|
if item.UnwatchedCount > 0 {
|
|
return ep + fmt.Sprintf(" · 还有%d集未看", item.UnwatchedCount)
|
|
}
|
|
return ep + " · 未看完"
|
|
}
|
|
|
|
// historyTagText 历史卡片标签:仅展示 TagNames 第一项,无标签时不显示。
|
|
func historyTagText(item PlayletHistoryData) string {
|
|
return firstName(item.TagNames)
|
|
}
|
|
|
|
// historyPlayURL 继续播放:优先最近观看剧集。
|
|
func historyPlayURL(item PlayletHistoryData) string {
|
|
return chapterPlayURL(item.Slug, item.ID, item.LastChapterID)
|
|
}
|
|
|
|
// historyContinueMeta 首页「继续观看」文案:看至第 N 集 / 剩 M 集。
|
|
func historyContinueMeta(item PlayletHistoryData) string {
|
|
if item.LastEpisode <= 0 {
|
|
return "观看记录"
|
|
}
|
|
meta := fmt.Sprintf("看至第 %d 集", item.LastEpisode)
|
|
if item.UnwatchedCount > 0 {
|
|
meta += fmt.Sprintf(" 剩 %d 集", item.UnwatchedCount)
|
|
}
|
|
return meta
|
|
}
|
|
|
|
// historyProgressPct 观看进度百分比,供卡片 --p 使用。
|
|
func historyProgressPct(item PlayletHistoryData) string {
|
|
progress := 0
|
|
if item.TotalEpisode > 0 {
|
|
progress = item.WatchedCount * 100 / item.TotalEpisode
|
|
}
|
|
if progress < 0 {
|
|
progress = 0
|
|
}
|
|
if progress > 100 {
|
|
progress = 100
|
|
}
|
|
return fmt.Sprintf("%d%%", progress)
|
|
}
|
|
|
|
// FormatHistoryAt 最近观看时间展示。
|
|
func FormatHistoryAt(raw string) string {
|
|
return formatHistoryAt(raw)
|
|
}
|
|
|
|
// FormatHistoryListTime 观看历史列表时间:今天/昨天带时刻,更早仅日期。
|
|
func FormatHistoryListTime(raw string) string {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return ""
|
|
}
|
|
t, ok := parseHistoryTime(raw)
|
|
if !ok {
|
|
return raw
|
|
}
|
|
now := time.Now()
|
|
startToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
startThat := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
|
dayDiff := int(startToday.Sub(startThat).Hours() / 24)
|
|
hm := t.Format("15:04")
|
|
switch {
|
|
case dayDiff <= 0:
|
|
return "今天 " + hm
|
|
case dayDiff == 1:
|
|
return "昨天 " + hm
|
|
default:
|
|
return fmt.Sprintf("%d 月 %d 日", int(t.Month()), t.Day())
|
|
}
|
|
}
|
|
|
|
// HistoryDayBucket 列表分组标题:今天 / 昨天 / 更早。
|
|
func HistoryDayBucket(raw string) string {
|
|
raw = strings.TrimSpace(raw)
|
|
t, ok := parseHistoryTime(raw)
|
|
if !ok {
|
|
return "更早"
|
|
}
|
|
now := time.Now()
|
|
startToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
startThat := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
|
dayDiff := int(startToday.Sub(startThat).Hours() / 24)
|
|
switch {
|
|
case dayDiff <= 0:
|
|
return "今天"
|
|
case dayDiff == 1:
|
|
return "昨天"
|
|
default:
|
|
return "更早"
|
|
}
|
|
}
|
|
|
|
func formatHistoryAt(raw string) string {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return ""
|
|
}
|
|
t, ok := parseHistoryTime(raw)
|
|
if !ok {
|
|
return raw
|
|
}
|
|
now := time.Now()
|
|
startToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
startThat := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
|
dayDiff := int(startToday.Sub(startThat).Hours() / 24)
|
|
hm := t.Format("15:04")
|
|
switch {
|
|
case dayDiff <= 0:
|
|
return "今天 " + hm
|
|
case dayDiff == 1:
|
|
return "昨天 " + hm
|
|
case dayDiff == 2:
|
|
return "前天 " + hm
|
|
case dayDiff < 7:
|
|
return fmt.Sprintf("%d天前 %s", dayDiff, hm)
|
|
case dayDiff < 14:
|
|
return "一周前 " + hm
|
|
default:
|
|
return fmt.Sprintf("%d 月 %d 日", int(t.Month()), t.Day())
|
|
}
|
|
}
|
|
|
|
func parseHistoryTime(raw string) (time.Time, bool) {
|
|
layouts := []string{
|
|
time.RFC3339Nano,
|
|
time.RFC3339,
|
|
"2006-01-02 15:04:05",
|
|
"2006-01-02T15:04:05",
|
|
"2006-01-02T15:04:05Z07:00",
|
|
"2006-01-02 15:04:05.000",
|
|
"2006-01-02 15:04:05.999999999",
|
|
}
|
|
for _, layout := range layouts {
|
|
if t, err := time.ParseInLocation(layout, raw, time.Local); err == nil {
|
|
return t, true
|
|
}
|
|
if t, err := time.Parse(layout, raw); err == nil {
|
|
return t.In(time.Local), true
|
|
}
|
|
}
|
|
return time.Time{}, false
|
|
}
|
|
|
|
// ParseHistoryTime 解析观看历史时间字符串。
|
|
func ParseHistoryTime(raw string) (time.Time, bool) {
|
|
return parseHistoryTime(strings.TrimSpace(raw))
|
|
}
|