feat: 实现工位终端滑动续签机制,强化安全与身份追溯
- 新增滑动续签功能:人工触屏触发本地心跳 + 30秒节流刷新JWT,避免作业中令牌过期 - 拆分质量检验权限:过程检/完工检使用不同权限码,细化权限粒度 - 新增操作人用户ID字段:报工、拧紧记录落库时携带可信用户ID,解决身份追溯问题 - 移除编译期默认密钥,启动期校验所有必要密钥非空,防止密钥泄露风险 - 修复WMS附件越权遍历问题:使用UUID文件名替代顺序ID访问附件 - 实现登录态过期数据隔离:仅返回当前操作人的待同步数据,避免身份混淆
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bj_power_workstation/internal/auth"
|
||||
"bj_power_workstation/internal/svc"
|
||||
)
|
||||
|
||||
@@ -50,6 +52,29 @@ func loginHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
fail(w, http.StatusBadGateway, "MES 服务不可达:"+errString(err))
|
||||
return
|
||||
}
|
||||
// 方案 B:落盘工位登录态 JWT,作为后续报工/拧紧的可信身份锚点
|
||||
var loginResp struct {
|
||||
Code int `json:"code"`
|
||||
Data struct {
|
||||
AccessToken string `json:"accessToken"`
|
||||
ExpireAt int64 `json:"expireAt"`
|
||||
StationNo int `json:"stationNo"`
|
||||
User struct {
|
||||
Username string `json:"username"`
|
||||
Name string `json:"name"`
|
||||
} `json:"user"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &loginResp); err == nil && loginResp.Data.AccessToken != "" {
|
||||
// 解析 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)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(body)
|
||||
@@ -65,6 +90,12 @@ func logoutHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
StationNo int `json:"stationNo"`
|
||||
}
|
||||
_ = parseJSON(r, &req)
|
||||
// 退出前先统计「当前登录人」尚未成功上报的本地数据条数,供前端提醒本人处理
|
||||
pendingQueue, pendingTorque := int64(0), int64(0)
|
||||
if ss, sErr := ctx.Store.GetStationSession(); sErr == nil && ss != nil {
|
||||
pendingQueue, _ = ctx.Store.PendingQueueCountForOperator(ss.UserID)
|
||||
pendingTorque, _ = ctx.Store.PendingTorqueCountForOperator(ss.UserID)
|
||||
}
|
||||
username := strings.TrimSpace(req.Username)
|
||||
if username != "" {
|
||||
no := req.StationNo
|
||||
@@ -75,10 +106,66 @@ 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("")
|
||||
ok(w, map[string]any{
|
||||
"pendingQueue": pendingQueue,
|
||||
"pendingTorque": pendingTorque,
|
||||
"pendingTotal": pendingQueue + pendingTorque,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// touchHandler POST /api/auth/touch 滑动续签心跳(修订需求):任何人类触屏行为触发,
|
||||
// 立即刷新本地登录态空闲计时 last_active_at(零成本本地写),防止作业中误登出。
|
||||
// 由前端根组件全局事件监听(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 {
|
||||
fail(w, http.StatusInternalServerError, "刷新登录态失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
ok(w, nil)
|
||||
}
|
||||
}
|
||||
|
||||
// refreshTokenHandler POST /api/auth/refresh-token 滑动续签 Bearer:
|
||||
// 用当前有效 MES JWT 向 MES 换取新签发的 JWT(延长有效期),避免作业中令牌过期被登出。
|
||||
// 前置 Interceptor 已校验前端登录态;此处再代理 MES 刷新并回写本地 Bearer。
|
||||
func refreshTokenHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ss, err := ctx.Store.GetStationSession()
|
||||
if err != nil || ss == nil || ss.Token == "" {
|
||||
fail(w, http.StatusUnauthorized, "未登录或登录态已失效")
|
||||
return
|
||||
}
|
||||
respBody, _, err := ctx.Mes.Post("/api/internal/station/refresh-token", nil)
|
||||
if err != nil {
|
||||
fail(w, http.StatusUnauthorized, "MES 续签失败:"+errString(err))
|
||||
return
|
||||
}
|
||||
var rr struct {
|
||||
Code int `json:"code"`
|
||||
Data struct {
|
||||
AccessToken string `json:"accessToken"`
|
||||
ExpireAt int64 `json:"expireAt"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(respBody, &rr); err == nil && rr.Data.AccessToken != "" {
|
||||
userID := ss.UserID
|
||||
if claims, perr := auth.ParseToken(ctx.Config.Auth.AccessSecret, rr.Data.AccessToken); perr == nil {
|
||||
userID = claims.UserID
|
||||
}
|
||||
_ = ctx.Store.SaveStationSession(rr.Data.AccessToken, ss.Username, ss.Name, ss.StationNo, rr.Data.ExpireAt, userID)
|
||||
ctx.Mes.SetStationToken(rr.Data.AccessToken)
|
||||
ok(w, map[string]any{"accessToken": rr.Data.AccessToken, "expireAt": rr.Data.ExpireAt})
|
||||
return
|
||||
}
|
||||
fail(w, http.StatusUnauthorized, "MES 续签响应异常")
|
||||
}
|
||||
}
|
||||
|
||||
// configHandler GET /api/config 返回工位配置:固定工位号 stationNo、固定接驳台 dock
|
||||
// (均由 yaml 配置在启动时加载,终端不可修改)。
|
||||
func configHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
@@ -88,4 +175,4 @@ func configHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
"dock": ctx.Config.Station.Dock,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,6 +72,17 @@ func processDoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// 方案 B:操作人取自工位登录态可信身份,忽略浏览器请求体里的 operator(防冒名);
|
||||
// operator_id 用于离线队列按操作人隔离发送(仅当前登录人自己的数据被发送)
|
||||
op := req.Operator
|
||||
opID := int64(0)
|
||||
if ss, sErr := ctx.Store.GetStationSession(); sErr == nil && ss != nil {
|
||||
if ss.Name != "" {
|
||||
op = ss.Name
|
||||
}
|
||||
opID = ss.UserID
|
||||
}
|
||||
|
||||
// 先尽力冲刷历史积压,失败不影响本次上报
|
||||
ctx.Syncer.RunOnce(r.Context())
|
||||
|
||||
@@ -80,8 +91,7 @@ func processDoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
"orderNo": req.OrderNo,
|
||||
"flowId": req.FlowId,
|
||||
"stationNo": req.StationNo,
|
||||
"steps": req.Steps,
|
||||
"operator": req.Operator,
|
||||
"operator": op,
|
||||
}
|
||||
if len(req.Binds) > 0 {
|
||||
body["binds"] = req.Binds
|
||||
@@ -94,12 +104,12 @@ func processDoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
payloadBytes, _ := json.Marshal(body)
|
||||
|
||||
ctx.Store.AddEventLog(req.OrderNo, req.Operator, "process_done",
|
||||
ctx.Store.AddEventLog(req.OrderNo, op, "process_done",
|
||||
"工艺完成上报 sn="+req.Sn+" stationNo="+itoa(req.StationNo)+" flowId="+itoa(req.FlowId)+" binds="+itoa(len(req.Binds)))
|
||||
|
||||
respBody, _, err := ctx.Mes.Post("/api/internal/station/report", json.RawMessage(payloadBytes))
|
||||
if err != nil {
|
||||
if qerr := ctx.Store.InsertQueueItem("process_done", string(payloadBytes)); qerr != nil {
|
||||
if qerr := ctx.Store.InsertQueueItem("process_done", string(payloadBytes), opID, op); qerr != nil {
|
||||
fail(w, http.StatusInternalServerError, "上报失败且离线队列写入失败:"+qerr.Error())
|
||||
return
|
||||
}
|
||||
@@ -139,6 +149,17 @@ func tempStoreHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// 方案 B:操作人取自工位登录态可信身份,忽略浏览器请求体里的 operator(防冒名);
|
||||
// operator_id 用于离线队列按操作人隔离发送(仅当前登录人自己的数据被发送)
|
||||
op := req.Operator
|
||||
opID := int64(0)
|
||||
if ss, sErr := ctx.Store.GetStationSession(); sErr == nil && ss != nil {
|
||||
if ss.Name != "" {
|
||||
op = ss.Name
|
||||
}
|
||||
opID = ss.UserID
|
||||
}
|
||||
|
||||
// 先尽力冲刷历史积压,失败不影响本次上报
|
||||
ctx.Syncer.RunOnce(r.Context())
|
||||
|
||||
@@ -147,16 +168,16 @@ func tempStoreHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
"orderNo": req.OrderNo,
|
||||
"stationNo": req.StationNo,
|
||||
"flowId": req.FlowId,
|
||||
"operator": req.Operator,
|
||||
"operator": op,
|
||||
}
|
||||
payloadBytes, _ := json.Marshal(body)
|
||||
|
||||
ctx.Store.AddEventLog(req.OrderNo, req.Operator, "temp_store",
|
||||
ctx.Store.AddEventLog(req.OrderNo, op, "temp_store",
|
||||
"暂存/退回库房 sn="+req.Sn+" stationNo="+itoa(req.StationNo))
|
||||
|
||||
respBody, _, err := ctx.Mes.Post("/api/internal/station/checkin", json.RawMessage(payloadBytes))
|
||||
if err != nil {
|
||||
if qerr := ctx.Store.InsertQueueItem("temp_store", string(payloadBytes)); qerr != nil {
|
||||
if qerr := ctx.Store.InsertQueueItem("temp_store", string(payloadBytes), opID, op); qerr != nil {
|
||||
fail(w, http.StatusInternalServerError, "上报失败且离线队列写入失败:"+qerr.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -30,6 +30,10 @@ func RegisterHandlers(server *rest.Server, ctx *svc.ServiceContext) {
|
||||
// 业务接口(均需登录 JWT;PDF 代理除外——原样转发文件流)
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
// 滑动续签(修订需求):任何人工触屏触发;touch 刷本地空闲计时,
|
||||
// refresh-token 节流 30 秒换新 MES Bearer,作业中令牌不因固定有效期过期
|
||||
{Method: http.MethodPost, Path: "/api/auth/touch", Handler: touchHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/auth/refresh-token", Handler: refreshTokenHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/task/current", Handler: taskCurrentHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/bind/panel", Handler: bindPanelHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/bind/record", Handler: bindRecordHandler(ctx)},
|
||||
|
||||
@@ -17,18 +17,23 @@ func syncFlushHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// syncStatsHandler GET /api/sync/stats 返回待同步条数统计。
|
||||
// syncStatsHandler GET /api/sync/stats 返回「当前登录人」尚未成功上报的本地数据(失败列表),
|
||||
// 供前端在「同一人重新登录」时仅展示失败信息(不自动打开页面)。他人数据不被返回、不可见。
|
||||
func syncStatsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
pendingTorque, err1 := ctx.Store.PendingTorqueCount()
|
||||
pendingQueue, err2 := ctx.Store.PendingQueueCount()
|
||||
if err1 != nil || err2 != nil {
|
||||
fail(w, http.StatusInternalServerError, "查询待同步统计失败")
|
||||
ss, err := ctx.Store.GetStationSession()
|
||||
if err != nil || ss == nil {
|
||||
ok(w, map[string]any{"pendingTorque": 0, "pendingQueue": 0, "pendingTotal": 0, "queue": []any{}, "torque": []any{}})
|
||||
return
|
||||
}
|
||||
queue, _ := ctx.Store.PendingQueueItemsForOperator(ss.UserID, 200)
|
||||
torque, _ := ctx.Store.PendingTorqueResultsForOperator(ss.UserID, 200)
|
||||
ok(w, map[string]any{
|
||||
"pendingTorque": pendingTorque,
|
||||
"pendingQueue": pendingQueue,
|
||||
"pendingQueue": len(queue),
|
||||
"pendingTorque": len(torque),
|
||||
"pendingTotal": len(queue) + len(torque),
|
||||
"queue": queue,
|
||||
"torque": torque,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -79,6 +79,16 @@ func tighteningRecordHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
if req.Result != "NG" {
|
||||
req.Result = "OK"
|
||||
}
|
||||
// 方案 B:操作人取自工位登录态可信身份,忽略浏览器请求体里的 operator(防冒名)
|
||||
ss, _ := ctx.Store.GetStationSession()
|
||||
op := req.Operator
|
||||
if ss != nil && ss.Name != "" {
|
||||
op = ss.Name
|
||||
}
|
||||
opID := int64(0)
|
||||
if ss != nil {
|
||||
opID = ss.UserID
|
||||
}
|
||||
dockCode := fmt.Sprintf("DOCK%02d", ctx.Config.Station.StationNo)
|
||||
id, err := ctx.Store.InsertTorqueResult(store.TorqueResult{
|
||||
WorkOrderNo: req.WorkOrderNo,
|
||||
@@ -90,13 +100,14 @@ func tighteningRecordHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
Torque: req.Torque,
|
||||
Angle: req.Angle,
|
||||
Result: req.Result,
|
||||
Operator: req.Operator,
|
||||
Operator: op,
|
||||
OperatorID: opID,
|
||||
})
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, "拧紧数据入库失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
ctx.Store.AddEventLog(req.WorkOrderNo, req.Operator, "torque_record",
|
||||
ctx.Store.AddEventLog(req.WorkOrderNo, op, "torque_record",
|
||||
fmt.Sprintf("拧紧录入 sn=%s 步骤=%s 扭矩=%.1f 结果=%s", req.Sn, req.StepName, req.Torque, req.Result))
|
||||
ok(w, map[string]any{"id": id, "result": req.Result})
|
||||
}
|
||||
|
||||
@@ -111,6 +111,15 @@ func onlineHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
ctx.Syncer.RunOnce(r.Context())
|
||||
// 录入时操作人(当前登录人),供离线队列按操作人隔离发送
|
||||
opID := int64(0)
|
||||
opName := req.Operator
|
||||
if ss, sErr := ctx.Store.GetStationSession(); sErr == nil && ss != nil {
|
||||
opID = ss.UserID
|
||||
if ss.Name != "" {
|
||||
opName = ss.Name
|
||||
}
|
||||
}
|
||||
ctx.Store.AddEventLog(req.OrderNo, req.Operator, "online_register", "上线建档 sn="+req.Sn)
|
||||
|
||||
q := url.Values{}
|
||||
@@ -119,7 +128,7 @@ func onlineHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
respBody, _, err := ctx.Mes.Post("/api/internal/workpiece/online?"+q.Encode(), json.RawMessage(payload))
|
||||
if err != nil {
|
||||
if qerr := ctx.Store.InsertQueueItem("online_register", string(payload)); qerr != nil {
|
||||
if qerr := ctx.Store.InsertQueueItem("online_register", string(payload), opID, opName); qerr != nil {
|
||||
fail(w, http.StatusInternalServerError, "上报失败且离线队列写入失败:"+qerr.Error())
|
||||
return
|
||||
}
|
||||
@@ -155,6 +164,15 @@ func doneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
ctx.Syncer.RunOnce(r.Context())
|
||||
// 录入时操作人(当前登录人),供离线队列按操作人隔离发送
|
||||
opID := int64(0)
|
||||
opName := req.Operator
|
||||
if ss, sErr := ctx.Store.GetStationSession(); sErr == nil && ss != nil {
|
||||
opID = ss.UserID
|
||||
if ss.Name != "" {
|
||||
opName = ss.Name
|
||||
}
|
||||
}
|
||||
ctx.Store.AddEventLog("", req.Operator, "offline_finish", "下线完工入库 sn="+req.Sn)
|
||||
|
||||
q := url.Values{}
|
||||
@@ -163,7 +181,7 @@ func doneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
respBody, _, err := ctx.Mes.Post("/api/internal/workpiece/done?"+q.Encode(), json.RawMessage(payload))
|
||||
if err != nil {
|
||||
if qerr := ctx.Store.InsertQueueItem("offline_finish", string(payload)); qerr != nil {
|
||||
if qerr := ctx.Store.InsertQueueItem("offline_finish", string(payload), opID, opName); qerr != nil {
|
||||
fail(w, http.StatusInternalServerError, "上报失败且离线队列写入失败:"+qerr.Error())
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user