271 lines
10 KiB
Go
271 lines
10 KiB
Go
package handler
|
||
|
||
import (
|
||
"encoding/json"
|
||
"io"
|
||
"net/http"
|
||
"net/url"
|
||
"strings"
|
||
|
||
"bj_power_workstation/internal/svc"
|
||
)
|
||
|
||
// ============ 产线点位 / 叫料(代理 MES 内部 API) ============
|
||
// MES 侧对应接口均以 X-API-TOKEN 保护(/api/internal/line/*),本服务用配置 token 转发,
|
||
// 前端只与本服务交互(JWT 登录)。MES 响应体原样透传({code,message,data})。
|
||
|
||
// mesJSON 透传 MES 响应体;网络不可达时返回 502 中文提示。
|
||
func mesJSON(w http.ResponseWriter, body []byte, err error) bool {
|
||
if err != nil {
|
||
// 2026-11-13 修复:MES 侧把 401 折叠成 "station auth expired or invalid",
|
||
// 原实现把鉴权失败一并包装成 502「MES 服务不可达」,误导用户以为网络断了。
|
||
if strings.Contains(errString(err), "auth expired") || strings.Contains(errString(err), "401") {
|
||
fail(w, http.StatusUnauthorized, "工位登录已失效,请重新登录后再操作")
|
||
return false
|
||
}
|
||
failMes(w, err, "MES 服务不可达:"+errString(err))
|
||
return false
|
||
}
|
||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||
w.WriteHeader(http.StatusOK)
|
||
_, _ = w.Write(body)
|
||
return true
|
||
}
|
||
|
||
// linePointsHandler GET /api/line/points(产线点位台账:当前每个点位上的工件)
|
||
func linePointsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
body, _, err := ctx.Mes.Get("/api/internal/line/points", nil, bearerOf(r))
|
||
mesJSON(w, body, err)
|
||
}
|
||
}
|
||
|
||
// linePointSensorHandler GET /api/line/point-sensor?pointNo=
|
||
// 建档前实物核对:转发 MES 读取所选点位 PLC 传感器实时有货状态(只读,不触发下流)。
|
||
// 透传 {pointNo, hasGoods, plcOk, code};plcOk=false 时前端降级为人工确认提示。
|
||
func linePointSensorHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
q := url.Values{}
|
||
if v := strings.TrimSpace(r.URL.Query().Get("pointNo")); v != "" {
|
||
q.Set("pointNo", v)
|
||
}
|
||
body, _, err := ctx.Mes.Get("/api/internal/line/point-sensor", q, bearerOf(r))
|
||
mesJSON(w, body, err)
|
||
}
|
||
}
|
||
|
||
// lineOccupyHandler POST /api/line/occupy(点位人工记账:IN 上料 / OUT 下料)
|
||
// 请求体:{pointNo, sn, orderNo, action} —— operator 由本服务补当前登录人。
|
||
func lineOccupyHandler(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 req map[string]any
|
||
if err := json.Unmarshal(payload, &req); err != nil {
|
||
fail(w, http.StatusBadRequest, "请求体解析失败")
|
||
return
|
||
}
|
||
if req["operator"] == nil || req["operator"] == "" {
|
||
if u := currentUsername(r); u != "" {
|
||
req["operator"] = u
|
||
}
|
||
}
|
||
body, _ := json.Marshal(req)
|
||
resp, _, err := ctx.Mes.Post("/api/internal/line/occupy", json.RawMessage(body), bearerOf(r))
|
||
mesJSON(w, resp, err)
|
||
}
|
||
}
|
||
|
||
// ============ B5 叫料(代理 MES 内部 API,2026-10-13 定稿) ============
|
||
// 接驳台(2026-11-09 拍板):期望接驳台由工人当次可选(可空),终端不再有固定接驳台配置;
|
||
// 留空=配送时库房定实际接驳台。
|
||
|
||
// callPoolHandler GET /api/call/pool 叫料池(本工位物料行四数:需求/调整/已发/占用/可用)
|
||
func callPoolHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
q := url.Values{}
|
||
if v := strings.TrimSpace(r.URL.Query().Get("stationNo")); v != "" {
|
||
q.Set("stationNo", v)
|
||
}
|
||
body, _, err := ctx.Mes.Get("/api/internal/call/pool", q, bearerOf(r))
|
||
mesJSON(w, body, err)
|
||
}
|
||
}
|
||
|
||
// callCreateHandler POST /api/call/create 发起叫料(按物料行拆 N 张单,占用池子可用量)
|
||
// 请求体:{stationNo, items:[{materialCode, qty, dock?}]};dock=期望接驳台(可空,不兜底——
|
||
// 2026-11-09 拍板:删除终端固定接驳台配置,留空=配送时库房定实际接驳台)。
|
||
func callCreateHandler(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 req map[string]any
|
||
if err := json.Unmarshal(payload, &req); err != nil {
|
||
fail(w, http.StatusBadRequest, "请求体解析失败")
|
||
return
|
||
}
|
||
if req["operator"] == nil || req["operator"] == "" {
|
||
if u := currentUsername(r); u != "" {
|
||
req["operator"] = u
|
||
}
|
||
}
|
||
body, _ := json.Marshal(req)
|
||
resp, _, err := ctx.Mes.Post("/api/internal/call/create", json.RawMessage(body), bearerOf(r))
|
||
mesJSON(w, resp, err)
|
||
}
|
||
}
|
||
|
||
// callListHandler GET /api/call/list 叫料单列表(本工位;status 可空=全部)
|
||
func callListHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
q := url.Values{}
|
||
for _, k := range []string{"stationNo", "status", "materialCode", "targetDock", "source", "requestNo"} {
|
||
if v := strings.TrimSpace(r.URL.Query().Get(k)); v != "" {
|
||
q.Set(k, v)
|
||
}
|
||
}
|
||
body, _, err := ctx.Mes.Get("/api/internal/material-requests", q, bearerOf(r))
|
||
mesJSON(w, body, err)
|
||
}
|
||
}
|
||
|
||
// 2026-11-05 叫料包拍板:需求明细(两类提示)/ 左右接驳台实时传感器 / 托盘回库 / 工位物料明细
|
||
|
||
// callPlanDetailHandler GET /api/call/plan-detail?stationNo=
|
||
// 叫料需求明细(工单×排产日)+「当日剩余没叫/跨日量明细」两类提示(实时计算不落库不拦提交)。
|
||
func callPlanDetailHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
q := url.Values{}
|
||
if v := strings.TrimSpace(r.URL.Query().Get("stationNo")); v != "" {
|
||
q.Set("stationNo", v)
|
||
}
|
||
body, _, err := ctx.Mes.Get("/api/internal/call/plan-detail", q, bearerOf(r))
|
||
mesJSON(w, body, err)
|
||
}
|
||
}
|
||
|
||
// stationDockPalletsHandler GET /api/dock/station-pallets?stationNo=
|
||
// 左右接驳台实时传感器状态(叫料空台校验/下班校验共用;PLC 不可达回退台账并标 plcOk=false)。
|
||
func stationDockPalletsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
q := url.Values{}
|
||
if v := strings.TrimSpace(r.URL.Query().Get("stationNo")); v != "" {
|
||
q.Set("stationNo", v)
|
||
}
|
||
body, _, err := ctx.Mes.Get("/api/internal/dock/station-pallets", q, bearerOf(r))
|
||
mesJSON(w, body, err)
|
||
}
|
||
}
|
||
|
||
// palletReturnHandler POST /api/dock/pallet-return 托盘回库(物理拉回仓库,非退料;
|
||
// 物料账与库存不变;传感器为准、台账软校验,不一致由操作人确认留痕)。
|
||
func palletReturnHandler(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 req map[string]any
|
||
if err := json.Unmarshal(payload, &req); err != nil {
|
||
fail(w, http.StatusBadRequest, "请求体解析失败")
|
||
return
|
||
}
|
||
if req["operator"] == nil || req["operator"] == "" {
|
||
if u := currentUsername(r); u != "" {
|
||
req["operator"] = u
|
||
}
|
||
}
|
||
body, _ := json.Marshal(req)
|
||
resp, _, perr := ctx.Mes.Post("/api/internal/dock/pallet-return", json.RawMessage(body), bearerOf(r))
|
||
mesJSON(w, resp, perr)
|
||
}
|
||
}
|
||
|
||
// materialDetailHandler GET /api/call/material-detail?stationNo=
|
||
// 工位物料明细(工艺物料全集 左连接 工位账四数,量为0也显示、不分左右托盘)。
|
||
func materialDetailHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
q := url.Values{}
|
||
if v := strings.TrimSpace(r.URL.Query().Get("stationNo")); v != "" {
|
||
q.Set("stationNo", v)
|
||
}
|
||
body, _, err := ctx.Mes.Get("/api/internal/call/material-detail", q, bearerOf(r))
|
||
mesJSON(w, body, err)
|
||
}
|
||
}
|
||
|
||
// callSignHandler POST /api/call/sign 签收(仅已发货;签收=确认收到,系统不再对差异)
|
||
func callSignHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return callStateHandler(ctx, "/api/internal/call/sign", "签收")
|
||
}
|
||
|
||
// callCancelHandler POST /api/call/cancel 取消(仅待备料,工位本人)
|
||
func callCancelHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return callStateHandler(ctx, "/api/internal/call/cancel", "取消")
|
||
}
|
||
|
||
// callReturnHandler POST /api/call/return 申请退库(仅已发货/签收前;整单退,仓库确认收回后关单)
|
||
func callReturnHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return callStateHandler(ctx, "/api/internal/call/return", "退库申请")
|
||
}
|
||
|
||
// callStateHandler 叫料单状态动作通用代理:补 operator(当前登录人)后转发 MES 内部端点。
|
||
func callStateHandler(ctx *svc.ServiceContext, mesPath, action string) 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 req map[string]any
|
||
if err := json.Unmarshal(payload, &req); err != nil {
|
||
fail(w, http.StatusBadRequest, "请求体解析失败")
|
||
return
|
||
}
|
||
if req["requestNo"] == nil || req["requestNo"] == "" {
|
||
fail(w, http.StatusBadRequest, "缺少叫料单号")
|
||
return
|
||
}
|
||
if req["operator"] == nil || req["operator"] == "" {
|
||
if u := currentUsername(r); u != "" {
|
||
req["operator"] = u
|
||
}
|
||
}
|
||
body, _ := json.Marshal(req)
|
||
resp, _, err := ctx.Mes.Post(mesPath, json.RawMessage(body), bearerOf(r))
|
||
if err != nil {
|
||
failMes(w, err, action+"失败:MES 不可达")
|
||
return
|
||
}
|
||
mesJSON(w, resp, err)
|
||
}
|
||
}
|
||
|
||
// docksHandler GET /api/docks 接驳台列表(代理 MES /api/internal/docks;终端按工位过滤 左L/右R)
|
||
func docksHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
body, _, err := ctx.Mes.Get("/api/internal/docks", url.Values{}, bearerOf(r))
|
||
mesJSON(w, body, err)
|
||
}
|
||
}
|
||
|
||
// reasonDictHandler GET /api/dict/reasons?kind=stop|defect|loss 原因基础字典
|
||
//(2026-11-14 用户拍板:工位终端停机/不良原因由自由文本改下拉,MES 为唯一真源,终端代理)
|
||
func reasonDictHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
q := url.Values{}
|
||
if v := strings.TrimSpace(r.URL.Query().Get("kind")); v != "" {
|
||
q.Set("kind", v)
|
||
}
|
||
body, _, err := ctx.Mes.Get("/api/internal/dict/reasons", q, bearerOf(r))
|
||
mesJSON(w, body, err)
|
||
}
|
||
}
|