71 lines
1.8 KiB
Go
71 lines
1.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,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
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))
|
||
|
|
}
|