feat: 添加代理获取应用详情和卡类列表API
- 添加 GET /agent/apps/:id 接口 - 返回代理有权使用的卡类列表及其价格 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
|||||||
func SetupAgentRoutes(r *gin.RouterGroup) {
|
func SetupAgentRoutes(r *gin.RouterGroup) {
|
||||||
r.GET("/stats", handleGetStats)
|
r.GET("/stats", handleGetStats)
|
||||||
r.GET("/apps", handleGetApps)
|
r.GET("/apps", handleGetApps)
|
||||||
|
r.GET("/apps/:id", handleGetAppDetail)
|
||||||
r.GET("/cards", handleGetCards)
|
r.GET("/cards", handleGetCards)
|
||||||
r.POST("/cards/generate", handleGenerateCards)
|
r.POST("/cards/generate", handleGenerateCards)
|
||||||
r.GET("/users", handleGetUsers)
|
r.GET("/users", handleGetUsers)
|
||||||
@@ -89,6 +90,66 @@ func handleGetApps(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleGetAppDetail(c *gin.Context) {
|
||||||
|
userID := c.GetUint("user_id")
|
||||||
|
appID := c.Param("id")
|
||||||
|
|
||||||
|
// 检查代理是否有该应用的授权
|
||||||
|
var agentApp model.AgentApplication
|
||||||
|
if err := database.DB.Where("agent_id = ? AND application_id = ?", userID, appID).Preload("Application").First(&agentApp).Error; err != nil {
|
||||||
|
response.Error(c, 404, "应用不存在或无权访问")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取该应用可用的卡类
|
||||||
|
var cardTypes []model.CardType
|
||||||
|
if err := database.DB.Where("application_id = ?", appID).Find(&cardTypes).Error; err != nil {
|
||||||
|
response.Error(c, 500, "获取卡类列表失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取代理对该应用的卡类权限和价格
|
||||||
|
var agentCardTypes []model.AgentApplicationCardType
|
||||||
|
database.DB.Where("agent_application_id = ?", agentApp.ID).Find(&agentCardTypes)
|
||||||
|
|
||||||
|
// 构建卡类列表,包含代理权限信息
|
||||||
|
cardTypesList := make([]gin.H, 0)
|
||||||
|
for _, ct := range cardTypes {
|
||||||
|
// 查找代理是否有该卡类的权限
|
||||||
|
var agentPrice float64
|
||||||
|
var canGenerate bool
|
||||||
|
for _, act := range agentCardTypes {
|
||||||
|
if act.CardTypeID == ct.ID {
|
||||||
|
agentPrice = act.Price
|
||||||
|
canGenerate = act.CanGenerate
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只返回代理有权限的卡类
|
||||||
|
if canGenerate {
|
||||||
|
cardTypesList = append(cardTypesList, gin.H{
|
||||||
|
"id": ct.ID,
|
||||||
|
"name": ct.Name,
|
||||||
|
"billing_type": ct.RechargeType,
|
||||||
|
"price": agentPrice,
|
||||||
|
"value": ct.Value,
|
||||||
|
"duration_days": ct.Value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
response.Success(c, gin.H{
|
||||||
|
"app": gin.H{
|
||||||
|
"id": agentApp.Application.ID,
|
||||||
|
"name": agentApp.Application.Name,
|
||||||
|
"description": agentApp.Application.Description,
|
||||||
|
"status": agentApp.Application.Status,
|
||||||
|
},
|
||||||
|
"cardTypes": cardTypesList,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func handleGetCards(c *gin.Context) {
|
func handleGetCards(c *gin.Context) {
|
||||||
userID := c.GetUint("user_id")
|
userID := c.GetUint("user_id")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user