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:
@@ -21,14 +21,14 @@ func NewProductHandler(db *gorm.DB) *ProductHandler {
|
||||
|
||||
// List GET /api/v1/products
|
||||
func (h *ProductHandler) List(c *gin.Context) {
|
||||
hotelID := middleware.GetHotelID(c)
|
||||
shopID := middleware.GetShopID(c)
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
keyword := c.Query("keyword")
|
||||
categoryID := c.Query("category_id")
|
||||
|
||||
query := h.db.Model(&model.Product{}).
|
||||
Where("hotel_id = ? AND deleted_at IS NULL", hotelID)
|
||||
Where("shop_id = ? AND deleted_at IS NULL", shopID)
|
||||
|
||||
if keyword != "" {
|
||||
query = query.Where("name LIKE ? OR code LIKE ? OR barcode LIKE ?",
|
||||
@@ -56,13 +56,13 @@ func (h *ProductHandler) List(c *gin.Context) {
|
||||
|
||||
// Create POST /api/v1/products
|
||||
func (h *ProductHandler) Create(c *gin.Context) {
|
||||
hotelID := middleware.GetHotelID(c)
|
||||
shopID := middleware.GetShopID(c)
|
||||
var product model.Product
|
||||
if err := c.ShouldBindJSON(&product); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
product.HotelID = hotelID
|
||||
product.ShopID = shopID
|
||||
|
||||
if err := h.db.Create(&product).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
@@ -73,11 +73,11 @@ func (h *ProductHandler) Create(c *gin.Context) {
|
||||
|
||||
// Update PUT /api/v1/products/:id
|
||||
func (h *ProductHandler) Update(c *gin.Context) {
|
||||
hotelID := middleware.GetHotelID(c)
|
||||
shopID := middleware.GetShopID(c)
|
||||
id := c.Param("id")
|
||||
|
||||
var product model.Product
|
||||
if err := h.db.Where("id = ? AND hotel_id = ? AND deleted_at IS NULL", id, hotelID).
|
||||
if err := h.db.Where("id = ? AND shop_id = ? AND deleted_at IS NULL", id, shopID).
|
||||
First(&product).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
@@ -87,7 +87,7 @@ func (h *ProductHandler) Update(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
product.HotelID = hotelID // 防止篡改
|
||||
product.ShopID = shopID // 防止篡改
|
||||
|
||||
if err := h.db.Save(&product).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
@@ -98,12 +98,12 @@ func (h *ProductHandler) Update(c *gin.Context) {
|
||||
|
||||
// Delete DELETE /api/v1/products/:id (软删除)
|
||||
func (h *ProductHandler) Delete(c *gin.Context) {
|
||||
hotelID := middleware.GetHotelID(c)
|
||||
shopID := middleware.GetShopID(c)
|
||||
id := c.Param("id")
|
||||
|
||||
now := timeNow()
|
||||
result := h.db.Model(&model.Product{}).
|
||||
Where("id = ? AND hotel_id = ? AND deleted_at IS NULL", id, hotelID).
|
||||
Where("id = ? AND shop_id = ? AND deleted_at IS NULL", id, shopID).
|
||||
Update("deleted_at", now)
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
|
||||
Reference in New Issue
Block a user