85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
package myPlant
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
filePb "sundynix-micro-go/app/file/rpc/file"
|
|
"sundynix-micro-go/app/plant/api/internal/svc"
|
|
)
|
|
|
|
// ========== 图片 DTO ==========
|
|
|
|
// ImageInfo 图片信息 DTO,匹配旧单体 Oss 结构
|
|
type ImageInfo struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Url string `json:"url"`
|
|
Tag string `json:"tag"`
|
|
Key string `json:"key"`
|
|
Suffix string `json:"suffix"`
|
|
MD5 string `json:"md5"`
|
|
CreatedAt string `json:"createdAt"`
|
|
UpdatedAt string `json:"updatedAt"`
|
|
CreatedAtStr string `json:"createdAtStr"`
|
|
}
|
|
|
|
// ========== 时间格式化 ==========
|
|
|
|
func FormatTime(t time.Time) string {
|
|
if t.IsZero() {
|
|
return ""
|
|
}
|
|
return t.Format(time.RFC3339)
|
|
}
|
|
|
|
func FormatTimeShort(t time.Time) string {
|
|
if t.IsZero() {
|
|
return ""
|
|
}
|
|
return t.Format("2006-01-02 15:04:05")
|
|
}
|
|
|
|
func UnixToTimeStr(ts int64) string {
|
|
if ts == 0 {
|
|
return ""
|
|
}
|
|
return time.Unix(ts, 0).Format(time.RFC3339)
|
|
}
|
|
|
|
func UnixToTimeStrShort(ts int64) string {
|
|
if ts == 0 {
|
|
return ""
|
|
}
|
|
return time.Unix(ts, 0).Format("2006-01-02 15:04:05")
|
|
}
|
|
|
|
// ========== 文件服务 RPC 封装 ==========
|
|
|
|
// FetchFilesByIds 通过 FileRpc 批量获取文件信息,返回 id->ImageInfo 映射
|
|
func FetchFilesByIds(ctx context.Context, svcCtx *svc.ServiceContext, ids []string) map[string]ImageInfo {
|
|
result := make(map[string]ImageInfo)
|
|
if len(ids) == 0 {
|
|
return result
|
|
}
|
|
resp, err := svcCtx.FileRpc.GetFilesByIds(ctx, &filePb.GetFilesByIdsReq{Ids: ids})
|
|
if err != nil || resp == nil {
|
|
return result
|
|
}
|
|
for _, f := range resp.Files {
|
|
result[f.Id] = ImageInfo{
|
|
ID: f.Id,
|
|
Name: f.Name,
|
|
Url: f.Url,
|
|
Tag: f.Tag,
|
|
Key: f.Key,
|
|
Suffix: f.Suffix,
|
|
MD5: f.Md5,
|
|
CreatedAt: UnixToTimeStr(f.CreatedAt),
|
|
UpdatedAt: UnixToTimeStr(f.CreatedAt),
|
|
CreatedAtStr: UnixToTimeStrShort(f.CreatedAt),
|
|
}
|
|
}
|
|
return result
|
|
}
|