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:
@@ -46,10 +46,18 @@ func loginHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
fail(w, http.StatusBadRequest, "用户名和密码必填")
|
||||
return
|
||||
}
|
||||
// 2026-11-09 拍板:配置固定工位(>0)时忽略前端传值、强制用配置工位(防串位);
|
||||
// 未配置(<=0)时接受前端选择的工位(实体+虚拟 0/13 都可选,MES 端对 <=0 免工位权限校验)。
|
||||
// 2026-11-09 拍板:配置固定工位(>0)时忽略前端传值、强制用配置工位(防串位)。
|
||||
// 2026-11-11 多会话拍板:未配置(<=0)的终端实例(虚拟工位专用机)只允许登录虚拟工位,
|
||||
// 拒绝实体工位(后端硬校验防前端绕过;校验依据 MES 工位清单 stationType=OFFLINE)。
|
||||
if ctx.Config.Station.StationNo > 0 {
|
||||
req.StationNo = ctx.Config.Station.StationNo
|
||||
} else {
|
||||
virtual, vErr := isVirtualStation(ctx, req.StationNo)
|
||||
if vErr == nil && !virtual {
|
||||
fail(w, http.StatusForbidden, "本终端未配置固定工位,仅允许登录虚拟工位")
|
||||
return
|
||||
}
|
||||
// vErr != nil:工位清单拉取失败,放行走登录链路(登录本身也依赖 MES,不可达时自然失败)
|
||||
}
|
||||
|
||||
body, _, err := ctx.Mes.Post("/api/internal/station/login", map[string]any{
|
||||
@@ -84,22 +92,17 @@ func loginHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &loginResp); err == nil && loginResp.Data.AccessToken != "" {
|
||||
// 后登录踢前一个:若已存在不同 token 的旧会话,先通知 MES 记录旧会话登出(尽力而为),
|
||||
// 随后用新会话覆盖本地单行登录态;旧端下一次请求因 token 与会话不一致被 401 踢下线。
|
||||
if old, oerr := ctx.Store.GetStationSession(); oerr == nil && old != nil &&
|
||||
old.Token != "" && old.Token != loginResp.Data.AccessToken {
|
||||
// 多会话(2026-11-11):同工位只允许一个会话——该工位已有会话则先踢旧会话
|
||||
//(ReplaceStationSession 返回被踢者),通知 MES 记录旧会话登出(尽力而为);
|
||||
// 不同工位会话互不影响。被踢端下一次请求因 token 不在会话表中被 401 踢下线,
|
||||
// 其本地记录随会话删除;工位物料/工件信息在 MES 侧按工位号存,不随会话删除。
|
||||
kicked, serr := ctx.Store.SaveStationSession(loginResp.Data.AccessToken, loginResp.Data.User.Username,
|
||||
loginResp.Data.User.Name, loginResp.Data.StationNo, loginResp.Data.ExpireAt, userIDOf(ctx, loginResp.Data.AccessToken))
|
||||
if serr == nil && kicked != nil && kicked.Token != "" {
|
||||
go func(u string, n int) {
|
||||
_, _, _ = ctx.Mes.Post("/api/internal/station/logout", map[string]any{"username": u, "stationNo": n})
|
||||
}(old.Username, old.StationNo)
|
||||
}(kicked.Username, kicked.StationNo)
|
||||
}
|
||||
// 解析 MES JWT 取出用户 ID,随登录态保存:离线数据落库 operator_id(按操作人隔离发送)
|
||||
userID := int64(0)
|
||||
if claims, perr := auth.ParseToken(ctx.Config.Auth.AccessSecret, loginResp.Data.AccessToken); perr == nil {
|
||||
userID = claims.UserID
|
||||
}
|
||||
_ = ctx.Store.SaveStationSession(loginResp.Data.AccessToken, loginResp.Data.User.Username,
|
||||
loginResp.Data.User.Name, loginResp.Data.StationNo, loginResp.Data.ExpireAt, userID)
|
||||
ctx.Mes.SetStationToken(loginResp.Data.AccessToken)
|
||||
// 拧紧枪配置快照补报(2026-11-09):仅 MES 展示记录,不参与数据收集;
|
||||
// 启动时若 MES 未就绪,登录这一刻补上,失败只记日志不影响登录。
|
||||
go func(no int) {
|
||||
@@ -117,6 +120,43 @@ func loginHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// isVirtualStation 查询 MES 工位清单判断工位号是否虚拟工位(stationType=OFFLINE)。
|
||||
// 多会话拍板(2026-11-11):未配置固定工位的终端实例只允许登录虚拟工位。
|
||||
func isVirtualStation(ctx *svc.ServiceContext, stationNo int) (bool, error) {
|
||||
if stationNo <= 0 {
|
||||
return false, nil // 0/负数不是合法工位(原 0 号上线位已废弃)
|
||||
}
|
||||
body, _, err := ctx.Mes.Get("/api/internal/station/options", nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var opts struct {
|
||||
Code int `json:"code"`
|
||||
Data []struct {
|
||||
StationNo int `json:"stationNo"`
|
||||
Name string `json:"name"`
|
||||
StationType string `json:"stationType"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &opts); err != nil {
|
||||
return false, err
|
||||
}
|
||||
for _, o := range opts.Data {
|
||||
if o.StationNo == stationNo {
|
||||
return o.StationType == "OFFLINE", nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// userIDOf 解析 MES JWT 取用户 ID(随登录态保存,离线数据落库 operator_id)。
|
||||
func userIDOf(ctx *svc.ServiceContext, token string) int64 {
|
||||
if claims, err := auth.ParseToken(ctx.Config.Auth.AccessSecret, token); err == nil {
|
||||
return claims.UserID
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// logoutHandler POST /api/station/logout 退出登录:转发 MES 记录"工位终端退出"操作日志。
|
||||
// MES 不可达时静默忽略(日志不阻塞本地退出)。
|
||||
func logoutHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
@@ -126,9 +166,16 @@ func logoutHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
StationNo int `json:"stationNo"`
|
||||
}
|
||||
_ = parseJSON(r, &req)
|
||||
// 多会话版(2026-11-11):按请求 Bearer 定位自己的会话,只删自己那一行,不影响其他工位会话。
|
||||
token := bearerOf(r)
|
||||
ss, _ := ctx.Store.GetStationSessionByToken(token)
|
||||
username := strings.TrimSpace(req.Username)
|
||||
no := req.StationNo
|
||||
if ss != nil {
|
||||
username = ss.Username
|
||||
no = ss.StationNo
|
||||
}
|
||||
if username != "" {
|
||||
no := req.StationNo
|
||||
if no <= 0 {
|
||||
no = ctx.Config.Station.StationNo
|
||||
}
|
||||
@@ -136,9 +183,8 @@ func logoutHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
_, _, _ = ctx.Mes.Post("/api/internal/station/logout", map[string]any{"username": u, "stationNo": n})
|
||||
}(username, no)
|
||||
}
|
||||
// 方案 B:退出即清除本地登录态与 Bearer 凭证
|
||||
_ = ctx.Store.ClearStationSession()
|
||||
ctx.Mes.SetStationToken("")
|
||||
// 方案 B:退出即清除自己的本地登录态(多会话:仅本会话行)
|
||||
_ = ctx.Store.ClearSessionByToken(token)
|
||||
ok(w, map[string]any{"ok": true})
|
||||
}
|
||||
}
|
||||
@@ -148,7 +194,8 @@ func logoutHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
// 由前端根组件全局事件监听(click/touchstart/keydown/scroll 捕获阶段)调用,节流由前端控制。
|
||||
func touchHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := ctx.Store.TouchStationSession(); err != nil {
|
||||
// 多会话版:按请求 Bearer 只刷当前会话的空闲计时
|
||||
if err := ctx.Store.TouchSessionByToken(bearerOf(r)); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "刷新登录态失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
@@ -158,15 +205,17 @@ func touchHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
|
||||
// refreshTokenHandler POST /api/auth/refresh-token 滑动续签 Bearer:
|
||||
// 用当前有效 MES JWT 向 MES 换取新签发的 JWT(延长有效期),避免作业中令牌过期被登出。
|
||||
// 前置 Interceptor 已校验前端登录态;此处再代理 MES 刷新并回写本地 Bearer。
|
||||
// 前置 Interceptor 已校验前端登录态;此处再代理 MES 刷新。
|
||||
// 多会话版(2026-11-11):按请求 Bearer 只续签本会话。
|
||||
func refreshTokenHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ss, err := ctx.Store.GetStationSession()
|
||||
token := bearerOf(r)
|
||||
ss, err := ctx.Store.GetStationSessionByToken(token)
|
||||
if err != nil || ss == nil || ss.Token == "" {
|
||||
fail(w, http.StatusUnauthorized, "未登录或登录态已失效")
|
||||
return
|
||||
}
|
||||
respBody, _, err := ctx.Mes.Post("/api/internal/station/refresh-token", nil)
|
||||
respBody, _, err := ctx.Mes.Post("/api/internal/station/refresh-token", nil, token)
|
||||
if err != nil {
|
||||
fail(w, http.StatusUnauthorized, "MES 续签失败:"+errString(err))
|
||||
return
|
||||
@@ -175,13 +224,12 @@ func refreshTokenHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
Code int `json:"code"`
|
||||
Data struct {
|
||||
AccessToken string `json:"accessToken"`
|
||||
ExpireAt int64 `json:"expireAt"`
|
||||
ExpireAt int64 `json:"expireAt"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(respBody, &rr); err == nil && rr.Data.AccessToken != "" {
|
||||
// 只更新转发 MES 用的 Bearer;本地会话 token 保持与前端一致,
|
||||
// 由滑动续签的 X-Renewed-Token + SessionTokenSync 同步,避免前后端 token 不一致被误判为旧会话。
|
||||
ctx.Mes.SetStationToken(rr.Data.AccessToken)
|
||||
// MES 重签 token 仅作响应回执(前端当前不消费);本地会话 token 与前端保持一致,
|
||||
// 由滑动续签的 X-Renewed-Token + SessionTokenSync(old,new) 同步,避免前后端 token 不一致被误判。
|
||||
ok(w, map[string]any{"accessToken": rr.Data.AccessToken, "expireAt": rr.Data.ExpireAt})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user