feat: 会话令牌与守卫、看板组件及工位终端功能完善

- 用户表扩展:MES/WMS user 实体新增字段,新增 active_token 迁移 SQL(2026-11-11)
- 会话机制:MES 新增 session_guard,重构 token/auth/jwt/perm 与路由;WMS/工作站同步令牌续签与鉴权
- 看板(board):调整 MES 看板产线/生产/侧栏/仓库组件及样式;WMS 看板展示页同步调整
- 工位终端后端:完善报警流、绑定、PLC 对账、拧紧、proxy、上报、站点告警/操作等 handler,补充 mes client/store
- 工位终端前端:登录/主界面/各操作面板调整,新增滚动分页与多语言 yaml 文案,刷新构建产物
- MES 业务:新增流程链 logic,调整待审核/产能支撑/产品工艺/设置页、PLC 监控与帮助文档,刷新构建产物
- 更新问题记录文档
This commit is contained in:
SunYF
2026-11-11 14:36:16 +08:00
parent 619fc8983d
commit 7b7d27d150
79 changed files with 2056 additions and 471 deletions
@@ -108,15 +108,21 @@ func clientIP(r *http.Request) string {
}
// LogoutHandler POST /api/v1/logout 退出登录(需登录 JWT)
// 记录退出日志;登录态清除由前端完成(无状态 JWT 无服务端会话)。
// 记录退出日志 + 清空 active_token(同账号互踢会话标识,2026-11-11):主动登出后该 Sid 立即失效。
func LogoutHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
username, _ := r.Context().Value("username").(string)
uid := uidFromRequest(r, svcCtx)
if username == "" {
if u, err := svcCtx.EntClient.User.Get(r.Context(), uidFromRequest(r, svcCtx)); err == nil {
if u, err := svcCtx.EntClient.User.Get(r.Context(), uid); err == nil {
username = u.Username
}
}
if uid > 0 {
if _, err := svcCtx.EntClient.User.UpdateOneID(uid).SetActiveToken("").Save(r.Context()); err != nil {
// 清理失败不阻断登出响应(token 本身仍在有效期内,仅互踢标识残留)
}
}
if username != "" {
svcCtx.EventLog.Write(r.Context(), "auth.logout", "", username, "auth", username,
"退出登录", map[string]any{"ip": clientIP(r)})
+2
View File
@@ -31,6 +31,8 @@ var pathPermMap = map[string]string{
// 数量不符处理(produce.qty 菜单下:补发/退库/调整关闭)
"POST:/api/v1/qty-reports/handle": "produce.qty:handle",
"POST:/api/v1/plc/send-process": "produce.plc:send",
// 报工后自动流转连锁的手动全线扫描(2026-11-11):与工位下发同级操作权限
"POST:/api/v1/plc/flow-scan": "produce.plc:send",
"POST:/api/v1/plc/mismatch/override": "produce.plc.mismatch:override",
"POST:/api/v1/torque/dispose": "produce.torque:dispose",
"POST:/api/v1/process-flows": "produce.processflow:add",
@@ -163,3 +163,16 @@ func PlcReleaseEndPointHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
httpx.OkMessage(w, "已释放", nil)
}
}
// ScanFlowChainHandler POST /plc/flow-scan 手动全线流转扫描:
// 与报工后自动连锁(triggerFlowChain)同一套逻辑(送下一工序候选工位 / 缓存策略 / 层层拉入)。
func ScanFlowChainHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, err := logic.New(svcCtx).ScanFlowChain(r.Context(), operator(r, ""))
if err != nil {
httpx.Fail(w, 3313, err.Error())
return
}
httpx.Ok(w, res)
}
}
+2 -1
View File
@@ -53,7 +53,8 @@ func RegisterRoutes(server *rest.Server, serverCtx *svc.ServiceContext) {
{Method: http.MethodGet, Path: "/backup/records", Handler: BackupRecordsHandler(serverCtx)},
}
server.AddRoutes(
rest.WithMiddlewares([]rest.Middleware{permissionGuard(serverCtx)}, jwtAdmin...),
// SessionGuard:后台同账号互踢(2026-11-11);permissionGuard:按钮级权限兜底
rest.WithMiddlewares([]rest.Middleware{SessionGuard(serverCtx), permissionGuard(serverCtx)}, jwtAdmin...),
rest.WithPrefix("/api/v1"),
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
rest.WithMaxBytes(serverCtx.Config.Upload.MaxMB<<20),
@@ -407,6 +407,9 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
{Method: http.MethodPost, Path: "/plc/monitor/position-correct", Handler: production.PlcPositionCorrectHandler(serverCtx)},
{Method: http.MethodGet, Path: "/plc/monitor/end-points", Handler: production.PlcEndPointOccupiedHandler(serverCtx)},
{Method: http.MethodPost, Path: "/plc/monitor/release-end-point", Handler: production.PlcReleaseEndPointHandler(serverCtx)},
// 报工后自动流转连锁(triggerFlowChain,2026-11-11 拍板):手动全线扫描走同一套逻辑
// (送下一工序候选工位 / 缓存策略 strict 进缓存位、满仓报警 / 模拟模式闭环)。
{Method: http.MethodPost, Path: "/plc/flow-scan", Handler: production.ScanFlowChainHandler(serverCtx)},
// PLC↔MES 对账检查(#8):终端 T2/T3 单工位(弹终端)、系统 T6/T7 全量 11 位(落预警中心)。事件驱动,禁止定时轮询。
// MES 前端(T7 手动核对)与管理后台走本 JWT 路由;工位终端经 /api/internal/plc/mismatch/check 代理。
@@ -592,7 +595,7 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
{Method: http.MethodGet, Path: "/todos/stats", Handler: production.TodoStatsHandler(serverCtx)},
}
server.AddRoutes(
rest.WithMiddlewares([]rest.Middleware{permissionGuard(serverCtx)}, jwtRoutes...),
rest.WithMiddlewares([]rest.Middleware{SessionGuard(serverCtx), permissionGuard(serverCtx)}, jwtRoutes...),
rest.WithPrefix("/api/v1"),
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
// 请求体上限取「Upload.MaxMB 与各文件类型 MaxSize 的最大值」,否则 go-zero 默认 1MB 会 413
@@ -0,0 +1,38 @@
package handler
import (
"net/http"
"strings"
httpx "bj_power_mes/common/httpx"
tokenx "bj_power_mes/common/token"
"bj_power_mes/ent/user"
"bj_power_mes/internal/svc"
)
// SessionGuard 后台同账号互踢守卫(2026-11-11 拍板:三条互踢并存——MES 后台同账号互踢、
// WMS 后台同账号互踢、工位终端同工位互踢;三端之间同账号登录互不影响)。
//
// 校验:JWT 内 Sid(后台登录时生成、写入 user.active_token、续签原样保留)
// 与 user.active_token 不一致 → 401「账号已在其他地方登录」。
// - 工位终端登录(StationLogin)不写 active_token、其 token 无 Sid,且终端只调 /api/internal/*
// (StationSessionAuth 只验签不查本守卫)→ 登录 MES 后台不会踢工位终端,反之亦然。
// - active_token 为空(从未后台登录 / 存量旧会话)→ 放行,下次后台登录后生效。
// - token 解析失败交由 go-zero rest.WithJwt 统一 401,本守卫不重复报错。
func SessionGuard(svcCtx *svc.ServiceContext) func(http.HandlerFunc) http.HandlerFunc {
return func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if strings.HasPrefix(auth, "Bearer ") {
if claims, err := tokenx.Parse(svcCtx.Config.Auth.AccessSecret, strings.TrimPrefix(auth, "Bearer ")); err == nil && claims.UserId > 0 {
if u, uerr := svcCtx.EntClient.User.Query().
Where(user.ID(claims.UserId)).First(r.Context()); uerr == nil && u.ActiveToken != "" && u.ActiveToken != claims.Sid {
httpx.FailHTTP(w, http.StatusUnauthorized, "账号已在其他地方登录,本会话已下线,请重新登录")
return
}
}
}
next(w, r)
}
}
}