本次迭代覆盖MES与WMS核心业务: 1. 新增接驳台托盘传感器读取与AGV对接能力 2. 完善工单排产、备料流程与权限体系拆分 3. 优化看板接口与前端路由、样式 4. 新增操作日志、库存盘点与角色保护逻辑 5. 修复代理地址、BOM保存等已知问题
134 lines
3.8 KiB
Go
134 lines
3.8 KiB
Go
package wmsclient
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// Client WMS 内部接口客户端(半成品流转等)
|
|
type Client struct {
|
|
baseURL string
|
|
token string
|
|
hc *http.Client
|
|
}
|
|
|
|
func New(baseURL, token string) *Client {
|
|
if baseURL == "" {
|
|
baseURL = "http://127.0.0.1:9091"
|
|
}
|
|
return &Client{
|
|
baseURL: baseURL,
|
|
token: token,
|
|
hc: &http.Client{Timeout: 5 * time.Second},
|
|
}
|
|
}
|
|
|
|
// SemiInbound 调 WMS /api/internal/semi/inbound 半成品入库
|
|
func (c *Client) SemiInbound(ctx context.Context, sn, orderNo string, doneProcessCodes []int, operator string) error {
|
|
return c.post(ctx, "/api/internal/semi/inbound", map[string]any{
|
|
"sn": sn, "orderNo": orderNo, "doneProcessCodes": doneProcessCodes, "operator": operator,
|
|
})
|
|
}
|
|
|
|
// SemiOutbound 调 WMS /api/internal/semi/outbound 半成品出库(重上线)
|
|
func (c *Client) SemiOutbound(ctx context.Context, sn, orderNo string, doneProcessCodes []int, operator string) error {
|
|
return c.post(ctx, "/api/internal/semi/outbound", map[string]any{
|
|
"sn": sn, "orderNo": orderNo, "doneProcessCodes": doneProcessCodes, "operator": operator,
|
|
})
|
|
}
|
|
|
|
// MaterialExists 批量校验物料编码是否存在于 WMS 物料档案。
|
|
// 返回 WMS 中不存在的编码列表(全部存在则为空)。WMS 响应 {code,message,data:{missing:[]}}。
|
|
// 失败(网络/超时/非0响应)返回 err,由调用方决定降级策略。
|
|
func (c *Client) MaterialExists(ctx context.Context, codes []string) ([]string, error) {
|
|
b, err := json.Marshal(map[string]any{"codes": codes})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/api/internal/material/exists", bytes.NewReader(b))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-API-TOKEN", c.token)
|
|
resp, err := c.hc.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
var body struct {
|
|
Code int `json:"code"`
|
|
Data struct {
|
|
Missing []string `json:"missing"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
|
return nil, err
|
|
}
|
|
if resp.StatusCode >= 300 || body.Code != 0 {
|
|
return nil, &RespError{Status: resp.StatusCode}
|
|
}
|
|
return body.Data.Missing, nil
|
|
}
|
|
|
|
// DisplayOverview 拉取 WMS 免登录看板总览 /api/display/overview。
|
|
// 用于看板「仓储动态」屏。WMS 不可达时返回 err,由调用方降级为空对象(看板其余屏不受影响)。
|
|
func (c *Client) DisplayOverview(ctx context.Context) (map[string]any, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/api/display/overview", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("X-API-TOKEN", c.token)
|
|
resp, err := c.hc.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode >= 300 {
|
|
return nil, &RespError{Status: resp.StatusCode}
|
|
}
|
|
var body struct {
|
|
Code int `json:"code"`
|
|
Data map[string]any `json:"data"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
|
return nil, err
|
|
}
|
|
if body.Data == nil {
|
|
return map[string]any{}, nil
|
|
}
|
|
return body.Data, nil
|
|
}
|
|
|
|
func (c *Client) post(ctx context.Context, path string, body any) error {
|
|
b, err := json.Marshal(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+path, bytes.NewReader(b))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-API-TOKEN", c.token)
|
|
resp, err := c.hc.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode >= 300 {
|
|
return &RespError{Status: resp.StatusCode}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type RespError struct {
|
|
Status int
|
|
}
|
|
|
|
func (e *RespError) Error() string {
|
|
return "wms call failed with status " + string(rune('0'+e.Status))
|
|
} |