diff --git a/README.md b/README.md index 3ff3888..54c47c2 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ IDS 电商矩阵中的**商品域微服务**,负责 SPU/SKU 主数据、分类 |---|---|---|---| | `product_id` | `string` | 是 | 商品 SPU ID | -响应 `product` 包含 SPU 基础信息、SKU 列表、`created_at`、`updated_at`。 +响应 `product` 包含 SPU 基础信息、SKU 列表、`created_at`、`updated_at`。时间字段统一返回 13 位 Unix 毫秒时间戳字符串,前端展示时按 `Asia/Shanghai` 渲染。 ### `ListCategoryProducts` @@ -121,6 +121,8 @@ IDS 电商矩阵中的**商品域微服务**,负责 SPU/SKU 主数据、分类 | `total` | `int64` | 分类/分组下商品总数 | | `total_pages` | `int32` | 总页数 | +`products[].created_at`、`products[].updated_at` 统一返回 13 位 Unix 毫秒时间戳字符串,前端展示时按 `Asia/Shanghai` 渲染。 + ### `ListCategoryTree` | 字段 | 类型 | 必填 | 说明 | diff --git a/internal/server/product_server.go b/internal/server/product_server.go index 29716c1..d5e73c7 100644 --- a/internal/server/product_server.go +++ b/internal/server/product_server.go @@ -450,6 +450,18 @@ func timestampToTime(value *timestamppb.Timestamp) *time.Time { 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 { if raw == "" { return errors.New(field + " is required") @@ -485,9 +497,9 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco V2: sku.V2, K3: sku.K3, V3: sku.V3, - DeletedAt: timestampOrNil(sku.DeletedAt), - CreatedAt: timestamppb.New(sku.CreatedAt), - UpdatedAt: timestamppb.New(sku.UpdatedAt), + DeletedAt: timePtrToMillisString(sku.DeletedAt), + CreatedAt: timeToMillisString(sku.CreatedAt), + UpdatedAt: timeToMillisString(sku.UpdatedAt), Inventory: toProtoInventory(sku.Inventory), }) } @@ -499,8 +511,8 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco SellingPoint: product.SellingPoint, Status: product.Status, Skus: protoSKUs, - CreatedAt: timestamppb.New(product.CreatedAt), - UpdatedAt: timestamppb.New(product.UpdatedAt), + CreatedAt: timeToMillisString(product.CreatedAt), + UpdatedAt: timeToMillisString(product.UpdatedAt), ImageIds: product.ImageIDs, BrandId: product.BrandID, SupplierId: product.SupplierID, @@ -514,8 +526,8 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco FreightFixedAmount: product.FreightFixedAmount, FreightTemplateId: product.FreightTemplateID, SaleTimeType: product.SaleTimeType, - ScheduledSaleTime: timestampOrNil(product.ScheduledSaleTime), - DeletedAt: timestampOrNil(product.DeletedAt), + ScheduledSaleTime: timePtrToMillisString(product.ScheduledSaleTime), + DeletedAt: timePtrToMillisString(product.DeletedAt), } } @@ -532,8 +544,8 @@ func toProtoCategoryProduct(product *repository.CategoryProductRecord, skus []re }) } return &productv1.CategoryProduct{ - UpdatedAt: timestamppb.New(product.UpdatedAt), - CreatedAt: timestamppb.New(product.CreatedAt), + UpdatedAt: timeToMillisString(product.UpdatedAt), + CreatedAt: timeToMillisString(product.CreatedAt), Title: product.Title, ProductId: product.ID, SpuNo: product.SPUNo, @@ -553,18 +565,11 @@ func toProtoInventory(inventory *repository.InventoryRecord) *productv1.ProductI AvailableQuantity: inventory.AvailableQuantity, LockedQuantity: inventory.LockedQuantity, SoldQuantity: inventory.SoldQuantity, - CreatedAt: timestamppb.New(inventory.CreatedAt), - UpdatedAt: timestamppb.New(inventory.UpdatedAt), + CreatedAt: timeToMillisString(inventory.CreatedAt), + 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 { nodes := make(map[string]*productv1.CategoryNode, len(categories)) records := make(map[string]*repository.CategoryRecord, len(categories))