package middleware import ( "errors" "affiliate_dash/internal/model" "affiliate_dash/internal/pkg/response" "affiliate_dash/internal/service" "github.com/gin-gonic/gin" "gorm.io/gorm" ) // RequireMerchantFeature 校验当前商户是否开通指定功能。 func RequireMerchantFeature(db *gorm.DB, features ...string) gin.HandlerFunc { return func(c *gin.Context) { merchantID := GetMerchantID(c) if merchantID == 0 { response.Forbidden(c, "当前请求未绑定商户") c.Abort() return } var merchant model.Merchant if err := db.Select("id", "status", "features").First(&merchant, merchantID).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { response.Forbidden(c, "商户不存在") } else { response.ServerError(c, err.Error()) } c.Abort() return } if merchant.Status != model.MerchantStatusActive { response.Forbidden(c, "商户已禁用") c.Abort() return } if !service.MerchantHasFeature(merchant.Features, features...) { response.Forbidden(c, "商户功能未开通") c.Abort() return } c.Next() } }