bc85b2c8ad
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
91 lines
2.4 KiB
Go
91 lines
2.4 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) {
|
|
var req struct {
|
|
LicenseKey string `json:"license_key" binding:"required"`
|
|
DeviceID string `json:"device_id" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
lic, err := h.svc.Activate(req.LicenseKey, req.DeviceID)
|
|
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 — 当前门店授权概况(无需 device_id)
|
|
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
|
|
}
|
|
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,
|
|
"expires_at": lic.ExpiresAt,
|
|
"phase": phase,
|
|
}})
|
|
}
|
|
|
|
// 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"})
|
|
}
|