diff --git a/backend/internal/router/agent/agent.go b/backend/internal/router/agent/agent.go index 8615a93..b6da1cf 100644 --- a/backend/internal/router/agent/agent.go +++ b/backend/internal/router/agent/agent.go @@ -183,6 +183,7 @@ func handleGenerateCards(c *gin.Context) { var req struct { ApplicationID uint `json:"application_id"` + AppID uint `json:"app_id"` // 兼容前端传的app_id CardTypeID uint `json:"card_type_id"` Quantity int `json:"quantity"` } @@ -191,16 +192,36 @@ func handleGenerateCards(c *gin.Context) { return } + // 兼容 app_id 和 application_id + appID := req.ApplicationID + if appID == 0 { + appID = req.AppID + } + var agentApp model.AgentApplication - if err := database.DB.Where("agent_id = ? AND application_id = ?", userID, req.ApplicationID).First(&agentApp).Error; err != nil { + if err := database.DB.Where("agent_id = ? AND application_id = ?", userID, appID).First(&agentApp).Error; err != nil { response.Error(c, 403, "无权操作该应用") return } + // 检查代理是否有该卡类的生成权限 + var agentCardType model.AgentApplicationCardType + if err := database.DB.Where("agent_application_id = ? AND card_type_id = ? AND can_generate = ?", agentApp.ID, req.CardTypeID, true).First(&agentCardType).Error; err != nil { + response.Error(c, 403, "无权生成该卡类") + return + } + + // 获取卡类信息以计算价格 + var cardType model.CardType + if err := database.DB.First(&cardType, req.CardTypeID).Error; err != nil { + response.Error(c, 404, "卡类不存在") + return + } + cards := make([]model.Card, req.Quantity) for i := 0; i < req.Quantity; i++ { cards[i] = model.Card{ - ApplicationID: req.ApplicationID, + ApplicationID: appID, CardTypeID: req.CardTypeID, CardKey: generateCardCode(), CreatorID: userID, @@ -213,8 +234,15 @@ func handleGenerateCards(c *gin.Context) { return } + // 返回生成的卡号 + codes := make([]string, req.Quantity) + for i, card := range cards { + codes[i] = card.CardKey + } + response.Success(c, gin.H{ "count": req.Quantity, + "codes": codes, }) } diff --git a/frontend/src/pages/agent/cards/create.vue b/frontend/src/pages/agent/cards/create.vue index 039ca49..8b21529 100644 --- a/frontend/src/pages/agent/cards/create.vue +++ b/frontend/src/pages/agent/cards/create.vue @@ -104,7 +104,7 @@ async function handleGenerate() { saving.value = true try { const data = await api.post<{ codes: string[], cards: any[] }>('/agent/cards/generate', { - app_id: Number(form.value.application_id), + application_id: Number(form.value.application_id), card_type_id: Number(form.value.card_type_id), quantity: form.value.count, }) @@ -178,7 +178,7 @@ onMounted(() => { - {{ type.name }} + {{ type.name }} - ¥{{ type.price }}