feat: 更新商城前后端代码

This commit is contained in:
2026-04-01 22:03:59 +08:00
parent f6e150ba97
commit 1f16966174
34 changed files with 3014 additions and 5538 deletions

View File

@@ -1,11 +1,13 @@
package handlers
import (
"log"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"mengyastore-backend/internal/cache"
"mengyastore-backend/internal/models"
)
@@ -30,7 +32,19 @@ type togglePayload struct {
Active bool `json:"active"`
}
// VerifyAdminToken 验证管理员令牌是否正确,返回 {"valid": true/false},不暴露实际令牌值。
// invalidateProductCache removes the cached product list so that the next
// public request rebuilds it from the freshest DB state (Write-Invalidate).
func (h *AdminHandler) invalidateProductCache(c *gin.Context) {
if h.cache == nil {
return
}
if err := h.cache.Del(c.Request.Context(), cache.KeyProductList); err != nil {
log.Printf("[cache] DEL %s error: %v", cache.KeyProductList, err)
}
}
// VerifyAdminToken checks whether the supplied token is correct.
// Returns {"valid": true/false} without leaking the real token value.
func (h *AdminHandler) VerifyAdminToken(c *gin.Context) {
var payload struct {
Token string `json:"token"`
@@ -84,7 +98,7 @@ func (h *AdminHandler) CreateProduct(c *gin.Context) {
Active: active,
RequireLogin: payload.RequireLogin,
MaxPerAccount: payload.MaxPerAccount,
DeliveryMode: payload.DeliveryMode,
DeliveryMode: "auto",
ShowNote: payload.ShowNote,
ShowContact: payload.ShowContact,
}
@@ -93,6 +107,7 @@ func (h *AdminHandler) CreateProduct(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
h.invalidateProductCache(c) // Write-Invalidate: clear stale cache
c.JSON(http.StatusOK, gin.H{"data": created})
}
@@ -127,7 +142,7 @@ func (h *AdminHandler) UpdateProduct(c *gin.Context) {
Active: active,
RequireLogin: payload.RequireLogin,
MaxPerAccount: payload.MaxPerAccount,
DeliveryMode: payload.DeliveryMode,
DeliveryMode: "auto",
ShowNote: payload.ShowNote,
ShowContact: payload.ShowContact,
}
@@ -136,6 +151,7 @@ func (h *AdminHandler) UpdateProduct(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
h.invalidateProductCache(c)
c.JSON(http.StatusOK, gin.H{"data": updated})
}
@@ -154,6 +170,7 @@ func (h *AdminHandler) ToggleProduct(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
h.invalidateProductCache(c)
c.JSON(http.StatusOK, gin.H{"data": updated})
}
@@ -166,6 +183,7 @@ func (h *AdminHandler) DeleteProduct(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
h.invalidateProductCache(c)
c.JSON(http.StatusOK, gin.H{"data": gin.H{"ok": true}})
}