- 新增产品工艺(product_process)及明细(product_process_item)数据模型 - 新增工序审核开关(need_audit)与审核角色(audit_role)字段 - 新增待审核签字页面与路由 - 优化 WMS/MES 接口交互,增加业务错误处理与友好提示 - 补充库存过滤逻辑,避免返回已出库 SN 数据 - 调整默认查询时间范围为近一个月,优化用户体验
147 lines
5.1 KiB
Go
147 lines
5.1 KiB
Go
package mes
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
"net/url"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
)
|
||
|
||
// ErrAuth 认证失效(401 / 登录态过期):可恢复错误。
|
||
// 调用方(离线 syncer)据此跳过该条,数据留在本地 SQLite,待同一操作人重新登录后继续发送。
|
||
var ErrAuth = errors.New("station auth expired or invalid")
|
||
|
||
// 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))
|
||
}
|
||
|
||
// Client MES 内部 API HTTP 客户端。
|
||
// 自动携带请求头:X-API-TOKEN(项目间 token)、Content-Type: application/json。
|
||
// 任何非 2xx 状态返回包含响应体的错误信息。
|
||
type Client struct {
|
||
baseURL string
|
||
token string // X-API-TOKEN 共享内部 token(项目间)
|
||
stationToken string // 工位登录态 JWT(Bearer):方案 B 报工链路可信身份凭证
|
||
httpClient *http.Client
|
||
|
||
mu sync.Mutex
|
||
Touch func() // 每次以「当前有效登录态」发起的调用触发,用于刷新工位空闲计时(核实4)
|
||
}
|
||
|
||
func New(baseURL, token string) *Client {
|
||
return &Client{
|
||
baseURL: strings.TrimRight(baseURL, "/"),
|
||
token: token,
|
||
httpClient: &http.Client{Timeout: 10 * time.Second},
|
||
}
|
||
}
|
||
|
||
// SetStationToken 设置工位登录态 JWT(方案 B):登录成功后由上层注入,
|
||
// 后续报工/拧紧调用将以 Authorization: Bearer 携带,供 MES 校验可信操作人。
|
||
// 重新登录即清除 stale(若有),使离线 syncer 恢复重传。
|
||
func (c *Client) SetStationToken(t string) {
|
||
c.mu.Lock()
|
||
c.stationToken = t
|
||
c.mu.Unlock()
|
||
}
|
||
|
||
// 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)
|
||
}
|
||
|
||
// Post 发送 POST 请求(实时路径),body 序列化为 JSON;Bearer 使用当前登录态 JWT。
|
||
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)
|
||
}
|
||
|
||
|
||
// do 执行请求:Bearer 固定使用当前登录态 JWT(c.stationToken)。
|
||
// 仅在「当前登录态有效」时刷新空闲计时(核实4)。
|
||
func (c *Client) do(method, u string, body io.Reader) ([]byte, http.Header, error) {
|
||
c.mu.Lock()
|
||
bearer := c.stationToken
|
||
c.mu.Unlock()
|
||
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")
|
||
// 方案 B:携带工位登录态 JWT,使 MES 能从 claims 取出可信操作人/工位号
|
||
if bearer != "" {
|
||
req.Header.Set("Authorization", "Bearer "+bearer)
|
||
// 核实4:携带当前登录态的调用视为一次人工活动,刷新空闲计时
|
||
if c.Touch != nil {
|
||
c.Touch()
|
||
}
|
||
}
|
||
|
||
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
|
||
}
|
||
if resp.StatusCode == http.StatusUnauthorized {
|
||
// 401 = 工位登录态过期/无效,属可恢复错误:
|
||
// 调用方(syncer)据此跳过该条,数据留在本地,待同一操作人重新登录后继续发送。
|
||
return respBody, resp.Header, fmt.Errorf("MES %s %s: %w", method, u, ErrAuth)
|
||
}
|
||
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))
|
||
}
|
||
// W2(安全审计):MES 内部接口常以 HTTP 200 + {code≠0} 返回业务失败(如数据校验不通过、
|
||
// 重复提交被拒)。仅看 HTTP 状态会误判成功,导致离线队列数据被标记 synced=1 永久丢失。
|
||
// 此处解析业务码,code≠0 一律视为失败;但与网络失败不同——业务拒绝返回 BizError
|
||
//(调用方直接回前端/死信,不进离线队列无限重传),网络失败返回普通 error(可重试,入离线队列)。
|
||
var br struct {
|
||
Code int `json:"code"`
|
||
Message string `json:"message"`
|
||
}
|
||
if err := json.Unmarshal(respBody, &br); err == nil && br.Code != 0 {
|
||
return respBody, resp.Header, &BizError{Body: respBody, Code: br.Code, Message: br.Message}
|
||
}
|
||
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
|
||
}
|