feat(product): return product time fields as millisecond timestamp strings

This commit is contained in:
BXY0414 2026-07-13 14:12:05 +08:00
parent 2fa8872603
commit ae940225c4
2 changed files with 26 additions and 19 deletions

View File

@ -101,7 +101,7 @@ IDS 电商矩阵中的**商品域微服务**,负责 SPU/SKU 主数据、分类
|---|---|---|---| |---|---|---|---|
| `product_id` | `string` | 是 | 商品 SPU ID | | `product_id` | `string` | 是 | 商品 SPU ID |
响应 `product` 包含 SPU 基础信息、SKU 列表、`created_at`、`updated_at`。 响应 `product` 包含 SPU 基础信息、SKU 列表、`created_at`、`updated_at`。时间字段统一返回 13 位 Unix 毫秒时间戳字符串,前端展示时按 `Asia/Shanghai` 渲染。
### `ListCategoryProducts` ### `ListCategoryProducts`
@ -121,6 +121,8 @@ IDS 电商矩阵中的**商品域微服务**,负责 SPU/SKU 主数据、分类
| `total` | `int64` | 分类/分组下商品总数 | | `total` | `int64` | 分类/分组下商品总数 |
| `total_pages` | `int32` | 总页数 | | `total_pages` | `int32` | 总页数 |
`products[].created_at`、`products[].updated_at` 统一返回 13 位 Unix 毫秒时间戳字符串,前端展示时按 `Asia/Shanghai` 渲染。
### `ListCategoryTree` ### `ListCategoryTree`
| 字段 | 类型 | 必填 | 说明 | | 字段 | 类型 | 必填 | 说明 |

View File

@ -450,6 +450,18 @@ func timestampToTime(value *timestamppb.Timestamp) *time.Time {
return &t return &t
} }
func timeToMillisString(value time.Time) string {
return strconv.FormatInt(value.UnixMilli(), 10)
}
func timePtrToMillisString(value *time.Time) *string {
millis := ""
if value != nil {
millis = timeToMillisString(*value)
}
return &millis
}
func validatePositiveID(raw, field string) error { func validatePositiveID(raw, field string) error {
if raw == "" { if raw == "" {
return errors.New(field + " is required") return errors.New(field + " is required")
@ -485,9 +497,9 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco
V2: sku.V2, V2: sku.V2,
K3: sku.K3, K3: sku.K3,
V3: sku.V3, V3: sku.V3,
DeletedAt: timestampOrNil(sku.DeletedAt), DeletedAt: timePtrToMillisString(sku.DeletedAt),
CreatedAt: timestamppb.New(sku.CreatedAt), CreatedAt: timeToMillisString(sku.CreatedAt),
UpdatedAt: timestamppb.New(sku.UpdatedAt), UpdatedAt: timeToMillisString(sku.UpdatedAt),
Inventory: toProtoInventory(sku.Inventory), Inventory: toProtoInventory(sku.Inventory),
}) })
} }
@ -499,8 +511,8 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco
SellingPoint: product.SellingPoint, SellingPoint: product.SellingPoint,
Status: product.Status, Status: product.Status,
Skus: protoSKUs, Skus: protoSKUs,
CreatedAt: timestamppb.New(product.CreatedAt), CreatedAt: timeToMillisString(product.CreatedAt),
UpdatedAt: timestamppb.New(product.UpdatedAt), UpdatedAt: timeToMillisString(product.UpdatedAt),
ImageIds: product.ImageIDs, ImageIds: product.ImageIDs,
BrandId: product.BrandID, BrandId: product.BrandID,
SupplierId: product.SupplierID, SupplierId: product.SupplierID,
@ -514,8 +526,8 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco
FreightFixedAmount: product.FreightFixedAmount, FreightFixedAmount: product.FreightFixedAmount,
FreightTemplateId: product.FreightTemplateID, FreightTemplateId: product.FreightTemplateID,
SaleTimeType: product.SaleTimeType, SaleTimeType: product.SaleTimeType,
ScheduledSaleTime: timestampOrNil(product.ScheduledSaleTime), ScheduledSaleTime: timePtrToMillisString(product.ScheduledSaleTime),
DeletedAt: timestampOrNil(product.DeletedAt), DeletedAt: timePtrToMillisString(product.DeletedAt),
} }
} }
@ -532,8 +544,8 @@ func toProtoCategoryProduct(product *repository.CategoryProductRecord, skus []re
}) })
} }
return &productv1.CategoryProduct{ return &productv1.CategoryProduct{
UpdatedAt: timestamppb.New(product.UpdatedAt), UpdatedAt: timeToMillisString(product.UpdatedAt),
CreatedAt: timestamppb.New(product.CreatedAt), CreatedAt: timeToMillisString(product.CreatedAt),
Title: product.Title, Title: product.Title,
ProductId: product.ID, ProductId: product.ID,
SpuNo: product.SPUNo, SpuNo: product.SPUNo,
@ -553,18 +565,11 @@ func toProtoInventory(inventory *repository.InventoryRecord) *productv1.ProductI
AvailableQuantity: inventory.AvailableQuantity, AvailableQuantity: inventory.AvailableQuantity,
LockedQuantity: inventory.LockedQuantity, LockedQuantity: inventory.LockedQuantity,
SoldQuantity: inventory.SoldQuantity, SoldQuantity: inventory.SoldQuantity,
CreatedAt: timestamppb.New(inventory.CreatedAt), CreatedAt: timeToMillisString(inventory.CreatedAt),
UpdatedAt: timestamppb.New(inventory.UpdatedAt), UpdatedAt: timeToMillisString(inventory.UpdatedAt),
} }
} }
func timestampOrNil(value *time.Time) *timestamppb.Timestamp {
if value == nil {
return nil
}
return timestamppb.New(*value)
}
func buildCategoryTree(categories []*repository.CategoryRecord) []*productv1.CategoryNode { func buildCategoryTree(categories []*repository.CategoryRecord) []*productv1.CategoryNode {
nodes := make(map[string]*productv1.CategoryNode, len(categories)) nodes := make(map[string]*productv1.CategoryNode, len(categories))
records := make(map[string]*repository.CategoryRecord, len(categories)) records := make(map[string]*repository.CategoryRecord, len(categories))