fix: 修复代理生成卡密权限检查和参数问题
- 后端兼容application_id和app_id参数 - 添加卡类权限检查,确保代理只能生成有权限的卡类 - 前端使用application_id参数名 - 移除卡类显示中的billing_type原始值,改为显示价格 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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(() => {
|
||||
</UiSelectTrigger>
|
||||
<UiSelectContent>
|
||||
<UiSelectItem v-for="type in cardTypes" :key="type.id" :value="String(type.id)">
|
||||
{{ type.name }}<template v-if="type.billing_type"> ({{ type.billing_type }})</template>
|
||||
{{ type.name }} - ¥{{ type.price }}
|
||||
</UiSelectItem>
|
||||
</UiSelectContent>
|
||||
</UiSelect>
|
||||
|
||||
Reference in New Issue
Block a user