e0bee00ff4
- Web: 隐藏 ICP 备案号(条件渲染),联系入口改为微信咨询(条件渲染,配置 wechat 字段后生效) - backend: 拼音回填抽取为独立 cmd/backfill-pinyin 工具,不再在启动时运行 - backend: DB 连接池参数(max_idle_conns/max_open_conns)改为配置可控,默认 10/100 - backend: Inventory 反范式字段添加注释说明快照语义 - backend: 全量 handler 采用 util.RespondSuccess/RespondCreated 统一响应格式(51 处) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
114 lines
3.0 KiB
Go
114 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"
|
|
"github.com/wangjia/jiu/backend/internal/util"
|
|
)
|
|
|
|
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
|
|
}
|
|
util.RespondSuccess(c, 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
|
|
}
|
|
util.RespondSuccess(c, 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 {
|
|
util.RespondSuccess(c, nil)
|
|
return
|
|
}
|
|
|
|
deviceCount, err := h.svc.CountDevices(lic.ID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
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,
|
|
"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
|
|
}
|
|
util.RespondSuccess(c, 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"})
|
|
}
|