2026-09-07 11:58:19 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"bj_power_workstation/internal/svc"
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-22 11:32:29 +08:00
|
|
|
// bindPanelHandler 代理 MES GET /api/internal/workpiece/bind-panel?sn=&flowId=。
|
|
|
|
|
// 工位终端扫码工件后按工位绑定工艺(flowId)拉取应装/已装物料清单(装机绑定引导),响应原样透传。
|
2026-09-07 11:58:19 +08:00
|
|
|
func bindPanelHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
sn := strings.TrimSpace(r.URL.Query().Get("sn"))
|
2026-09-22 11:32:29 +08:00
|
|
|
flowId := strings.TrimSpace(r.URL.Query().Get("flowId"))
|
|
|
|
|
if sn == "" || flowId == "" {
|
|
|
|
|
fail(w, http.StatusBadRequest, "缺少 sn / flowId 参数")
|
2026-09-07 11:58:19 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
body, _, err := ctx.Mes.Get("/api/internal/workpiece/bind-panel",
|
2026-09-22 11:32:29 +08:00
|
|
|
url.Values{"sn": {sn}, "flowId": {flowId}})
|
2026-09-07 11:58:19 +08:00
|
|
|
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 装机绑定。
|
2026-09-22 11:32:29 +08:00
|
|
|
// 透传原 body,MES 负责校验(料是否属于本工艺装配 / 电气件SN防重 / 幂等)。
|
|
|
|
|
// 请求体按定稿契约:{sn, flowId, stationNo, items:[{materialCode, bindValue}]}。
|
2026-09-07 11:58:19 +08:00
|
|
|
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 {
|
2026-09-22 11:32:29 +08:00
|
|
|
Sn string `json:"sn"`
|
|
|
|
|
FlowId int `json:"flowId"`
|
2026-09-07 11:58:19 +08:00
|
|
|
}
|
|
|
|
|
_ = json.Unmarshal(payload, &probe)
|
2026-09-22 11:32:29 +08:00
|
|
|
if probe.Sn == "" || probe.FlowId <= 0 {
|
|
|
|
|
fail(w, http.StatusBadRequest, "sn / flowId 必填")
|
2026-09-07 11:58:19 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|