优化api密钥

This commit is contained in:
yml2213
2026-08-03 10:38:06 +08:00
parent 9a3471cc61
commit 7fe4a05ed7
6 changed files with 71 additions and 15 deletions
+24 -1
View File
@@ -506,7 +506,9 @@ func (s *MerchantService) CreateAPIClient(merchantID uint, in CreateAPIClientInp
return errors.New("商户不存在或已禁用")
}
var clientCount int64
if err := tx.Model(&model.APIClient{}).Where("merchant_id = ?", merchantID).Count(&clientCount).Error; err != nil {
if err := tx.Model(&model.APIClient{}).
Where("merchant_id = ? AND status = ?", merchantID, model.APIClientStatusActive).
Count(&clientCount).Error; err != nil {
return err
}
if clientCount >= MaxAPIClientsPerMerchant {
@@ -545,6 +547,27 @@ func (s *MerchantService) UpdateAPIClientStatus(merchantID, id uint, status stri
})
}
// DeleteAPIClient 删除商户的 API 密钥(物理删除,不可恢复)。
// 删除前要求先停用,避免在用的密钥被误删。
func (s *MerchantService) DeleteAPIClient(merchantID, id uint, actorUserID uint) error {
return s.db.Transaction(func(tx *gorm.DB) error {
var client model.APIClient
if err := tx.Where("id = ? AND merchant_id = ?", id, merchantID).First(&client).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("API 客户端不存在")
}
return err
}
if client.Status == model.APIClientStatusActive {
return errors.New("请先停用该密钥再删除")
}
if err := tx.Delete(&client).Error; err != nil {
return err
}
return writeAudit(tx, &merchantID, &actorUserID, nil, "api_client.delete", "api_client", fmt.Sprint(id), map[string]string{"name": client.Name, "app_key": client.AppKey})
})
}
// ProductCatalogItem 是商品目录(自营商户可售商品)的展示项,供平台管理员分配商品时勾选。
type ProductCatalogItem struct {
ID uint `json:"id"`