feat: 实现工位终端滑动续签机制,强化安全与身份追溯
- 新增滑动续签功能:人工触屏触发本地心跳 + 30秒节流刷新JWT,避免作业中令牌过期 - 拆分质量检验权限:过程检/完工检使用不同权限码,细化权限粒度 - 新增操作人用户ID字段:报工、拧紧记录落库时携带可信用户ID,解决身份追溯问题 - 移除编译期默认密钥,启动期校验所有必要密钥非空,防止密钥泄露风险 - 修复WMS附件越权遍历问题:使用UUID文件名替代顺序ID访问附件 - 实现登录态过期数据隔离:仅返回当前操作人的待同步数据,避免身份混淆
This commit is contained in:
@@ -252,6 +252,31 @@ func (s *Service) StationLogin(ctx context.Context, req StationLoginReq) (Statio
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// StationRefreshTokenResp 工位终端滑动续签响应
|
||||
type StationRefreshTokenResp struct {
|
||||
AccessToken string `json:"accessToken"`
|
||||
ExpireAt int64 `json:"expireAt"`
|
||||
}
|
||||
|
||||
// StationRefreshToken 工位终端滑动续签:校验当前登录态 JWT 有效后,原样重签新 JWT(延长有效期)。
|
||||
// 终端任何人工触屏经前端 30 秒节流后调用,作业中令牌不再因固定有效期过期而被登出。
|
||||
// 工位终端与 MES 同密钥,claims(含 StationNo)原样保留。
|
||||
func (s *Service) StationRefreshToken(tokenStr string) (StationRefreshTokenResp, error) {
|
||||
var resp StationRefreshTokenResp
|
||||
claims, err := tokenx.Parse(s.ctx.Config.Auth.AccessSecret, tokenStr)
|
||||
if err != nil || claims == nil {
|
||||
return resp, errors.New("登录凭证无效或已过期,请重新登录")
|
||||
}
|
||||
expire := s.sessionExpire()
|
||||
access, err := tokenx.Issue(s.ctx.Config.Auth.AccessSecret, time.Duration(expire)*time.Second, *claims)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
resp.AccessToken = access
|
||||
resp.ExpireAt = time.Now().Add(time.Duration(expire) * time.Second).Unix()
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// StationChangePassword 工位终端修改自己的工位终端密码
|
||||
func (s *Service) StationChangePassword(ctx context.Context, userId int, oldPwd, newPwd string) error {
|
||||
if userId <= 0 {
|
||||
|
||||
@@ -378,7 +378,9 @@ func (s *Service) finishedInspectionInbound(ctx context.Context, rec *ent.Inspec
|
||||
"完工检合格但成品回流WMS入库失败:工单不存在", map[string]any{"error": "work order not found"})
|
||||
return "检验记录已保存;WMS 入库失败:工件所属工单不存在"
|
||||
}
|
||||
qualityStatusForWMS := productStatus
|
||||
// 质量状态落库值:合格类(已通过/部分通过)统一传「合格」;让步接收传「让步接收」。
|
||||
// 不再传「已通过/部分通过」等非库存枚举值(WMS 库存质量状态仅认 合格/不合格/让步接收/未检/待退)。
|
||||
qualityStatusForWMS := "合格"
|
||||
if concession {
|
||||
qualityStatusForWMS = "让步接收"
|
||||
}
|
||||
|
||||
@@ -255,6 +255,9 @@ func (s *Service) AutoOutboundRequests(ctx context.Context, planDate, orderNo, o
|
||||
if mr.Source == "REFILL" {
|
||||
obType = "refill"
|
||||
}
|
||||
// C2 幂等键轮次:同一备料单每次 StockDeduct 调用用稳定唯一键,
|
||||
// 网络超时重试时 WMS 按键复用已生成的出库单,杜绝重复扣减。
|
||||
round := 0
|
||||
opts := wmsclient.StockDeductOpts{
|
||||
Qty: need, Operator: operator, TargetDock: dock,
|
||||
OutboundType: obType, NeedAgv: mr.NeedAgv, OverReason: mr.OverReason,
|
||||
@@ -264,10 +267,12 @@ func (s *Service) AutoOutboundRequests(ctx context.Context, planDate, orderNo, o
|
||||
if remain <= 0 {
|
||||
break
|
||||
}
|
||||
round++
|
||||
opts.SnList = []string{sn}
|
||||
opts.BatchNo = ""
|
||||
opts.Qty = 1
|
||||
no, _, e := s.ctx.Wms.StockDeduct(ctx, mr.OrderNo, mr.MaterialCode, opts)
|
||||
opts.IdempotencyKey = fmt.Sprintf("IDEM-%s-%s-%s-%d", mr.OrderNo, mr.MaterialCode, sn, round)
|
||||
no, _, _, e := s.ctx.Wms.StockDeduct(ctx, mr.OrderNo, mr.MaterialCode, opts)
|
||||
if e != nil {
|
||||
break
|
||||
}
|
||||
@@ -286,11 +291,13 @@ func (s *Service) AutoOutboundRequests(ctx context.Context, planDate, orderNo, o
|
||||
if take <= 0 {
|
||||
continue
|
||||
}
|
||||
round++
|
||||
opts.SnList = nil
|
||||
opts.BatchNo = b.BatchNo
|
||||
opts.Qty = take
|
||||
opts.ZoneCode = b.ZoneCode
|
||||
no, _, e := s.ctx.Wms.StockDeduct(ctx, mr.OrderNo, mr.MaterialCode, opts)
|
||||
opts.IdempotencyKey = fmt.Sprintf("IDEM-%s-%s-%s-%d", mr.OrderNo, mr.MaterialCode, b.BatchNo, round)
|
||||
no, _, _, e := s.ctx.Wms.StockDeduct(ctx, mr.OrderNo, mr.MaterialCode, opts)
|
||||
if e != nil {
|
||||
break
|
||||
}
|
||||
@@ -300,15 +307,47 @@ func (s *Service) AutoOutboundRequests(ctx context.Context, planDate, orderNo, o
|
||||
}
|
||||
sent := need - remain
|
||||
if sent > 0 {
|
||||
upd := s.ctx.EntClient.MaterialRequest.UpdateOneID(mr.ID).AddSentQty(float64(sent))
|
||||
if remain == 0 {
|
||||
// 出库量已达标:备料完成,等 AGV 配送(WMS 下发后回写 DELIVERING)
|
||||
upd = upd.SetStatus("LOCKED")
|
||||
done++
|
||||
// C3 行锁 + 钳制:在事务内锁定该行后重读 sentQty 并重算,避免并发/重试重复累计;
|
||||
// 以 reqQty 为上限钳制,杜绝超出 BOM 需求量的虚高计数(网络重试幂等复用场景下尤其关键)。
|
||||
applied := false
|
||||
if tx, txErr := s.ctx.EntClient.Tx(ctx); txErr == nil {
|
||||
locked, lErr := tx.MaterialRequest.Query().
|
||||
Where(materialrequest.ID(mr.ID)).ForUpdate().Only(ctx)
|
||||
if lErr == nil {
|
||||
newSent := locked.SentQty + float64(sent)
|
||||
if newSent > locked.ReqQty {
|
||||
newSent = locked.ReqQty // 钳制到需求量上限
|
||||
}
|
||||
upd := tx.MaterialRequest.UpdateOneID(locked.ID).SetSentQty(newSent)
|
||||
if remain == 0 {
|
||||
// 出库量已达标:备料完成,等 AGV 配送(WMS 下发后回写 DELIVERING)
|
||||
upd = upd.SetStatus("LOCKED")
|
||||
}
|
||||
if _, e := upd.Save(ctx); e == nil {
|
||||
_ = tx.Commit()
|
||||
applied = true
|
||||
if remain == 0 {
|
||||
done++
|
||||
}
|
||||
s.ctx.EventLog.Write(ctx, "material.outbound.auto", mr.OrderNo, operator, "material_request", mr.RequestNo,
|
||||
"备料自动出库", map[string]any{"materialCode": mr.MaterialCode, "qty": sent, "outboundNos": strings.Join(outboundNos, ",")})
|
||||
}
|
||||
}
|
||||
if !applied {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}
|
||||
if _, e := upd.Save(ctx); e == nil {
|
||||
s.ctx.EventLog.Write(ctx, "material.outbound.auto", mr.OrderNo, operator, "material_request", mr.RequestNo,
|
||||
"备料自动出库", map[string]any{"materialCode": mr.MaterialCode, "qty": sent, "outboundNos": strings.Join(outboundNos, ",")})
|
||||
if !applied {
|
||||
// 行锁事务不可用时的兜底:直接原子累加(并发下仍有极小概率重复累计,由 WMS 幂等键兜底扣减)
|
||||
upd := s.ctx.EntClient.MaterialRequest.UpdateOneID(mr.ID).AddSentQty(float64(sent))
|
||||
if remain == 0 {
|
||||
upd = upd.SetStatus("LOCKED")
|
||||
done++
|
||||
}
|
||||
if _, e := upd.Save(ctx); e == nil {
|
||||
s.ctx.EventLog.Write(ctx, "material.outbound.auto", mr.OrderNo, operator, "material_request", mr.RequestNo,
|
||||
"备料自动出库", map[string]any{"materialCode": mr.MaterialCode, "qty": sent, "outboundNos": strings.Join(outboundNos, ",")})
|
||||
}
|
||||
}
|
||||
}
|
||||
if remain > 0 {
|
||||
|
||||
@@ -36,13 +36,15 @@ func (s *Service) Seed(ctx context.Context) error {
|
||||
"produce.processflow:upload", "produce.station:edit", "produce.station:add", "produce.station:delete",
|
||||
// 预警中心 / 附件中心(含归档清理按钮)
|
||||
"sys.alert", "sys.attachment", "sys.attachment:manage",
|
||||
// 数量不符处理(上报在工位终端,处理在本页)
|
||||
"produce.qty", "produce.qty:handle",
|
||||
}
|
||||
// 数量不符处理(上报在工位终端,处理在本页)
|
||||
"produce.qty", "produce.qty:handle",
|
||||
// 质量检验:过程检操作工自检(process);完工检(final)由质检员专属(见下方 dropCodes,补1 权限粒度拆分)
|
||||
"produce.quality", "produce.quality:process",
|
||||
}
|
||||
} else if r.code == "INSPECTOR" {
|
||||
codes = []string{"produce.trace", "produce.wip", "produce.performance", "sys.eventlog",
|
||||
"sys.inspect", "sys.inspect:checkin", "sys.inspect:point", "sys.inspect:process",
|
||||
"sys.inspect:done", "sys.inspect:alarm", "sys.inspect:view", "sys.inspect:sign", "produce.quality", "produce.quality:edit"}
|
||||
"sys.inspect:done", "sys.inspect:alarm", "sys.inspect:view", "sys.inspect:sign", "produce.quality", "produce.quality:process", "produce.quality:final"}
|
||||
}
|
||||
exist, err := s.ctx.EntClient.Role.Query().Where(role.Code(r.code)).First(ctx)
|
||||
if err != nil {
|
||||
@@ -50,8 +52,9 @@ func (s *Service) Seed(ctx context.Context) error {
|
||||
} else {
|
||||
merged := mergeCodes(exist.PermissionCodes, codes)
|
||||
if r.code == "OPERATOR" {
|
||||
// 操作工不持质量检验录入/处置权限:完工检由质检员专属录入(P0-1 闭环)
|
||||
merged = dropCodes(merged, "produce.quality", "produce.quality:edit")
|
||||
// 操作工持「过程检」自检权限(produce.quality:process);不持「完工检」(produce.quality:final)。
|
||||
// 旧 produce.quality:edit 已拆为 process/final 两码(补1 权限粒度拆分),存量角色里的旧码一并摘除。
|
||||
merged = dropCodes(merged, "produce.quality:edit", "produce.quality:final")
|
||||
}
|
||||
_ = s.ctx.EntClient.Role.UpdateOneID(exist.ID).SetPermissionCodes(merged).Exec(ctx)
|
||||
}
|
||||
@@ -154,7 +157,8 @@ func (s *Service) Seed(ctx context.Context) error {
|
||||
{"sys.inspect:alarm", "巡检-异常上报", "BUTTON", "", "sys.inspect"},
|
||||
{"sys.inspect:view", "巡检-记录查看", "BUTTON", "", "sys.inspect"},
|
||||
{"sys.inspect:sign", "巡检-签字确认", "BUTTON", "", "sys.inspect"},
|
||||
{"produce.quality:edit", "质量检验-录入/处置", "BUTTON", "", "produce.quality"},
|
||||
{"produce.quality:process", "质量检验-过程检录入/处置", "BUTTON", "", "produce.quality"},
|
||||
{"produce.quality:final", "质量检验-完工检录入/处置", "BUTTON", "", "produce.quality"},
|
||||
// 附件中心(对齐 WMS attachment:manage 口径):删除 + 归档清理
|
||||
{"sys.attachment:manage", "附件-删除/归档清理", "BUTTON", "", "sys.attachment"},
|
||||
{"sys.settings:edit", "基础设置-修改", "BUTTON", "", "sys.settings"},
|
||||
@@ -191,6 +195,9 @@ func (s *Service) Seed(ctx context.Context) error {
|
||||
// 拧紧数据本体(/torque/report、/torque/records)与 torque_record 表保留,仅下线查询页与审核签字入口。
|
||||
// 清掉存量角色里的旧码与旧菜单/按钮权限定义行(幂等)。
|
||||
migrateLegacyTorque(ctx, s)
|
||||
// 3.7 旧「质量检验-录入/处置」权限码 produce.quality:edit 已拆分为 process/final 两码(补1 权限粒度拆分):
|
||||
// 摘掉存量角色里的旧码、删除旧权限定义行(幂等)。
|
||||
migrateLegacyQualityEdit(ctx, s)
|
||||
|
||||
// 4. 初始化产线工位(内置只读主数据);工艺流程/工序步骤由用户在「工艺流程」页自建,不预置
|
||||
seedStations(ctx, s)
|
||||
@@ -461,3 +468,32 @@ func migrateLegacyTorque(ctx context.Context, s *Service) {
|
||||
Where(permission.Code(code)).Exec(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// migrateLegacyQualityEdit 清理已拆分的旧「质量检验-录入/处置」权限码 produce.quality:edit(幂等,可重复执行):
|
||||
// 补1 权限粒度拆分后,该码被 produce.quality:process(过程检) / produce.quality:final(完工检) 取代。
|
||||
// 从存量角色的 permissionCodes 摘除旧码并删除其权限定义行,避免菜单树/接口残留失效入口。
|
||||
func migrateLegacyQualityEdit(ctx context.Context, s *Service) {
|
||||
deadCodes := map[string]bool{
|
||||
"produce.quality:edit": true,
|
||||
}
|
||||
roleList, _ := s.ctx.EntClient.Role.Query().All(ctx)
|
||||
for _, rl := range roleList {
|
||||
kept := make([]string, 0, len(rl.PermissionCodes))
|
||||
changed := false
|
||||
for _, c := range rl.PermissionCodes {
|
||||
if deadCodes[c] {
|
||||
changed = true
|
||||
continue
|
||||
}
|
||||
kept = append(kept, c)
|
||||
}
|
||||
if changed {
|
||||
_ = s.ctx.EntClient.Role.UpdateOneID(rl.ID).
|
||||
SetPermissionCodes(mergeCodes(kept, nil)).Exec(ctx)
|
||||
}
|
||||
}
|
||||
for code := range deadCodes {
|
||||
_, _ = s.ctx.EntClient.Permission.Delete().
|
||||
Where(permission.Code(code)).Exec(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,11 +96,26 @@ func (s *Service) InspectorSign(ctx context.Context, req InspectorSignReq, opera
|
||||
SetSignedBy(operator).SetRound(round).
|
||||
SetSource("inspect").SetRemark(req.Remark).Save(ctx)
|
||||
if err != nil {
|
||||
if isUniqueSignViolation(err) {
|
||||
// 并发签字竞态:唯一索引(sn,stationNo,stepId,signRole,round)冲突
|
||||
return errors.New("该步骤已签字,请勿重复提交")
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// isUniqueSignViolation 判定是否为签字唯一索引冲突(并发签字竞态)
|
||||
func isUniqueSignViolation(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := strings.ToLower(err.Error())
|
||||
return strings.Contains(msg, "duplicate key") ||
|
||||
strings.Contains(msg, "violates unique constraint") ||
|
||||
strings.Contains(msg, "step_sign_record")
|
||||
}
|
||||
|
||||
// PendingSignFilter 待检验员签字查询过滤。
|
||||
type PendingSignFilter struct {
|
||||
Sn string `json:"sn"`
|
||||
|
||||
@@ -29,8 +29,9 @@ type TorqueReq struct {
|
||||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
||||
// ReportTorque 接收拧紧数据(工位终端/内部接口上报),入库并做扭矩 RANGE 判定写 step_data
|
||||
func (s *Service) ReportTorque(ctx context.Context, req TorqueReq, operator string) error {
|
||||
// ReportTorque 接收拧紧数据(工位终端/内部接口上报),入库并做扭矩 RANGE 判定写 step_data。
|
||||
// operatorId:落库操作人用户ID,由 MES 从当前登录态 JWT 解析,即「本次发送所用的登录人」。
|
||||
func (s *Service) ReportTorque(ctx context.Context, req TorqueReq, operator string, operatorId int) error {
|
||||
if req.Sn == "" {
|
||||
return errors.New("sn 不能为空")
|
||||
}
|
||||
@@ -51,7 +52,7 @@ func (s *Service) ReportTorque(ctx context.Context, req TorqueReq, operator stri
|
||||
_, err := s.ctx.EntClient.TorqueRecord.Create().
|
||||
SetWorkOrderNo(workOrderNo).SetSn(req.Sn).SetScrewNo(req.ScrewNo).
|
||||
SetStrain(req.Strain).SetTorque(req.Strain).SetAngle(req.Angle).
|
||||
SetResult(result).SetOperator(req.Operator).SetStationNo(req.StationNo).
|
||||
SetResult(result).SetOperator(operator).SetOperatorId(operatorId).SetStationNo(req.StationNo).
|
||||
SetTime(req.Time).Save(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -219,6 +219,7 @@ type ReportProcessReq struct {
|
||||
Sn string `json:"sn"`
|
||||
FlowId int `json:"flowId"` // 报工工艺(缺省回退工位当前绑定的工艺)
|
||||
StationNo int `json:"stationNo"`
|
||||
Operator string `json:"operator"` // 操作工(工位终端登录人注入;内部接口无 JWT 时作为操作人来源,杜绝「system」伪造留痕)
|
||||
Steps []StepDataReq `json:"steps"`
|
||||
// 签字确认(需求B):needOperatorSign 步骤须带操作工签字(签字确认弹框采集),服务端强校验并落库
|
||||
Signs []StepSignInput `json:"signs"`
|
||||
@@ -248,7 +249,7 @@ func parseClientTime(s string) (time.Time, error) {
|
||||
// 绑料是"落到工艺"的强约束:工艺物料清单(flow_material)中要求装配的物料必须绑齐,
|
||||
// 电气件按单台用量逐颗SN、结构件至少一个批次号;缺料直接拒绝报工并返回缺料清单。
|
||||
// 顺序校验(工单工艺组合为唯一路线源头):报工工位必须在组合中、工艺匹配,且不可倒序(可跳过未选工位)。
|
||||
func (s *Service) ReportProcess(ctx context.Context, req ReportProcessReq, operator string) error {
|
||||
func (s *Service) ReportProcess(ctx context.Context, req ReportProcessReq, operator string, operatorId int) error {
|
||||
if req.Sn == "" || req.StationNo <= 0 {
|
||||
return errors.New("sn 与 stationNo 必填")
|
||||
}
|
||||
@@ -352,7 +353,7 @@ func (s *Service) ReportProcess(ctx context.Context, req ReportProcessReq, opera
|
||||
SetSn(req.Sn).SetFlowId(flowId).
|
||||
SetStationNo(itoa(req.StationNo)).SetOrderNo(wp.OrderNo).
|
||||
SetProcessName(s.flowNameById(ctx, flowId)).
|
||||
SetStatus("DONE").SetOperator(operator).SetResult("OK").
|
||||
SetStatus("DONE").SetOperator(operator).SetOperatorId(operatorId).SetResult("OK").
|
||||
SetStartedAt(startedAt).SetEndedAt(endedAt).SetDurationSec(durationSec).Save(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user