149 lines
5.1 KiB
Go
149 lines
5.1 KiB
Go
package router
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"hfb_sys/backend/internal/filekey"
|
|
"hfb_sys/backend/internal/middleware"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/modules/chat"
|
|
"hfb_sys/backend/internal/modules/distribution"
|
|
"hfb_sys/backend/internal/sitecode"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
"hfb_sys/backend/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type distributionQrCodeScopeKey struct{}
|
|
type distributionQrCodeScope struct {
|
|
AdminID, SiteID, TenantID uint64
|
|
}
|
|
|
|
// 独立分销后台只开放读取,不复用经营站的二维码管理权限。
|
|
func registerDistributionQrCodeRoutes(routes *gin.RouterGroup, db *gorm.DB, service *distribution.Service, chats *chat.Handler, image gin.HandlerFunc) {
|
|
scoped := routes.Group("/sites/:id/qrcodes", requireDistributionQrCodeScope(db, service))
|
|
scoped.GET("", chats.ListSiteQrCodesHandler)
|
|
scoped.GET("/stats", chats.GetSiteQrCodeStatsHandler)
|
|
scoped.GET("/:qrcodeId/image", distributionQrCodeImage(db, image))
|
|
}
|
|
|
|
func requireDistributionQrCodeScope(db *gorm.DB, service *distribution.Service) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
adminID := c.GetUint64(middleware.ContextAdminID)
|
|
if adminID == 0 {
|
|
response.Error(c, http.StatusUnauthorized, "unauthorized", "请先登录")
|
|
c.Abort()
|
|
return
|
|
}
|
|
siteID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil || siteID == 0 {
|
|
response.BadRequest(c, "分销站点编号不正确")
|
|
c.Abort()
|
|
return
|
|
}
|
|
if db == nil || service == nil {
|
|
response.ServiceUnavailable(c, "二维码查询暂时不可用")
|
|
c.Abort()
|
|
return
|
|
}
|
|
ctx := c.Request.Context()
|
|
if _, err := service.ScopeSiteID(ctx, adminID, siteID); err != nil {
|
|
if errors.Is(err, distribution.ErrSiteAccessDenied) {
|
|
response.Error(c, http.StatusForbidden, "forbidden", "无权访问该分销站点")
|
|
} else {
|
|
response.RecordError(c, err)
|
|
response.ServiceUnavailable(c, "站点权限校验失败")
|
|
}
|
|
c.Abort()
|
|
return
|
|
}
|
|
var owner struct {
|
|
SiteCode string
|
|
TenantID uint64
|
|
}
|
|
err = db.WithContext(ctx).Table("distribution_sites AS ds").
|
|
Select("ds.site_code, st.tenant_id").
|
|
Joins("JOIN chat_site_tenant_links st ON st.site_id = ds.id AND st.enabled = 1").
|
|
Joins("JOIN tenants t ON t.id = st.tenant_id AND t.status = 'active'").
|
|
Where("ds.id = ? AND ds.status = 'active'", siteID).Take(&owner).Error
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
response.RecordError(c, err)
|
|
response.ServiceUnavailable(c, "站点归属查询失败")
|
|
c.Abort()
|
|
return
|
|
}
|
|
boundCode := sitecode.BoundSiteCodeFromContext(ctx)
|
|
requestTenant := tenancy.IDFromContext(ctx)
|
|
if err != nil || owner.TenantID == 0 ||
|
|
(boundCode != "" && boundCode != owner.SiteCode) ||
|
|
(requestTenant != 0 && requestTenant != owner.TenantID) {
|
|
response.Error(c, http.StatusForbidden, "forbidden", "无权访问该站点二维码池")
|
|
c.Abort()
|
|
return
|
|
}
|
|
scope, _ := tenancy.FromContext(ctx)
|
|
scope.TenantID = owner.TenantID
|
|
ctx = tenancy.WithScope(ctx, scope)
|
|
ctx = context.WithValue(ctx, distributionQrCodeScopeKey{}, distributionQrCodeScope{adminID, siteID, owner.TenantID})
|
|
c.Request = c.Request.WithContext(ctx)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
type distributionQrCodeImageKey struct{}
|
|
type distributionQrCodeImageGrant struct {
|
|
AdminID uint64
|
|
Key string
|
|
}
|
|
|
|
// 图片编号在服务端解析,客户端不能借图片 URL 读取其他站点的私有对象。
|
|
func distributionQrCodeImage(db *gorm.DB, object gin.HandlerFunc) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
scope, ok := ctx.Value(distributionQrCodeScopeKey{}).(distributionQrCodeScope)
|
|
if !ok || scope.AdminID == 0 {
|
|
response.Error(c, http.StatusForbidden, "forbidden", "无权访问该二维码")
|
|
return
|
|
}
|
|
id, err := strconv.ParseUint(c.Param("qrcodeId"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
response.BadRequest(c, "二维码编号不正确")
|
|
return
|
|
}
|
|
var code model.ChatQrCode
|
|
err = db.WithContext(ctx).Select("id, image_url").
|
|
Where("id = ? AND distribution_site_id = ? AND tenant_id = ?", id, scope.SiteID, scope.TenantID).Take(&code).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
response.Error(c, http.StatusNotFound, "not_found", "二维码不存在")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.RecordError(c, err)
|
|
response.ServiceUnavailable(c, "二维码图片查询失败")
|
|
return
|
|
}
|
|
key := filekey.Canonical(code.ImageURL)
|
|
if key == "" {
|
|
response.Error(c, http.StatusNotFound, "not_found", "二维码图片暂不可用")
|
|
return
|
|
}
|
|
ctx = context.WithValue(ctx, distributionQrCodeImageKey{}, distributionQrCodeImageGrant{scope.AdminID, key})
|
|
c.Request = c.Request.WithContext(ctx)
|
|
query := c.Request.URL.Query()
|
|
query.Set("key", key)
|
|
query.Del("ticket")
|
|
c.Request.URL.RawQuery = query.Encode()
|
|
object(c)
|
|
}
|
|
}
|
|
|
|
func distributionQrCodeImageAuthorizer(ctx context.Context, adminID uint64, key string) (bool, error) {
|
|
grant, ok := ctx.Value(distributionQrCodeImageKey{}).(distributionQrCodeImageGrant)
|
|
return ok && adminID > 0 && adminID == grant.AdminID && key == grant.Key, nil
|
|
}
|