fix(backend): JWT config mapstructure tag 修复 + 模型从 hotel 重构为 shop
- 修复 JWTConfig 缺少 mapstructure tag 导致 access_expire_min 解析为 0, token 签发即过期,所有 API 请求返回 401 - 全部 config struct 补齐 mapstructure tag(secret/dsn/hmac_secret 等) - 模型层从 hotel/HotelID 统一重命名为 shop/ShopID - 删除旧 migrations(001-004),新增 001_init 综合迁移文件 - 更新 schema.sql、testutil、handler/service/model 相关引用 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -10,16 +10,16 @@ import (
|
||||
)
|
||||
|
||||
type Claims struct {
|
||||
UserID uint64 `json:"user_id"`
|
||||
HotelID uint64 `json:"hotel_id"`
|
||||
Role string `json:"role"`
|
||||
UserID uint64 `json:"user_id"`
|
||||
ShopID uint64 `json:"shop_id"`
|
||||
Role string `json:"role"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
const (
|
||||
CtxUserID = "user_id"
|
||||
CtxHotelID = "hotel_id"
|
||||
CtxRole = "role"
|
||||
CtxUserID = "user_id"
|
||||
CtxShopID = "shop_id"
|
||||
CtxRole = "role"
|
||||
)
|
||||
|
||||
func JWT() gin.HandlerFunc {
|
||||
@@ -41,7 +41,7 @@ func JWT() gin.HandlerFunc {
|
||||
}
|
||||
|
||||
c.Set(CtxUserID, claims.UserID)
|
||||
c.Set(CtxHotelID, claims.HotelID)
|
||||
c.Set(CtxShopID, claims.ShopID)
|
||||
c.Set(CtxRole, claims.Role)
|
||||
c.Next()
|
||||
}
|
||||
@@ -59,9 +59,21 @@ func AdminOnly() gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// GetHotelID 从 context 中安全获取 hotel_id
|
||||
func GetHotelID(c *gin.Context) uint64 {
|
||||
v, _ := c.Get(CtxHotelID)
|
||||
// ReadOnly 只读用户禁止写操作
|
||||
func ReadOnly() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
role, _ := c.Get(CtxRole)
|
||||
if role == "readonly" && c.Request.Method != "GET" {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "readonly user"})
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// GetShopID 从 context 中安全获取 shop_id
|
||||
func GetShopID(c *gin.Context) uint64 {
|
||||
v, _ := c.Get(CtxShopID)
|
||||
id, _ := v.(uint64)
|
||||
return id
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user