2026-08-28 15:06:01 +08:00
|
|
|
|
package mes
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"encoding/json"
|
2026-10-06 19:36:22 +08:00
|
|
|
|
"errors"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"io"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"net/url"
|
|
|
|
|
|
"strings"
|
2026-10-06 19:36:22 +08:00
|
|
|
|
"sync"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-10-06 19:36:22 +08:00
|
|
|
|
// ErrAuth 认证失效(401 / 登录态过期):可恢复错误。
|
|
|
|
|
|
// 调用方(离线 syncer)据此跳过该条,数据留在本地 SQLite,待同一操作人重新登录后继续发送。
|
|
|
|
|
|
var ErrAuth = errors.New("station auth expired or invalid")
|
|
|
|
|
|
|
2026-10-09 13:46:33 +08:00
|
|
|
|
// BizError MES 业务拒绝(HTTP 2xx + code≠0):请求已送达且被明确拒绝,数据本身不合法,
|
|
|
|
|
|
// 原样重传不可能成功。调用方应把拒绝原因直接回前端(不写离线队列);
|
|
|
|
|
|
// 离线 syncer 据此把队列条目标记死信(synced=2),停止自动重传,保留记录供人工排查。
|
|
|
|
|
|
type BizError struct {
|
|
|
|
|
|
Body []byte // MES 原始响应体
|
|
|
|
|
|
Code int // 业务码
|
|
|
|
|
|
Message string // 业务消息
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (e *BizError) Error() string {
|
|
|
|
|
|
return fmt.Sprintf("MES 业务拒绝(code=%d): %s", e.Code, truncate(e.Message, 200))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
|
// Client MES 内部 API HTTP 客户端。
|
|
|
|
|
|
// 自动携带请求头:X-API-TOKEN(项目间 token)、Content-Type: application/json。
|
|
|
|
|
|
// 任何非 2xx 状态返回包含响应体的错误信息。
|
|
|
|
|
|
type Client struct {
|
2026-10-06 19:36:22 +08:00
|
|
|
|
baseURL string
|
|
|
|
|
|
token string // X-API-TOKEN 共享内部 token(项目间)
|
|
|
|
|
|
stationToken string // 工位登录态 JWT(Bearer):方案 B 报工链路可信身份凭证
|
|
|
|
|
|
httpClient *http.Client
|
2026-10-20 16:36:26 +08:00
|
|
|
|
streamClient *http.Client // SSE 长连接专用(无总超时)
|
2026-10-06 19:36:22 +08:00
|
|
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
Touch func() // 每次以「当前有效登录态」发起的调用触发,用于刷新工位空闲计时(核实4)
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func New(baseURL, token string) *Client {
|
|
|
|
|
|
return &Client{
|
2026-10-20 16:36:26 +08:00
|
|
|
|
baseURL: strings.TrimRight(baseURL, "/"),
|
|
|
|
|
|
token: token,
|
|
|
|
|
|
httpClient: &http.Client{Timeout: 10 * time.Second},
|
|
|
|
|
|
streamClient: &http.Client{}, // SSE 长连接专用:无总超时,靠服务端心跳+断线重连兜底
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-06 19:36:22 +08:00
|
|
|
|
// SetStationToken 设置工位登录态 JWT(方案 B):登录成功后由上层注入,
|
|
|
|
|
|
// 后续报工/拧紧调用将以 Authorization: Bearer 携带,供 MES 校验可信操作人。
|
|
|
|
|
|
// 重新登录即清除 stale(若有),使离线 syncer 恢复重传。
|
|
|
|
|
|
func (c *Client) SetStationToken(t string) {
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
|
|
c.stationToken = t
|
|
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
|
// Get 发送 GET 请求,query 为空则忽略。
|
|
|
|
|
|
func (c *Client) Get(path string, query url.Values) ([]byte, http.Header, error) {
|
|
|
|
|
|
u := c.baseURL + path
|
|
|
|
|
|
if len(query) > 0 {
|
|
|
|
|
|
u += "?" + query.Encode()
|
|
|
|
|
|
}
|
|
|
|
|
|
return c.do(http.MethodGet, u, nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-06 19:36:22 +08:00
|
|
|
|
// Post 发送 POST 请求(实时路径),body 序列化为 JSON;Bearer 使用当前登录态 JWT。
|
2026-08-28 15:06:01 +08:00
|
|
|
|
func (c *Client) Post(path string, body any) ([]byte, http.Header, error) {
|
|
|
|
|
|
var reader io.Reader
|
|
|
|
|
|
if body != nil {
|
|
|
|
|
|
b, err := json.Marshal(body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, nil, fmt.Errorf("序列化请求体失败: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
reader = bytes.NewReader(b)
|
|
|
|
|
|
}
|
|
|
|
|
|
return c.do(http.MethodPost, c.baseURL+path, reader)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-06 19:36:22 +08:00
|
|
|
|
|
2026-10-20 16:36:26 +08:00
|
|
|
|
// GetStream 发起 SSE 长连接(工位报警事件流,2026-10-20):仅 X-API-TOKEN 鉴权,
|
|
|
|
|
|
// 不携带/不刷新工位登录态(长连接期间登录态可能过期,SSE 鉴权只认项目间 token)。
|
|
|
|
|
|
// 使用独立的无超时 http.Client(默认 httpClient 的 10s 总超时会掐断长连接);
|
|
|
|
|
|
// 调用方负责关闭返回的 ReadCloser,断线重连由调用方(alertstream.Manager)负责。
|
|
|
|
|
|
func (c *Client) GetStream(path string, query url.Values) (io.ReadCloser, error) {
|
|
|
|
|
|
u := c.baseURL + path
|
|
|
|
|
|
if len(query) > 0 {
|
|
|
|
|
|
u += "?" + query.Encode()
|
|
|
|
|
|
}
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, u, nil)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
req.Header.Set("X-API-TOKEN", c.token)
|
|
|
|
|
|
req.Header.Set("Accept", "text/event-stream")
|
|
|
|
|
|
resp, err := c.streamClient.Do(req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
return nil, fmt.Errorf("MES %s 返回 %d:%s", path, resp.StatusCode, truncate(string(body), 200))
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp.Body, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-06 19:36:22 +08:00
|
|
|
|
// do 执行请求:Bearer 固定使用当前登录态 JWT(c.stationToken)。
|
|
|
|
|
|
// 仅在「当前登录态有效」时刷新空闲计时(核实4)。
|
2026-08-28 15:06:01 +08:00
|
|
|
|
func (c *Client) do(method, u string, body io.Reader) ([]byte, http.Header, error) {
|
2026-10-06 19:36:22 +08:00
|
|
|
|
c.mu.Lock()
|
|
|
|
|
|
bearer := c.stationToken
|
|
|
|
|
|
c.mu.Unlock()
|
2026-08-28 15:06:01 +08:00
|
|
|
|
req, err := http.NewRequest(method, u, body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
req.Header.Set("X-API-TOKEN", c.token)
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json;charset=utf-8")
|
2026-10-06 19:36:22 +08:00
|
|
|
|
// 方案 B:携带工位登录态 JWT,使 MES 能从 claims 取出可信操作人/工位号
|
|
|
|
|
|
if bearer != "" {
|
|
|
|
|
|
req.Header.Set("Authorization", "Bearer "+bearer)
|
|
|
|
|
|
// 核实4:携带当前登录态的调用视为一次人工活动,刷新空闲计时
|
|
|
|
|
|
if c.Touch != nil {
|
|
|
|
|
|
c.Touch()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
|
|
|
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, resp.Header, err
|
|
|
|
|
|
}
|
2026-10-06 19:36:22 +08:00
|
|
|
|
if resp.StatusCode == http.StatusUnauthorized {
|
|
|
|
|
|
// 401 = 工位登录态过期/无效,属可恢复错误:
|
|
|
|
|
|
// 调用方(syncer)据此跳过该条,数据留在本地,待同一操作人重新登录后继续发送。
|
|
|
|
|
|
return respBody, resp.Header, fmt.Errorf("MES %s %s: %w", method, u, ErrAuth)
|
|
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
|
|
|
|
return respBody, resp.Header, fmt.Errorf("MES %s %s 返回 %d:%s",
|
|
|
|
|
|
method, u, resp.StatusCode, truncate(string(respBody), 200))
|
|
|
|
|
|
}
|
2026-10-06 19:36:22 +08:00
|
|
|
|
// W2(安全审计):MES 内部接口常以 HTTP 200 + {code≠0} 返回业务失败(如数据校验不通过、
|
|
|
|
|
|
// 重复提交被拒)。仅看 HTTP 状态会误判成功,导致离线队列数据被标记 synced=1 永久丢失。
|
2026-10-09 13:46:33 +08:00
|
|
|
|
// 此处解析业务码,code≠0 一律视为失败;但与网络失败不同——业务拒绝返回 BizError
|
|
|
|
|
|
//(调用方直接回前端/死信,不进离线队列无限重传),网络失败返回普通 error(可重试,入离线队列)。
|
2026-10-06 19:36:22 +08:00
|
|
|
|
var br struct {
|
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := json.Unmarshal(respBody, &br); err == nil && br.Code != 0 {
|
2026-10-09 13:46:33 +08:00
|
|
|
|
return respBody, resp.Header, &BizError{Body: respBody, Code: br.Code, Message: br.Message}
|
2026-10-06 19:36:22 +08:00
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
|
return respBody, resp.Header, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func truncate(s string, n int) string {
|
|
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
|
if len(s) > n {
|
|
|
|
|
|
return s[:n] + "..."
|
|
|
|
|
|
}
|
|
|
|
|
|
return s
|
2026-10-06 19:36:22 +08:00
|
|
|
|
}
|