fix: 修复代理卡密列表数据不显示问题
- Card模型添加AgentID字段 - 生成卡密时设置AgentID - handleGetCards返回正确格式的数据(包含app_name, card_type_name, code等) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -286,6 +286,7 @@ type Card struct {
|
||||
CardTypeID uint `gorm:"index;not null" json:"card_type_id"`
|
||||
CardKey string `gorm:"uniqueIndex;size:100" json:"card_key"`
|
||||
CreatorID uint `gorm:"index" json:"creator_id"`
|
||||
AgentID *uint `gorm:"index" json:"agent_id"`
|
||||
AppUserID *uint `gorm:"index" json:"app_user_id"`
|
||||
Status string `gorm:"size:20;default:unused;index" json:"status"`
|
||||
UsedAt *time.Time `json:"used_at"`
|
||||
@@ -297,6 +298,7 @@ type Card struct {
|
||||
Application *Application `gorm:"foreignKey:ApplicationID" json:"application,omitempty"`
|
||||
CardType CardType `gorm:"foreignKey:CardTypeID" json:"card_type,omitempty"`
|
||||
Creator User `gorm:"foreignKey:CreatorID" json:"creator,omitempty"`
|
||||
Agent *User `gorm:"foreignKey:AgentID" json:"agent,omitempty"`
|
||||
AppUser *AppUser `gorm:"foreignKey:AppUserID" json:"app_user,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
@@ -172,8 +172,51 @@ func handleGetCards(c *gin.Context) {
|
||||
|
||||
database.DB.Where("agent_id = ?", userID).Order("created_at DESC").Limit(limit).Offset(offset).Find(&cards)
|
||||
|
||||
// 获取关联数据
|
||||
cardTypeIDs := make([]uint, 0)
|
||||
appIDs := make([]uint, 0)
|
||||
for _, card := range cards {
|
||||
cardTypeIDs = append(cardTypeIDs, card.CardTypeID)
|
||||
appIDs = append(appIDs, card.ApplicationID)
|
||||
}
|
||||
|
||||
cardTypeMap := make(map[uint]model.CardType)
|
||||
if len(cardTypeIDs) > 0 {
|
||||
var cardTypes []model.CardType
|
||||
database.DB.Where("id IN ?", cardTypeIDs).Find(&cardTypes)
|
||||
for _, ct := range cardTypes {
|
||||
cardTypeMap[ct.ID] = ct
|
||||
}
|
||||
}
|
||||
|
||||
appMap := make(map[uint]model.Application)
|
||||
if len(appIDs) > 0 {
|
||||
var apps []model.Application
|
||||
database.DB.Where("id IN ?", appIDs).Find(&apps)
|
||||
for _, app := range apps {
|
||||
appMap[app.ID] = app
|
||||
}
|
||||
}
|
||||
|
||||
// 构建返回数据
|
||||
cardList := make([]gin.H, 0)
|
||||
for _, card := range cards {
|
||||
ct := cardTypeMap[card.CardTypeID]
|
||||
app := appMap[card.ApplicationID]
|
||||
|
||||
cardList = append(cardList, gin.H{
|
||||
"id": card.ID,
|
||||
"code": card.CardKey,
|
||||
"app_name": app.Name,
|
||||
"card_type_name": ct.Name,
|
||||
"status": card.Status,
|
||||
"created_at": card.CreatedAt,
|
||||
"used_at": card.UsedAt,
|
||||
})
|
||||
}
|
||||
|
||||
response.Success(c, gin.H{
|
||||
"cards": cards,
|
||||
"cards": cardList,
|
||||
"total": total,
|
||||
})
|
||||
}
|
||||
@@ -225,6 +268,7 @@ func handleGenerateCards(c *gin.Context) {
|
||||
CardTypeID: req.CardTypeID,
|
||||
CardKey: generateCardCode(),
|
||||
CreatorID: userID,
|
||||
AgentID: &userID,
|
||||
Status: "unused",
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user