feat: 添加代理充值功能到代理管理页面, 移除授权管理页面的无效操作
This commit is contained in:
@@ -21,6 +21,7 @@ func SetupAgentsRoutes(r *gin.RouterGroup) {
|
||||
agents.GET("/:id", handleGetAgentDetail)
|
||||
agents.PUT("/:id", handleUpdateAgent)
|
||||
agents.PUT("/:id/status", handleUpdateAgentStatus)
|
||||
agents.POST("/:id/recharge", handleRechargeAgent)
|
||||
agents.DELETE("/:id", handleDeleteAgent)
|
||||
agents.GET("/:id/cards", handleGetAgentCards)
|
||||
agents.PUT("/:id/cards", handleUpdateAgentCards)
|
||||
@@ -77,6 +78,7 @@ func handleGetAgents(c *gin.Context) {
|
||||
CreatedAt string `json:"created_at"`
|
||||
LastLoginAt string `json:"last_login_at"`
|
||||
Balance float64 `json:"balance"`
|
||||
TotalConsumption float64 `json:"total_consumption"`
|
||||
CanCreateAgent bool `json:"can_create_agent"`
|
||||
CardsCount int `json:"cards_count"`
|
||||
ChildAgentsCount int `json:"child_agents_count"`
|
||||
@@ -149,6 +151,7 @@ func handleGetAgents(c *gin.Context) {
|
||||
CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
LastLoginAt: lastLoginAt,
|
||||
Balance: user.Balance,
|
||||
TotalConsumption: user.TotalConsumption,
|
||||
CanCreateAgent: user.CanCreateAgent,
|
||||
CardsCount: cardsCountMap[user.ID],
|
||||
ChildAgentsCount: childCountMap[user.ID],
|
||||
@@ -678,3 +681,41 @@ func handleBatchDeleteAgents(c *gin.Context) {
|
||||
"deleted": len(req.AgentIDs),
|
||||
})
|
||||
}
|
||||
|
||||
func handleRechargeAgent(c *gin.Context) {
|
||||
agentID := c.Param("id")
|
||||
|
||||
var user model.User
|
||||
if err := database.DB.Where("id = ? AND role = ?", agentID, "agent").First(&user).Error; err != nil {
|
||||
response.Error(c, 404, "代理不存在")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Amount float64 `json:"amount" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.Error(c, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
if req.Amount <= 0 {
|
||||
response.Error(c, 400, "充值金额必须大于0")
|
||||
return
|
||||
}
|
||||
|
||||
newBalance := user.Balance + req.Amount
|
||||
if err := database.DB.Model(&user).Update("balance", newBalance).Error; err != nil {
|
||||
response.Error(c, 500, "充值失败")
|
||||
return
|
||||
}
|
||||
|
||||
service.LogOperation(c, "recharge", "agent", &user.ID, fmt.Sprintf("为代理充值: %.2f, 余额: %.2f -> %.2f", req.Amount, user.Balance, newBalance), nil)
|
||||
|
||||
response.Success(c, gin.H{
|
||||
"balance": newBalance,
|
||||
"recharge": req.Amount,
|
||||
"agent_id": user.ID,
|
||||
"agent_name": user.Username,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user