feat(mes/wms/终端): 三端统一对时JWT验签 + 盘点按库位拆快照 + 档案删除口径 + 退库单数量修正(2026-11-24 全场整改)
【全系统 · 时间权威与鉴权(核心)】 - 新增 clock 包(三端各一份):谁签发 JWT 就以谁的时间验 exp——验签方向签发方取 /api/server-time(30s 缓存,取不到降级本机 + 10min 容差兜底),本端签发用本机时间 - 三端均注册 GET /api/server-time;MES/WMS/终端配同一份「签发方→取时间地址」映射 - 密钥 SHA256 前 8 位指纹日志([鉴权诊断]),现场比对两端指纹秒定位密钥不一致导致的 401「登录已失效」 - token 由 localStorage 改 sessionStorage;登录响应带服务器时间,偏差只提示「时间已自动校正」不拦截 - 现场人工 w32tm 对时操作卡作废(随文档留存,正常部署无需执行) 【WMS · 库存盘点重构】 - 快照粒度改「物料 × 库位」:同物料 A/B 区拆两条各盘各的;扫码命中多行弹库位清单逐库位录入、写回精确定位,行不存在报错不静默跳过 - GET /api/stocktake/active 返回全部进行中单(旧 .First() 只返一条导致历史多单看不见、库存长期冻结);前端加单号下拉切换,刷新保持当前单 - 历史卡死单只走「完成/取消」事务解冻,严禁删库;写回任何一行失败都强提示,不吞错 - 去「全库」,按区域筛选支持选到区/货架/层/号任意层级;「本次盘点范围」同步加筛选、物料按区域过滤;去多余「(可选)」字样 【WMS/MES · 档案删除与停用口径】 - 产品档案新建默认停用(product_process.status ACTIVE→INACTIVE),配齐工艺路线+总账+分账才允许启用 - 产品/物料档案删除按钮统一为一个(按 WMS 做法),仅「无任何引用」可物理删;引用检查覆盖两库、查库报错一律拒绝删除(fail-close,不再误删) - 产品编码自动生成,重复则自增保证唯一;权限对齐:停用走「编辑」、删除走独立「删除」码 - WMS 删物料同步核查 MES 引用,MES 不可达/删除失败则拒绝并落同步失败台账,无兼容补录行 【业务整改】 - MES 工单整体完工自动结算遗留 PENDING 叫料单(释放占用额度);已发货 SHIPPED 不动,仅落日志提示人工签收 - WMS 已收货退库单支持直接修正数量(问题14):原单不动、留痕(修正人/时间/原因/前后值),库存差额补扣,SN 件按最终 SN 集合增删行 - 日排产:「排产日期范围」→「排产日期」;平铺补产品编码/名称;「产品型号」→「产品」去产品编号/名称;平均分摊与每日固定互算;删除写操作日志 - 齐套测算:逐日消耗模拟按实际排产日期;工单详情增「总物料清单」(照搬产品档案总账,实时) - PLC 对账:虚拟/离线工位(OFFLINE,如 15)跳过 PLC 托盘位读取,不再为特例扔给 PLC - AGV 点位锁与回调细节按 11-20 现场整改继续修正(agv.go / agv_point_lock.go) 【文档】 - 新增《全系统业务深坑审计报告》《全系统串联审计报告》《现场对时操作卡》;更新产线API.md、案例.md、问题临时.md(记录甲方逐条原话与验收口径)
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"bj_power_mes/ent/user"
|
||||
"bj_power_mes/ent/userstation"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
@@ -196,6 +197,10 @@ type StationLoginResp struct {
|
||||
User UserInfoResp `json:"user"`
|
||||
Stations []int `json:"stations"`
|
||||
StationNo int `json:"stationNo"`
|
||||
// 2026-11-24 时钟偏差自检(第 2 层防御):携带 MES 服务器当前 unix 秒。终端/前端拿它
|
||||
// 与本机时间比对,偏差超阈值即告警——token 有效期按签发方时钟写入、验签方本地时间判定,
|
||||
// 两机时钟漂移过大会导致 token 一签出来就"过期"(现场表现为登录成功后立刻 401)。
|
||||
ServerTime int64 `json:"serverTime"`
|
||||
}
|
||||
|
||||
// StationOption 登录页工位选项(2026-11-09 拍板:未配置工位的终端登录页可选实体+虚拟工位)。
|
||||
@@ -256,10 +261,11 @@ func (s *Service) StationLogin(ctx context.Context, req StationLoginReq) (Statio
|
||||
}
|
||||
}
|
||||
}
|
||||
if req.StationNo > 0 {
|
||||
if req.StationNo != 0 {
|
||||
// 虚拟工位(线下位 OFFLINE)免工位权限校验(2026-11-11 多会话拍板):虚拟工位不连 PLC、
|
||||
// 不占产线点位、仅记录作业,权限体系面向实体工位——任何已开通工位终端登录的账号均可登录。
|
||||
// 存在性仍以 station 表为准(存在且 ENABLED 的 OFFLINE 才放行);实体工位保持原权限校验。
|
||||
// 2026-11-24:负数前段虚拟工位同走此校验(!=0 而非 >0),不存在的负数工位同样拒绝,杜绝越权登录任意负数。
|
||||
if st, gerr := s.ctx.EntClient.Station.Query().
|
||||
Where(station.StationNo(req.StationNo)).First(ctx); gerr == nil && st.Status == "ENABLED" && st.StationType == "OFFLINE" {
|
||||
// 放行
|
||||
@@ -286,8 +292,25 @@ func (s *Service) StationLogin(ctx context.Context, req StationLoginReq) (Statio
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
// 2026-11-24 修复:工位终端登录此前不写最近登录时间,账号管理「最近登录」恒显示"从未登录"。
|
||||
// 只补 LastLoginAt,不写 activeToken——工位登录与后台登录的互踢体系互不影响(见 Login 内注释)。
|
||||
// 写失败不阻塞登录(与 Login 同口径)。
|
||||
now := time.Now().Unix()
|
||||
if err := s.ctx.EntClient.User.UpdateOneID(usr.ID).SetLastLoginAt(now).Exec(ctx); err != nil {
|
||||
// 不阻塞登录,仅记日志
|
||||
}
|
||||
resp.AccessToken = access
|
||||
resp.ExpireAt = time.Now().Add(time.Duration(s.ctx.Config.Auth.AccessExpire) * time.Second).Unix()
|
||||
resp.ServerTime = now
|
||||
// 2026-11-24 登录诊断:工位登录全量留痕(密钥指纹/有效期/签发到期/token 指纹)。现场把这条与
|
||||
// 终端 401 时的 [鉴权诊断] 指纹比对:指纹不同=两端密钥不统一(401 根因);时间对不上=时钟漂移。
|
||||
tp := access
|
||||
if len(tp) > 24 {
|
||||
tp = tp[:24]
|
||||
}
|
||||
logx.WithContext(ctx).Infof("[工位登录诊断] 登录成功 user=%s(id=%d) 工位=%d 密钥指纹=%s 有效期=%ds 签发=%s 到期unix=%d token前24=%s",
|
||||
req.Username, usr.ID, req.StationNo, tokenx.Fingerprint(s.ctx.Config.Auth.AccessSecret),
|
||||
s.ctx.Config.Auth.AccessExpire, time.Now().Format("2006-01-02 15:04:05"), resp.ExpireAt, tp)
|
||||
resp.User = UserInfoResp{UserId: usr.ID, Username: usr.Username, Name: usr.Name, RoleId: usr.RoleId}
|
||||
if roleEntity != nil {
|
||||
resp.User.RoleCode = roleEntity.Code
|
||||
@@ -344,7 +367,11 @@ func (s *Service) StationChangePassword(ctx context.Context, userId int, oldPwd,
|
||||
if bcrypt.CompareHashAndPassword([]byte(usr.WorkstationPassword), []byte(oldPwd)) != nil {
|
||||
return errors.New("原工位终端密码不正确")
|
||||
}
|
||||
hash, _ := bcrypt.GenerateFromPassword([]byte(newPwd), bcrypt.DefaultCost)
|
||||
// 2026-11-23 吞错修复:此前 hash,_ 吞掉生成错误 → 把空密码写库,账号从此密码校验恒过(安全洞)
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(newPwd), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return errors.New("密码加密失败:" + err.Error())
|
||||
}
|
||||
return s.ctx.EntClient.User.UpdateOneID(userId).SetWorkstationPassword(string(hash)).Exec(ctx)
|
||||
}
|
||||
|
||||
@@ -434,7 +461,11 @@ func (s *Service) UpdateUser(ctx context.Context, req UserReq) error {
|
||||
if err := checkPasswordStrong(req.Password); err != nil {
|
||||
return err
|
||||
}
|
||||
hash, _ := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
// 2026-11-23 吞错修复:此前 hash,_ 吞错会把空密码写库(安全洞)
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return errors.New("密码加密失败:" + err.Error())
|
||||
}
|
||||
u.SetPassword(string(hash))
|
||||
}
|
||||
if req.CanLoginWorkstation {
|
||||
@@ -442,7 +473,10 @@ func (s *Service) UpdateUser(ctx context.Context, req UserReq) error {
|
||||
if err := checkPasswordStrong(req.WorkstationPassword); err != nil {
|
||||
return errors.New("工位终端密码" + err.Error())
|
||||
}
|
||||
wsHash, _ := bcrypt.GenerateFromPassword([]byte(req.WorkstationPassword), bcrypt.DefaultCost)
|
||||
wsHash, err := bcrypt.GenerateFromPassword([]byte(req.WorkstationPassword), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return errors.New("工位终端密码加密失败:" + err.Error())
|
||||
}
|
||||
u.SetWorkstationPassword(string(wsHash))
|
||||
}
|
||||
} else {
|
||||
@@ -495,6 +529,10 @@ func (s *Service) DeleteUser(ctx context.Context, id int) error {
|
||||
if usr.Username == "admin" {
|
||||
return errors.New("系统内置管理员账号不允许删除")
|
||||
}
|
||||
// 先清工位绑定,避免 user_station 残留孤儿行(引用已删用户 id)
|
||||
if _, err := s.ctx.EntClient.UserStation.Delete().Where(userstation.UserId(id)).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.ctx.EntClient.User.DeleteOneID(id).Exec(ctx)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user