feat: 重构授权管理为层级模型 - 管理员直接对代理授权应用卡密权限
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package admin
|
||||
package admin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -17,6 +17,7 @@ func SetupAgentAppRoutes(r *gin.RouterGroup) {
|
||||
{
|
||||
agentApps.GET("", handleGetAgentApps)
|
||||
agentApps.GET("/requests", handleGetAgentRequests)
|
||||
agentApps.POST("/authorize", handleDirectAuthorize)
|
||||
agentApps.POST("/invite", handleInviteAgent)
|
||||
agentApps.PUT("/requests/:id/approve", handleApproveRequest)
|
||||
agentApps.PUT("/requests/:id/reject", handleRejectRequest)
|
||||
@@ -66,61 +67,34 @@ func checkAgentPermission(userID uint) bool {
|
||||
|
||||
func handleGetAgentApps(c *gin.Context) {
|
||||
userID := c.GetUint("user_id")
|
||||
log.Printf("[DEBUG] handleGetAgentApps called for Admin %d\n", userID)
|
||||
|
||||
var myAuthorizations []model.AgentApplication
|
||||
if err := database.DB.Where("admin_id = ?", userID).
|
||||
Preload("CardTypes.CardType").
|
||||
Find(&myAuthorizations).Error; err != nil {
|
||||
response.Error(c, 500, "获取授权列表失败")
|
||||
var currentUser model.User
|
||||
if err := database.DB.First(¤tUser, userID).Error; err != nil {
|
||||
response.Error(c, 500, "获取用户信息失败")
|
||||
return
|
||||
}
|
||||
|
||||
var receivedAuthorizations []model.AgentApplication
|
||||
if err := database.DB.Where("agent_id = ?", userID).
|
||||
Preload("CardTypes.CardType").
|
||||
Find(&receivedAuthorizations).Error; err != nil {
|
||||
response.Error(c, 500, "获取授权列表失败")
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Found %d my authorizations and %d received authorizations for Admin %d\n",
|
||||
len(myAuthorizations), len(receivedAuthorizations), userID)
|
||||
|
||||
var allAgentApps []model.AgentApplication
|
||||
allAgentApps = append(allAgentApps, myAuthorizations...)
|
||||
allAgentApps = append(allAgentApps, receivedAuthorizations...)
|
||||
|
||||
var agentIDs []uint
|
||||
var AdminIDs []uint
|
||||
var applicationIDs []uint
|
||||
|
||||
for _, aa := range allAgentApps {
|
||||
agentIDs = append(agentIDs, aa.AgentID)
|
||||
AdminIDs = append(AdminIDs, aa.AdminID)
|
||||
applicationIDs = append(applicationIDs, aa.ApplicationID)
|
||||
}
|
||||
|
||||
var users []model.User
|
||||
if err := database.DB.Where("id IN ?", append(agentIDs, AdminIDs...)).Find(&users).Error; err != nil {
|
||||
log.Printf("[ERROR] Failed to query users: %v\n", err)
|
||||
var authorizations []model.AgentApplication
|
||||
if currentUser.Role == "admin" {
|
||||
if err := database.DB.Where("admin_id = ?", userID).
|
||||
Preload("CardTypes.CardType").
|
||||
Preload("Agent").
|
||||
Preload("Application").
|
||||
Order("created_at DESC").
|
||||
Find(&authorizations).Error; err != nil {
|
||||
response.Error(c, 500, "获取授权列表失败")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
log.Printf("[DEBUG] Found %d users\n", len(users))
|
||||
}
|
||||
|
||||
var applications []model.Application
|
||||
if err := database.DB.Where("id IN ?", applicationIDs).Find(&applications).Error; err != nil {
|
||||
log.Printf("[ERROR] Failed to query applications: %v\n", err)
|
||||
}
|
||||
|
||||
userMap := make(map[uint]model.User)
|
||||
for _, user := range users {
|
||||
userMap[user.ID] = user
|
||||
}
|
||||
|
||||
applicationMap := make(map[uint]model.Application)
|
||||
for _, app := range applications {
|
||||
applicationMap[app.ID] = app
|
||||
if err := database.DB.Where("admin_id = ?", userID).
|
||||
Preload("CardTypes.CardType").
|
||||
Preload("Agent").
|
||||
Preload("Application").
|
||||
Order("created_at DESC").
|
||||
Find(&authorizations).Error; err != nil {
|
||||
response.Error(c, 500, "获取授权列表失败")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
type CardTypeResponse struct {
|
||||
@@ -139,25 +113,27 @@ func handleGetAgentApps(c *gin.Context) {
|
||||
AppName string `json:"app_name"`
|
||||
Discount float64 `json:"discount"`
|
||||
Status string `json:"status"`
|
||||
Balance float64 `json:"balance"`
|
||||
CardTypes []CardTypeResponse `json:"card_types"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
IsReceived bool `json:"is_received"`
|
||||
}
|
||||
|
||||
var result []AgentAppResponse
|
||||
for _, aa := range allAgentApps {
|
||||
for _, aa := range authorizations {
|
||||
agentName := ""
|
||||
agentEmail := ""
|
||||
if user, exists := userMap[aa.AgentID]; exists {
|
||||
agentName = user.Username
|
||||
if user.Email != nil {
|
||||
agentEmail = *user.Email
|
||||
var agentBalance float64
|
||||
if aa.Agent.ID != 0 {
|
||||
agentName = aa.Agent.Username
|
||||
if aa.Agent.Email != nil {
|
||||
agentEmail = *aa.Agent.Email
|
||||
}
|
||||
agentBalance = aa.Agent.Balance
|
||||
}
|
||||
|
||||
appName := ""
|
||||
if app, exists := applicationMap[aa.ApplicationID]; exists {
|
||||
appName = app.Name
|
||||
if aa.Application.ID != 0 {
|
||||
appName = aa.Application.Name
|
||||
}
|
||||
|
||||
var cardTypes []CardTypeResponse
|
||||
@@ -170,9 +146,6 @@ func handleGetAgentApps(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Processing AgentApp: ID=%d, AgentID=%d, ApplicationID=%d, CardTypes count=%d, IsReceived=%v\n",
|
||||
aa.ID, aa.AgentID, aa.ApplicationID, len(cardTypes), aa.AgentID == userID)
|
||||
|
||||
result = append(result, AgentAppResponse{
|
||||
ID: aa.ID,
|
||||
AgentID: aa.AgentID,
|
||||
@@ -182,13 +155,12 @@ func handleGetAgentApps(c *gin.Context) {
|
||||
AppName: appName,
|
||||
Discount: aa.Discount,
|
||||
Status: aa.Status,
|
||||
Balance: agentBalance,
|
||||
CardTypes: cardTypes,
|
||||
CreatedAt: aa.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
IsReceived: aa.AgentID == userID,
|
||||
})
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Returning %d agent apps for user %d\n", len(result), userID)
|
||||
response.Success(c, gin.H{
|
||||
"agent_apps": result,
|
||||
"total": len(result),
|
||||
@@ -765,3 +737,125 @@ func handleCancelRequest(c *gin.Context) {
|
||||
"id": req.ID,
|
||||
})
|
||||
}
|
||||
|
||||
func handleDirectAuthorize(c *gin.Context) {
|
||||
userID := c.GetUint("user_id")
|
||||
|
||||
var reqBody struct {
|
||||
AgentID uint `json:"agent_id" binding:"required"`
|
||||
ApplicationID uint `json:"application_id" binding:"required"`
|
||||
Discount float64 `json:"discount"`
|
||||
CardTypes []struct {
|
||||
CardTypeID uint `json:"card_type_id" binding:"required"`
|
||||
CanGenerate bool `json:"can_generate"`
|
||||
} `json:"card_types"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&reqBody); err != nil {
|
||||
response.Error(c, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
if reqBody.Discount <= 0 || reqBody.Discount > 1 {
|
||||
reqBody.Discount = 1.0
|
||||
}
|
||||
|
||||
var agent model.User
|
||||
if err := database.DB.Where("id = ? AND role = ?", reqBody.AgentID, "agent").First(&agent).Error; err != nil {
|
||||
response.Error(c, 404, "代理不存在")
|
||||
return
|
||||
}
|
||||
|
||||
if agent.ParentAgentID != nil && *agent.ParentAgentID != userID {
|
||||
var currentUser model.User
|
||||
if err := database.DB.First(¤tUser, userID).Error; err != nil {
|
||||
response.Error(c, 403, "无权授权该代理")
|
||||
return
|
||||
}
|
||||
if currentUser.Role != "admin" {
|
||||
response.Error(c, 403, "只能授权自己的下级代理")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var app model.Application
|
||||
if err := database.DB.Where("id = ?", reqBody.ApplicationID).First(&app).Error; err != nil {
|
||||
response.Error(c, 404, "应用不存在")
|
||||
return
|
||||
}
|
||||
|
||||
if app.UserID != userID {
|
||||
var currentUser model.User
|
||||
if err := database.DB.First(¤tUser, userID).Error; err != nil {
|
||||
response.Error(c, 403, "无权授权该应用")
|
||||
return
|
||||
}
|
||||
if currentUser.Role != "admin" {
|
||||
response.Error(c, 403, "只能授权自己的应用")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var existing model.AgentApplication
|
||||
if err := database.DB.Where("agent_id = ? AND application_id = ?", reqBody.AgentID, reqBody.ApplicationID).First(&existing).Error; err == nil {
|
||||
response.Error(c, 400, "该代理已获得此应用的授权")
|
||||
return
|
||||
}
|
||||
|
||||
tx := database.DB.Begin()
|
||||
|
||||
agentApp := model.AgentApplication{
|
||||
AgentID: reqBody.AgentID,
|
||||
ApplicationID: reqBody.ApplicationID,
|
||||
AdminID: userID,
|
||||
Discount: reqBody.Discount,
|
||||
Status: "active",
|
||||
IsReceived: true,
|
||||
}
|
||||
if err := tx.Create(&agentApp).Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, 500, "创建授权失败")
|
||||
return
|
||||
}
|
||||
|
||||
if len(reqBody.CardTypes) > 0 {
|
||||
for _, ct := range reqBody.CardTypes {
|
||||
agentCardType := model.AgentApplicationCardType{
|
||||
AgentApplicationID: agentApp.ID,
|
||||
CardTypeID: ct.CardTypeID,
|
||||
CanGenerate: ct.CanGenerate,
|
||||
}
|
||||
if err := tx.Create(&agentCardType).Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, 500, "创建卡密权限失败")
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var cardTypes []model.CardType
|
||||
database.DB.Where("application_id = ?", reqBody.ApplicationID).Find(&cardTypes)
|
||||
for _, ct := range cardTypes {
|
||||
agentCardType := model.AgentApplicationCardType{
|
||||
AgentApplicationID: agentApp.ID,
|
||||
CardTypeID: ct.ID,
|
||||
CanGenerate: false,
|
||||
}
|
||||
if err := tx.Create(&agentCardType).Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, 500, "创建卡密权限失败")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
response.Success(c, gin.H{
|
||||
"id": agentApp.ID,
|
||||
"agent_id": agentApp.AgentID,
|
||||
"agent_name": agent.Username,
|
||||
"app_id": agentApp.ApplicationID,
|
||||
"app_name": app.Name,
|
||||
"discount": agentApp.Discount,
|
||||
"status": agentApp.Status,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user