Files
bj_power/bj_power_workstation/internal/handler/report.go
T
SunYF 756d978900 feat: 实现工位终端滑动续签机制,强化安全与身份追溯
- 新增滑动续签功能:人工触屏触发本地心跳 + 30秒节流刷新JWT,避免作业中令牌过期
- 拆分质量检验权限:过程检/完工检使用不同权限码,细化权限粒度
- 新增操作人用户ID字段:报工、拧紧记录落库时携带可信用户ID,解决身份追溯问题
- 移除编译期默认密钥,启动期校验所有必要密钥非空,防止密钥泄露风险
- 修复WMS附件越权遍历问题:使用UUID文件名替代顺序ID访问附件
- 实现登录态过期数据隔离:仅返回当前操作人的待同步数据,避免身份混淆
2026-10-06 19:36:22 +08:00

198 lines
6.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handler
import (
"encoding/json"
"fmt"
"net/http"
"bj_power_workstation/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
// mesRespCode 解析 MES 统一响应 {code,message}。
// MES 业务失败约定为 HTTP 200 + code!=0(httpx.Fail),HTTP 层无法区分,必须显式解析业务码。
// code==0 返回空串;code!=0 返回 message(供调用方直接透传给前端)。
func mesRespCode(respBody []byte) string {
if len(respBody) == 0 {
return ""
}
var mr struct {
Code int `json:"code"`
Message string `json:"message"`
}
if err := json.Unmarshal(respBody, &mr); err != nil {
return ""
}
if mr.Code == 0 {
return ""
}
if mr.Message != "" {
return mr.Message
}
return fmt.Sprintf("MES 拒绝本次操作(业务码 %d)", mr.Code)
}
// processDoneHandler POST /api/report/process-done
// {sn, orderNo, flowId, stationNo, steps:[{stepId,name,value,text}], binds:[{materialCode,bindValue}], operator}
// 流程:先冲刷一轮积压 → 实时报 MES /api/internal/station/report;
// MES 不可达则写入 report_queue(kind=process_done) 并返回 data.queued=true(离线缓存稍后自动重传);
// MES 业务拒绝(HTTP200+code!=0,如装配物料未绑齐/电气件SN重复)直接失败回前端,绝不入离线队。
// binds 为本次工艺的装机绑定(结构件批次/电气件SN),MES 端落库 + 齐套强校验;离线重传时原样携带。
func processDoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Sn string `json:"sn"`
OrderNo string `json:"orderNo"`
FlowId int `json:"flowId"`
StationNo int `json:"stationNo"`
Steps []struct {
StepId int `json:"stepId"`
Name string `json:"name"`
Value float64 `json:"value"`
Text string `json:"text"`
// Checked:needCheck 步骤的「检测确认」结果,原样转发给 MES 做强校验(勿删,否则服务端拿不到确认)
Checked bool `json:"checked"`
} `json:"steps"`
Binds []struct {
MaterialCode string `json:"materialCode"`
BindValue string `json:"bindValue"`
} `json:"binds"`
Operator string `json:"operator"`
// 作业时长采集(P0-3):上料时刻/报工时刻(ISO8601),原样转发 MES 算 durationSec;离线重传时同 payload 保留
StartedAt string `json:"startedAt"`
EndedAt string `json:"endedAt"`
}
if err := parseJSON(r, &req); err != nil {
fail(w, http.StatusBadRequest, "参数错误")
return
}
if req.Sn == "" || req.FlowId <= 0 || req.StationNo <= 0 {
fail(w, http.StatusBadRequest, "sn/flowId/stationNo 必填")
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())
body := map[string]any{
"sn": req.Sn,
"orderNo": req.OrderNo,
"flowId": req.FlowId,
"stationNo": req.StationNo,
"operator": op,
}
if len(req.Binds) > 0 {
body["binds"] = req.Binds
}
if req.StartedAt != "" {
body["startedAt"] = req.StartedAt
}
if req.EndedAt != "" {
body["endedAt"] = req.EndedAt
}
payloadBytes, _ := json.Marshal(body)
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), opID, op); qerr != nil {
fail(w, http.StatusInternalServerError, "上报失败且离线队列写入失败:"+qerr.Error())
return
}
logx.Errorf("工序完成上报失败已离线排队: %v", err)
ok(w, map[string]any{"queued": true})
return
}
// MES 业务拒绝(HTTP200+code!=0):缺料/SN冲突等,直接失败,不入队
if msg := mesRespCode(respBody); msg != "" {
logx.Errorf("工序完成上报被 MES 拒绝: %s", msg)
fail(w, http.StatusConflict, msg)
return
}
ok(w, map[string]any{"queued": false})
}
}
// tempStoreHandler POST /api/report/temp-store
// {sn, orderNo, stationNo, flowId, operator} → 调 MES /api/internal/station/checkin(半成品入库)。
// 不可达时以同样 payload 写入 report_queue(kind=temp_store)。
func tempStoreHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Sn string `json:"sn"`
OrderNo string `json:"orderNo"`
StationNo int `json:"stationNo"`
FlowId int `json:"flowId"`
Operator string `json:"operator"`
}
if err := parseJSON(r, &req); err != nil {
fail(w, http.StatusBadRequest, "参数错误")
return
}
if req.Sn == "" {
fail(w, http.StatusBadRequest, "缺少 sn")
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())
body := map[string]any{
"sn": req.Sn,
"orderNo": req.OrderNo,
"stationNo": req.StationNo,
"flowId": req.FlowId,
"operator": op,
}
payloadBytes, _ := json.Marshal(body)
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), opID, op); qerr != nil {
fail(w, http.StatusInternalServerError, "上报失败且离线队列写入失败:"+qerr.Error())
return
}
logx.Errorf("暂存退库上报失败已离线排队: %v", err)
ok(w, map[string]any{"queued": true})
return
}
// MES 业务拒绝(HTTP200+code!=0)→ 直接失败,不入队
if msg := mesRespCode(respBody); msg != "" {
logx.Errorf("暂存退库被 MES 拒绝: %s", msg)
fail(w, http.StatusConflict, msg)
return
}
ok(w, map[string]any{"queued": false})
}
}