feat: 自动更新、系统设置、安全修复
后端: - 新增 GET /version 版本检查端点(version.go + version.yaml) - 新增 GET /license/info 接口,返回门店授权信息 - 修复 GenerateOrderNo 并发重复单号:事务内加 FOR UPDATE 行锁 - 修复 ApproveStockOut 超卖竞态:预检和库存更新均加 FOR UPDATE - 修复 Product Create 并发 code 冲突:加重试逻辑,schema 加 UNIQUE KEY - 修复 Product Update 全字段覆盖:改用 selective Updates() - 挂载 ReadOnly 中间件(全局)+ AdminOnly(用户管理路由) - version.go 配置缺失时返回 500 而非静默降级 前端: - 新增自动更新检测(update_provider.dart)+ shell 更新 banner/弹窗 - 新增系统设置"关于"标签页:版本、授权、开发信息、意见反馈 - 新增离线缓存:所有 AsyncNotifierProvider 支持断网浏览历史数据 - 新增门店信息弹窗(点击左上角 logo 或右上角门店号触发) - 提取 AppConfig 统一管理 BASE_URL,支持 --dart-define 注入 - update_provider.dart 加 kIsWeb 保护,修复 Web 平台崩溃 - dev.sh 新增 stop 命令,修复 stop 误杀前端进程问题 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -64,8 +66,34 @@ func (h *ProductHandler) Create(c *gin.Context) {
|
||||
}
|
||||
product.ShopID = shopID
|
||||
|
||||
if err := h.db.Create(&product).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
// Auto-generate product code if not provided (e.g. P001, P002)
|
||||
// Retry up to 5 times on duplicate key to handle concurrent creates
|
||||
if product.Code == "" {
|
||||
var count int64
|
||||
h.db.Model(&model.Product{}).
|
||||
Where("shop_id = ? AND deleted_at IS NULL", shopID).
|
||||
Count(&count)
|
||||
product.Code = fmt.Sprintf("P%03d", count+1)
|
||||
}
|
||||
|
||||
var createErr error
|
||||
for attempt := 0; attempt < 5; attempt++ {
|
||||
if createErr = h.db.Create(&product).Error; createErr == nil {
|
||||
break
|
||||
}
|
||||
if !errors.Is(createErr, gorm.ErrDuplicatedKey) {
|
||||
break
|
||||
}
|
||||
// Duplicate code: try next slot
|
||||
var count int64
|
||||
h.db.Model(&model.Product{}).
|
||||
Where("shop_id = ? AND deleted_at IS NULL", shopID).
|
||||
Count(&count)
|
||||
product.ID = 0
|
||||
product.Code = fmt.Sprintf("P%03d", count+int64(attempt)+2)
|
||||
}
|
||||
if createErr != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": createErr.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{"data": product})
|
||||
@@ -83,16 +111,34 @@ func (h *ProductHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&product); err != nil {
|
||||
var req model.Product
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
product.ShopID = shopID // 防止篡改
|
||||
|
||||
if err := h.db.Save(&product).Error; err != nil {
|
||||
// 只更新业务字段,防止 Save() 覆盖 shop_id / created_at 等系统字段
|
||||
if err := h.db.Model(&product).Updates(map[string]interface{}{
|
||||
"code": req.Code,
|
||||
"barcode": req.Barcode,
|
||||
"name": req.Name,
|
||||
"series": req.Series,
|
||||
"spec": req.Spec,
|
||||
"unit": req.Unit,
|
||||
"category_id": req.CategoryID,
|
||||
"brand": req.Brand,
|
||||
"purchase_price": req.PurchasePrice,
|
||||
"sale_price": req.SalePrice,
|
||||
"min_stock": req.MinStock,
|
||||
"remark": req.Remark,
|
||||
"custom_fields": req.CustomFields,
|
||||
}).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 重新读取完整数据返回
|
||||
h.db.Preload("Category").First(&product, product.ID)
|
||||
c.JSON(http.StatusOK, gin.H{"data": product})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user