3ab78dbf7a
- #32 License 激活迁移到 license_devices 表:Activate/Verify/Deactivate 全部改用 license_devices,新增 max_devices 校验和 GET /license/devices 端点; Activate 现在校验 shop_id 防跨租户激活 - #33 checkInventory 从 StockOutHandler 移到 StockService.CheckInventoryAvailability - #34 新增 util/response.go 统一错误响应工具(RespondError/RespondSuccess/RespondCreated) - #35 生产模式 CORS Origin='*' 启动时 Fatal - #36 生产模式 License 私钥未配置启动时 Fatal - #37 新增 util/page.go ValidatePageSize,应用到 partner/product/stock_in/stock_out handler Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
112 lines
3.0 KiB
Go
112 lines
3.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/wangjia/jiu/backend/internal/middleware"
|
|
"github.com/wangjia/jiu/backend/internal/service"
|
|
)
|
|
|
|
type LicenseHandler struct {
|
|
svc *service.LicenseService
|
|
}
|
|
|
|
func NewLicenseHandler(svc *service.LicenseService) *LicenseHandler {
|
|
return &LicenseHandler{svc: svc}
|
|
}
|
|
|
|
// Activate POST /api/v1/license/activate
|
|
func (h *LicenseHandler) Activate(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
var req struct {
|
|
LicenseKey string `json:"license_key" binding:"required"`
|
|
DeviceID string `json:"device_id" binding:"required"`
|
|
DeviceName string `json:"device_name"`
|
|
Platform string `json:"platform"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
lic, err := h.svc.Activate(shopID, req.LicenseKey, req.DeviceID, req.DeviceName, req.Platform)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": lic})
|
|
}
|
|
|
|
// Verify GET /api/v1/license/verify
|
|
func (h *LicenseHandler) Verify(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
deviceID := c.Query("device_id")
|
|
if deviceID == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "device_id required"})
|
|
return
|
|
}
|
|
|
|
lic, err := h.svc.Verify(shopID, deviceID)
|
|
if err != nil {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": lic})
|
|
}
|
|
|
|
// Info GET /api/v1/license/info — 当前门店授权概况
|
|
func (h *LicenseHandler) Info(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
lic, err := h.svc.ShopInfo(shopID)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"data": nil})
|
|
return
|
|
}
|
|
|
|
var deviceCount int64
|
|
// count active devices for this shop
|
|
devs, _ := h.svc.ListDevices(shopID)
|
|
deviceCount = int64(len(devs))
|
|
|
|
phase := middleware.CalcLicensePhase(lic.ExpiresAt)
|
|
c.JSON(http.StatusOK, gin.H{"data": gin.H{
|
|
"id": lic.ID,
|
|
"type": lic.Type,
|
|
"is_active": lic.IsActive,
|
|
"max_devices": lic.MaxDevices,
|
|
"device_count": deviceCount,
|
|
"expires_at": lic.ExpiresAt,
|
|
"phase": phase,
|
|
}})
|
|
}
|
|
|
|
// Devices GET /api/v1/license/devices — 已绑定设备列表
|
|
func (h *LicenseHandler) Devices(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
devs, err := h.svc.ListDevices(shopID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": devs})
|
|
}
|
|
|
|
// Deactivate POST /api/v1/license/deactivate
|
|
func (h *LicenseHandler) Deactivate(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
var req struct {
|
|
DeviceID string `json:"device_id" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if err := h.svc.Deactivate(shopID, req.DeviceID); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "deactivated"})
|
|
}
|