feat: 实现工位终端滑动续签机制,强化安全与身份追溯
- 新增滑动续签功能:人工触屏触发本地心跳 + 30秒节流刷新JWT,避免作业中令牌过期 - 拆分质量检验权限:过程检/完工检使用不同权限码,细化权限粒度 - 新增操作人用户ID字段:报工、拧紧记录落库时携带可信用户ID,解决身份追溯问题 - 移除编译期默认密钥,启动期校验所有必要密钥非空,防止密钥泄露风险 - 修复WMS附件越权遍历问题:使用UUID文件名替代顺序ID访问附件 - 实现登录态过期数据隔离:仅返回当前操作人的待同步数据,避免身份混淆
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -248,7 +247,8 @@ func attachmentView(ctx *svc.ServiceContext, rec *ent.Attachment) *attachmentVie
|
||||
ID: int64(rec.ID), BizType: rec.BizType, BizID: rec.BizID, FileType: rec.FileType,
|
||||
FileName: rec.FileName, FileSize: rec.FileSize, FileExt: rec.FileExt, MimeType: rec.MimeType,
|
||||
UploadedBy: rec.UploadedBy, CreatedAt: rec.CreatedAt, Archived: rec.Archived, Deleted: rec.Deleted,
|
||||
URL: "/uploads/" + strconv.FormatInt(int64(rec.ID), 10),
|
||||
// 公开预览 URL 用磁盘 UUID 文件名(不可枚举),杜绝顺序 ID 越权遍历(安全审计 #8)
|
||||
URL: "/uploads/" + filepath.Base(rec.FilePath),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -384,39 +384,29 @@ func deleteAttachmentHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// serveUploadFileHandler 附件文件服务(按 id 提供,免鉴权便于 <img>/新窗口预览;支持 Range 断点续传)。
|
||||
// GET /uploads/:name(name = 附件 id,或历史扁平文件名兜底)
|
||||
// serveUploadFileHandler 附件文件服务(按 UUID 存储名提供,免鉴权便于 <img>/新窗口预览;支持 Range 断点续传)。
|
||||
// GET /uploads/:name(name = 磁盘 UUID 文件名,如 <uuid>.pdf)。顺序 ID 已被移除,杜绝越权遍历(安全审计 #8)。
|
||||
func serveUploadFileHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// 仅接受 UUID 存储名(库内 FilePath 末尾一段),顺序 ID 不再作为访问入口
|
||||
name := filepath.Base(r.URL.Path)
|
||||
if name == "" || name == "." || name == "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
root := attRoot(ctx)
|
||||
var absPath string
|
||||
var fileName string
|
||||
if id, e := strconv.ParseInt(name, 10, 64); e == nil && id > 0 {
|
||||
rec, err := ctx.EntClient.Attachment.Get(ctx0(), int(id))
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
p, ok := safeAbsPath(root, rec.FilePath)
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
absPath, fileName = p, rec.FileName
|
||||
} else {
|
||||
// 历史扁平文件(uuid.ext)兜底
|
||||
p, ok := safeAbsPath(root, name)
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
absPath, fileName = p, name
|
||||
rec, err := ctx.EntClient.Attachment.Query().
|
||||
Where(attachment.FilePathHasSuffix("/" + name), attachment.DeletedEQ(false)).Only(ctx0())
|
||||
if err != nil || rec == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
root := attRoot(ctx)
|
||||
p, ok := safeAbsPath(root, rec.FilePath)
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
absPath, fileName := p, rec.FileName
|
||||
if _, err := os.Stat(absPath); err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
|
||||
@@ -32,19 +32,9 @@ func createFinishedInventory(ctx *svc.ServiceContext, req *finishedInboundReques
|
||||
if req.Sn == "" || req.ProductCode == "" {
|
||||
return nil, http.StatusBadRequest, errors.New("sn 与 productCode 必填")
|
||||
}
|
||||
quality := req.QualityStatus
|
||||
if quality == "" {
|
||||
// 未显式传质量状态时按产品状态派生(方案A契约:已通过→合格 / 部分通过→合格 / 返修→不合格),
|
||||
// 显式传入的值仍尊重不覆盖。
|
||||
switch req.ProductStatus {
|
||||
case "部分通过":
|
||||
quality = QStatusPass
|
||||
case "返修":
|
||||
quality = "不合格"
|
||||
default:
|
||||
quality = QStatusPass
|
||||
}
|
||||
}
|
||||
// 质量状态归一:合法枚举值原样尊重;空值或非枚举值(如 MES 误传「已通过/部分通过」)按产品状态派生兜底,
|
||||
// 避免脏值落库(库存质量状态仅认 合格/不合格/让步接收/未检/待退)。
|
||||
quality := normalizeQualityStatus(req.QualityStatus, req.ProductStatus)
|
||||
|
||||
// SN 全局防重(同品类成品 + 半成品 + 序列号件 SN 都不允许撞号)
|
||||
dup, err := ctx.EntClient.Inventory.Query().
|
||||
@@ -113,6 +103,9 @@ func createFinishedInventory(ctx *svc.ServiceContext, req *finishedInboundReques
|
||||
SetRemark(remark).
|
||||
Save(ctx0())
|
||||
if err != nil {
|
||||
if isUniqueViolation(err) {
|
||||
return nil, http.StatusConflict, errors.New("成品 SN 已存在库存: " + req.Sn)
|
||||
}
|
||||
return nil, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
@@ -126,6 +119,35 @@ func createFinishedInventory(ctx *svc.ServiceContext, req *finishedInboundReques
|
||||
return rec, 0, nil
|
||||
}
|
||||
|
||||
// normalizeQualityStatus 归一成品入库质量状态:合法枚举值原样尊重;空值或非库存枚举值
|
||||
// (如 MES 误传「已通过/部分通过」等业务态)按产品状态派生兜底,防止脏值落库。
|
||||
func normalizeQualityStatus(quality, productStatus string) string {
|
||||
switch quality {
|
||||
case QStatusPending, QStatusPass, QStatusFail, QStatusReturn, QStatusConcession:
|
||||
return quality
|
||||
}
|
||||
switch productStatus {
|
||||
case "部分通过", "已通过":
|
||||
return QStatusPass
|
||||
case "返修":
|
||||
return QStatusFail
|
||||
default:
|
||||
return QStatusPass
|
||||
}
|
||||
}
|
||||
|
||||
// isUniqueViolation 判定 DB 错误是否唯一约束冲突(部分唯一索引 ux_inventories_sn_code 等)。
|
||||
// 用错误文本匹配,避免强依赖 lib/pq(当前为 indirect 依赖)。
|
||||
func isUniqueViolation(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := strings.ToLower(err.Error())
|
||||
return strings.Contains(msg, "ux_inventories_sn_code") ||
|
||||
strings.Contains(msg, "duplicate key") ||
|
||||
strings.Contains(msg, "violates unique constraint")
|
||||
}
|
||||
|
||||
// manualFinishedInboundHandler POST /api/finished/inbound 手工成品入库(登录态,权限 wms.finished:edit)。
|
||||
// 与 MES 完工回流共享创建逻辑(createFinishedInventory);差异仅在必填校验:
|
||||
// SN/产品编码/产品状态/记录员必填,记录日期默认今天;成功返回库存行。
|
||||
|
||||
@@ -414,6 +414,11 @@ func createInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
SetInboundNo(inboundNo).
|
||||
SetNillableProductWorkNo(strPtr(req.ProductWorkNo)).
|
||||
Save(ctx0()); err != nil {
|
||||
// 并发插入唯一冲突(部分唯一索引 ux_inventories_sn_code)也映射 409,与预检口径一致
|
||||
if isUniqueViolation(err) {
|
||||
fail(w, http.StatusConflict, "SN 已存在: "+sn)
|
||||
return
|
||||
}
|
||||
fail(w, http.StatusInternalServerError, "创建 SN 库存行失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -45,6 +45,10 @@ type deductStockRequest struct {
|
||||
// OverReason 超 BOM 领用原因(损耗/报废/返修/其他)。
|
||||
// 超领不硬卡:net_out > 需求时,填了原因即放行并留痕;未填仍 409 拦截。
|
||||
OverReason string `json:"overReason"`
|
||||
// IdempotencyKey 幂等键(安全审计 C2):调用方(如 MES 备料自动出库网络超时重试)
|
||||
// 传入稳定唯一键,同一键重复提交直接复用已生成的出库单,避免重复扣减库存。
|
||||
// 留空则按 OutboundNo 规则(手填校验唯一 / 留空自动生成)处理。
|
||||
IdempotencyKey string `json:"idempotencyKey"`
|
||||
}
|
||||
|
||||
// deductStockHandler 出库扣减
|
||||
@@ -107,12 +111,30 @@ func deductStockHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
outboundNo := strings.TrimSpace(req.OutboundNo)
|
||||
// C2 幂等键(安全审计):调用方传入幂等键(如 MES 自动出库超时重试),
|
||||
// 同一键重复提交直接复用已生成的出库单,避免重复扣减库存。
|
||||
outboundNo := strings.TrimSpace(req.IdempotencyKey)
|
||||
if outboundNo != "" {
|
||||
if err := validateUniqueNo(ctx, "OB", outboundNo); err != nil {
|
||||
if exist, e := ctx.EntClient.OutboundOrder.Query().
|
||||
Where(outboundorder.OutboundNoEQ(outboundNo)).Only(ctx0()); e == nil && exist != nil {
|
||||
// 幂等复用:该出库请求此前已处理,直接返回既有单据与状态,不再扣减。
|
||||
ok(w, map[string]any{
|
||||
"outboundNo": outboundNo,
|
||||
"qty": exist.Quantity,
|
||||
"orderNo": req.OrderNo,
|
||||
"outboundType": exist.OutboundType,
|
||||
"status": exist.Status,
|
||||
"idempotent": true,
|
||||
"message": "幂等复用:该出库请求此前已处理",
|
||||
})
|
||||
return
|
||||
}
|
||||
} else if obNo := strings.TrimSpace(req.OutboundNo); obNo != "" {
|
||||
if err := validateUniqueNo(ctx, "OB", obNo); err != nil {
|
||||
fail(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
outboundNo = obNo
|
||||
} else {
|
||||
no, err := nextDailyNo(ctx, "OB")
|
||||
if err != nil {
|
||||
@@ -167,6 +189,22 @@ func deductStockHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
SetNillableRemark(strPtr(req.Remark)).
|
||||
Save(ctx0())
|
||||
if err != nil {
|
||||
// 并发幂等:另一条相同幂等键/单号的请求已抢先落库,复用其单号返回。
|
||||
if isDuplicateOutboundNo(err) {
|
||||
if exist, e2 := ctx.EntClient.OutboundOrder.Query().
|
||||
Where(outboundorder.OutboundNoEQ(outboundNo)).Only(ctx0()); e2 == nil && exist != nil {
|
||||
ok(w, map[string]any{
|
||||
"outboundNo": outboundNo,
|
||||
"qty": exist.Quantity,
|
||||
"orderNo": req.OrderNo,
|
||||
"outboundType": exist.OutboundType,
|
||||
"status": exist.Status,
|
||||
"idempotent": true,
|
||||
"message": "幂等复用:该出库请求此前已处理",
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
fail(w, http.StatusInternalServerError, "创建出库单失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
@@ -299,8 +337,10 @@ func deductStockHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
status = "进行中"
|
||||
}
|
||||
// 台账 completed_at = 最后一笔领料完结出库时间(客户诉求 问题记录 L238-240:台账增加完成时间筛选)
|
||||
// 台账 out_qty 用原子自增(AddOutQty)替代「读改写绝对赋值」,消除并发丢失更新
|
||||
//(C1:原 newOutQty = ledgerNow.OutQty + actualQty 再 SetOutQty 在并发下会少计)。
|
||||
ledgerUpd := tx.OrderMaterialLedger.UpdateOneID(ledgerNow.ID).
|
||||
SetOutQty(newOutQty).
|
||||
AddOutQty(actualQty).
|
||||
SetStatus(status)
|
||||
if status == "领料完结" && ledgerNow.Status != "领料完结" {
|
||||
ledgerUpd.SetCompletedAt(time.Now().Unix())
|
||||
@@ -822,6 +862,18 @@ func min(a, b int) int {
|
||||
return b
|
||||
}
|
||||
|
||||
// isDuplicateOutboundNo 判定 DB 错误是否唯一约束冲突(出库单号 ux_outbound_orders_outbound_no)。
|
||||
// 用于幂等键并发落库时把 500 转为幂等复用,避免重复扣减。
|
||||
func isDuplicateOutboundNo(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := strings.ToLower(err.Error())
|
||||
return strings.Contains(msg, "ux_outbound_orders_outbound_no") ||
|
||||
strings.Contains(msg, "duplicate key") ||
|
||||
strings.Contains(msg, "violates unique constraint")
|
||||
}
|
||||
|
||||
// locationText 库位展示串。入库/出库/库存统一口径:`Z02 A架/3层/12位`,无内容返回空串。
|
||||
// outboundLocationFallback 出库四级库位兜底(客户诉求 一.9「出库相同」)。
|
||||
//
|
||||
|
||||
@@ -342,8 +342,9 @@ func confirmReturnOrderHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
if st == "领料完结" && (led.OutQty-newReturned) < led.TotalQty {
|
||||
st = "进行中"
|
||||
}
|
||||
// returned_qty 用原子自增(AddReturnedQty)替代「读改写绝对赋值」(C1:消除并发丢失更新)
|
||||
if _, uerr := tx.OrderMaterialLedger.UpdateOneID(led.ID).
|
||||
SetReturnedQty(newReturned).SetStatus(st).Save(ctx0()); uerr != nil {
|
||||
AddReturnedQty(ro.Qty).SetStatus(st).Save(ctx0()); uerr != nil {
|
||||
fail(w, http.StatusInternalServerError, "回写台账退回量失败: "+uerr.Error())
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user