feat: query uploaded Shopee image URL
This commit is contained in:
@@ -9,8 +9,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go-admin/app/admin/models"
|
||||
"go-admin/app/admin/service"
|
||||
"go-admin/config"
|
||||
"go-admin/internal/imagestore"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var allowedImageContentTypes = map[string]bool{
|
||||
@@ -31,6 +34,43 @@ func (e ShopeeProduct) UploadImage(c *gin.Context) {
|
||||
e.uploadImage(c, strings.TrimSpace(c.Param("shopeeId")))
|
||||
}
|
||||
|
||||
// GetUploadedImage returns the URL generated by this project's image upload
|
||||
// endpoint. It deliberately reads only erpgo's image record: querying a URL
|
||||
// must not trigger a Huohanhan detail call or a Shopee synchronization.
|
||||
func (e ShopeeProduct) GetUploadedImage(c *gin.Context) {
|
||||
e.MakeContext(c)
|
||||
if !ValidateAPIKey(c) {
|
||||
e.Error(http.StatusUnauthorized, fmt.Errorf("invalid api key"), "未授权")
|
||||
return
|
||||
}
|
||||
shopeeID := strings.TrimSpace(c.Param("shopeeId"))
|
||||
if err := imagestore.ValidateShopeeID(shopeeID); err != nil {
|
||||
e.Error(http.StatusBadRequest, err, "参数错误")
|
||||
return
|
||||
}
|
||||
db, err := imageDB()
|
||||
if err != nil {
|
||||
e.Error(http.StatusInternalServerError, err, "数据库初始化失败")
|
||||
return
|
||||
}
|
||||
var row models.ShopeeProductImage
|
||||
if err := db.Where("shopee_id = ?", shopeeID).First(&row).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
e.Error(http.StatusNotFound, err, "未找到已上传图片")
|
||||
return
|
||||
}
|
||||
e.Error(http.StatusInternalServerError, err, "读取图片记录失败")
|
||||
return
|
||||
}
|
||||
base, _ := service.ResolveImageBaseURL(db, config.ExtConfig.Image.BaseURL)
|
||||
e.OK(gin.H{
|
||||
"shopeeId": row.ShopeeID,
|
||||
"url": service.ImageURL(base, row.ShopeeID, row.UpdatedAt),
|
||||
"fileSize": row.FileSize,
|
||||
"updatedAt": row.UpdatedAt,
|
||||
}, "查询成功")
|
||||
}
|
||||
|
||||
// UploadImageAdmin is the JWT-authenticated upload used by the Admin page.
|
||||
func (e ShopeeProduct) UploadImageAdmin(c *gin.Context) {
|
||||
e.MakeContext(c)
|
||||
|
||||
@@ -17,6 +17,7 @@ func registerShopeeProductRouter(v1 *gin.RouterGroup) {
|
||||
v1.GET("/shopee/product/spec/:shopeeId", a.GetSpecByShopeeID)
|
||||
v1.PUT("/shopee/products/:shopeeId/title", a.UpdateTitle)
|
||||
v1.PUT("/shopee/products/:shopeeId/cover", a.UpdateCover)
|
||||
v1.GET("/shopee/products/:shopeeId/image", a.GetUploadedImage)
|
||||
v1.POST("/shopee/products/:shopeeId/image", a.UploadImage)
|
||||
v1.POST("/shopee/products/:shopeeId/sync", a.SyncByShopeeID)
|
||||
v1.POST("/shopee/products/:shopeeId/sync-wait", a.SyncWaitByShopeeID)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
@baseUrl = http://127.0.0.1:8020/api/v1
|
||||
@shopeeId = 24720421377
|
||||
@apiKey = replace-with-api-key
|
||||
|
||||
### 查询由 POST /image 上传并保存的图片 URL
|
||||
GET {{baseUrl}}/shopee/products/{{shopeeId}}/image?apikey={{apiKey}}
|
||||
@@ -0,0 +1,21 @@
|
||||
# LOCAL-020:查询已上传 Shopee 商品图片 URL
|
||||
|
||||
## 目标
|
||||
|
||||
为 `POST /api/v1/shopee/products/{shopeeId}/image` 上传服务增加同路径的查询接口:
|
||||
|
||||
`GET /api/v1/shopee/products/{shopeeId}/image`
|
||||
|
||||
接口只查询 erpgo 保存的上传图片记录,不读取 Huohanhan,也不触发 Shopee 同步。
|
||||
|
||||
## 验收标准
|
||||
|
||||
- API Key 鉴权方式与上传接口一致。
|
||||
- 根据 Shopee ID 返回最近上传图片的可访问 URL、上传时间和文件大小。
|
||||
- 返回 URL 与上传接口一致,包含更新时间版本参数,避免浏览器/CDN 使用旧缓存。
|
||||
- Shopee ID 不合法返回 400;未上传图片返回 404。
|
||||
- 不发起 Huohanhan 或 Shopee 请求。
|
||||
|
||||
## 状态
|
||||
|
||||
已完成。
|
||||
Reference in New Issue
Block a user