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:
@@ -2,7 +2,10 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
tokenx "bj_power_mes/common/token"
|
||||
@@ -36,9 +39,18 @@ func (s *Service) sessionExpire() int64 {
|
||||
return 3600
|
||||
}
|
||||
|
||||
// currentToken 签发 access token(滑动续签模式,无 refresh)
|
||||
func (s *Service) issueTokens(usr *ent.User, roleEntity *ent.Role) (LoginResp, error) {
|
||||
claims := tokenx.Claims{UserId: usr.ID, Username: usr.Username, Name: usr.Name, RoleId: usr.RoleId}
|
||||
// newSessionID 生成后台登录会话标识(32 位 hex,随机 16 字节)
|
||||
func newSessionID() string {
|
||||
b := make([]byte, 16)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return fmt.Sprintf("%d", time.Now().UnixNano())
|
||||
}
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
// currentToken 签发 access token(滑动续签模式,无 refresh);sid=后台登录会话标识(互踢用,工位终端登录传空)
|
||||
func (s *Service) issueTokens(usr *ent.User, roleEntity *ent.Role, sid string) (LoginResp, error) {
|
||||
claims := tokenx.Claims{UserId: usr.ID, Username: usr.Username, Name: usr.Name, RoleId: usr.RoleId, Sid: sid}
|
||||
if roleEntity != nil {
|
||||
claims.RoleCode = roleEntity.Code
|
||||
claims.RoleName = roleEntity.Name
|
||||
@@ -87,14 +99,16 @@ func (s *Service) Login(ctx context.Context, req LoginReq) (LoginResp, error) {
|
||||
if bcrypt.CompareHashAndPassword([]byte(usr.Password), []byte(req.Password)) != nil {
|
||||
return resp, errors.New("用户名或密码错误")
|
||||
}
|
||||
// 记录最近登录时间(仅后台登录;工位终端 StationLogin 不计入)
|
||||
// 记录最近登录时间 + 写入新会话标识 activeToken=SID(同账号互踢:新登录顶掉旧后台会话;
|
||||
// 工位终端登录走 StationLogin,不写本字段,互不影响。写失败不阻塞登录)
|
||||
now := time.Now().Unix()
|
||||
if err := s.ctx.EntClient.User.UpdateOneID(usr.ID).SetLastLoginAt(now).Exec(ctx); err != nil {
|
||||
sid := newSessionID()
|
||||
if err := s.ctx.EntClient.User.UpdateOneID(usr.ID).SetLastLoginAt(now).SetActiveToken(sid).Exec(ctx); err != nil {
|
||||
// 不阻塞登录,仅记日志
|
||||
}
|
||||
usr.LastLoginAt = &now
|
||||
roleEntity, _ := s.ctx.EntClient.Role.Get(ctx, usr.RoleId)
|
||||
return s.issueTokens(usr, roleEntity)
|
||||
return s.issueTokens(usr, roleEntity, sid)
|
||||
}
|
||||
|
||||
// UserInfo 当前用户信息 + 菜单权限
|
||||
@@ -243,15 +257,23 @@ func (s *Service) StationLogin(ctx context.Context, req StationLoginReq) (Statio
|
||||
}
|
||||
}
|
||||
if req.StationNo > 0 {
|
||||
allowed := false
|
||||
for _, no := range stations {
|
||||
if no == req.StationNo {
|
||||
allowed = true
|
||||
break
|
||||
// 虚拟工位(线下位 OFFLINE)免工位权限校验(2026-11-11 多会话拍板):虚拟工位不连 PLC、
|
||||
// 不占产线点位、仅记录作业,权限体系面向实体工位——任何已开通工位终端登录的账号均可登录。
|
||||
// 存在性仍以 station 表为准(存在且 ENABLED 的 OFFLINE 才放行);实体工位保持原权限校验。
|
||||
if st, gerr := s.ctx.EntClient.Station.Query().
|
||||
Where(station.StationNo(req.StationNo)).First(ctx); gerr == nil && st.Status == "ENABLED" && st.StationType == "OFFLINE" {
|
||||
// 放行
|
||||
} else {
|
||||
allowed := false
|
||||
for _, no := range stations {
|
||||
if no == req.StationNo {
|
||||
allowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !allowed {
|
||||
return resp, errors.New("该账号无此工位的操作权限")
|
||||
}
|
||||
}
|
||||
if !allowed {
|
||||
return resp, errors.New("该账号无此工位的操作权限")
|
||||
}
|
||||
}
|
||||
roleEntity, _ := s.ctx.EntClient.Role.Get(ctx, usr.RoleId)
|
||||
|
||||
Reference in New Issue
Block a user