feat: 新增装机绑定功能与权限、BOM工序配置
1. 新增装机绑定实体与相关CRUD逻辑,支持工件工序物料绑定/解绑 2. 为BOM物料新增装配工序字段,支持按工序校验绑定 3. 扩展权限表,新增父权限码字段支持菜单按钮关联 4. 统一各页面帮助弹窗参数,新增角色管理帮助文档 5. 新增公共格式化工具函数,优化侧边栏菜单展开逻辑 6. 新增工位终端绑定代理接口与MES内部绑定API 7. 修复主入口数据库连接与schema初始化逻辑,新增一键迁移工具
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"bj_power_workstation/internal/svc"
|
||||
)
|
||||
|
||||
// bindPanelHandler 代理 MES GET /api/internal/workpiece/bind-panel?sn=&processCode=。
|
||||
// 工位终端扫码工件后拉取本工序应装/已装物料清单(装机绑定引导),响应原样透传。
|
||||
func bindPanelHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
sn := strings.TrimSpace(r.URL.Query().Get("sn"))
|
||||
processCode := strings.TrimSpace(r.URL.Query().Get("processCode"))
|
||||
if sn == "" || processCode == "" {
|
||||
fail(w, http.StatusBadRequest, "缺少 sn / processCode 参数")
|
||||
return
|
||||
}
|
||||
body, _, err := ctx.Mes.Get("/api/internal/workpiece/bind-panel",
|
||||
url.Values{"sn": {sn}, "processCode": {processCode}})
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadGateway, "MES 服务不可达:"+errString(err))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(body)
|
||||
}
|
||||
}
|
||||
|
||||
// bindRecordHandler POST /api/bind/record —— 工位扫/录一个物料(SN/批次)→ MES 装机绑定。
|
||||
// 透传原 body,MES 负责校验(料是否属于本产品本工序 / 精密件SN防重 / 幂等)。
|
||||
func bindRecordHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
payload, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "请求体读取失败")
|
||||
return
|
||||
}
|
||||
var probe struct {
|
||||
Sn string `json:"sn"`
|
||||
ProcessCode int `json:"processCode"`
|
||||
}
|
||||
_ = json.Unmarshal(payload, &probe)
|
||||
if probe.Sn == "" || probe.ProcessCode <= 0 {
|
||||
fail(w, http.StatusBadRequest, "sn / processCode 必填")
|
||||
return
|
||||
}
|
||||
body, _, err := ctx.Mes.Post("/api/internal/workpiece/binds", json.RawMessage(payload))
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadGateway, "MES 服务不可达:"+errString(err))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(body)
|
||||
}
|
||||
}
|
||||
|
||||
// bindRemoveHandler POST /api/bind/remove —— 撤销误绑(未报工前)
|
||||
func bindRemoveHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
payload, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "请求体读取失败")
|
||||
return
|
||||
}
|
||||
body, _, err := ctx.Mes.Post("/api/internal/workpiece/binds/remove", json.RawMessage(payload))
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadGateway, "MES 服务不可达:"+errString(err))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(body)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"bj_power_workstation/internal/svc"
|
||||
@@ -9,10 +10,35 @@ import (
|
||||
"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, processCode, stationNo, steps:[{stepId,name,value,text}]}
|
||||
// {sn, orderNo, processCode, 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 不可达则写入 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 {
|
||||
@@ -26,6 +52,10 @@ func processDoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
Value float64 `json:"value"`
|
||||
Text string `json:"text"`
|
||||
} `json:"steps"`
|
||||
Binds []struct {
|
||||
MaterialCode string `json:"materialCode"`
|
||||
BindValue string `json:"bindValue"`
|
||||
} `json:"binds"`
|
||||
Operator string `json:"operator"`
|
||||
}
|
||||
if err := parseJSON(r, &req); err != nil {
|
||||
@@ -48,12 +78,16 @@ func processDoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
"steps": req.Steps,
|
||||
"operator": req.Operator,
|
||||
}
|
||||
if len(req.Binds) > 0 {
|
||||
body["binds"] = req.Binds
|
||||
}
|
||||
payloadBytes, _ := json.Marshal(body)
|
||||
|
||||
ctx.Store.AddEventLog(req.OrderNo, req.Operator, "process_done",
|
||||
"工序完成上报 sn="+req.Sn+" stationNo="+itoa(req.StationNo)+" processCode="+itoa(req.ProcessCode))
|
||||
"工序完成上报 sn="+req.Sn+" stationNo="+itoa(req.StationNo)+" processCode="+itoa(req.ProcessCode)+" binds="+itoa(len(req.Binds)))
|
||||
|
||||
if _, _, err := ctx.Mes.Post("/api/internal/station/report", json.RawMessage(payloadBytes)); err != nil {
|
||||
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 {
|
||||
fail(w, http.StatusInternalServerError, "上报失败且离线队列写入失败:"+qerr.Error())
|
||||
return
|
||||
@@ -62,6 +96,12 @@ func processDoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
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})
|
||||
}
|
||||
@@ -103,7 +143,8 @@ func tempStoreHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
ctx.Store.AddEventLog(req.OrderNo, req.Operator, "temp_store",
|
||||
"暂存/退回库房 sn="+req.Sn+" stationNo="+itoa(req.StationNo))
|
||||
|
||||
if _, _, err := ctx.Mes.Post("/api/internal/station/checkin", json.RawMessage(payloadBytes)); err != nil {
|
||||
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 {
|
||||
fail(w, http.StatusInternalServerError, "上报失败且离线队列写入失败:"+qerr.Error())
|
||||
return
|
||||
@@ -112,6 +153,12 @@ func tempStoreHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
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})
|
||||
}
|
||||
|
||||
@@ -29,6 +29,9 @@ func RegisterHandlers(server *rest.Server, ctx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{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)},
|
||||
{Method: http.MethodPost, Path: "/api/bind/remove", Handler: bindRemoveHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/workload", Handler: workloadHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/pdf", Handler: pdfQueryHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/pdfopen/:name", Handler: pdfOpenHandler(ctx)},
|
||||
|
||||
Reference in New Issue
Block a user