232 lines
5.6 KiB
Go
232 lines
5.6 KiB
Go
// Package client 四服务统一 HTTP 客户端。
|
||||
|
|
// 统一响应为 {code, message, data},HTTP 业务失败可能 200 也可能 4xx/5xx,两者均由 L1 断言判读。
|
|||
|
|
package client
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"bytes"
|
|||
|
|
"context"
|
|||
|
|
"encoding/json"
|
|||
|
|
"fmt"
|
|||
|
|
"io"
|
|||
|
|
"net/http"
|
|||
|
|
"strconv"
|
|||
|
|
"strings"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"bj_power_test/internal/env"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// Response 一次真实调用的完整结果。
|
|||
|
|
type Response struct {
|
|||
|
|
HTTPStatus int `json:"httpStatus"`
|
|||
|
|
Code int `json:"code"`
|
|||
|
|
Message string `json:"message"`
|
|||
|
|
Data json.RawMessage `json:"data"`
|
|||
|
|
Raw string `json:"raw"`
|
|||
|
|
URL string `json:"url"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// DataGet 从 data 里按层级取字符串值,支持数组首元素;用于把上一步产生的真实单号传给下一步。
|
|||
|
|
func (r *Response) DataGet(keys ...string) string {
|
|||
|
|
if r == nil || len(r.Data) == 0 {
|
|||
|
|
return ""
|
|||
|
|
}
|
|||
|
|
var cur any
|
|||
|
|
if err := json.Unmarshal(r.Data, &cur); err != nil {
|
|||
|
|
return ""
|
|||
|
|
}
|
|||
|
|
for _, k := range keys {
|
|||
|
|
switch v := cur.(type) {
|
|||
|
|
case map[string]any:
|
|||
|
|
cur = v[k]
|
|||
|
|
case []any:
|
|||
|
|
if len(v) == 0 {
|
|||
|
|
return ""
|
|||
|
|
}
|
|||
|
|
if m, ok := v[0].(map[string]any); ok {
|
|||
|
|
cur = m[k]
|
|||
|
|
} else {
|
|||
|
|
return ""
|
|||
|
|
}
|
|||
|
|
default:
|
|||
|
|
return ""
|
|||
|
|
}
|
|||
|
|
if cur == nil {
|
|||
|
|
return ""
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
switch v := cur.(type) {
|
|||
|
|
case string:
|
|||
|
|
return v
|
|||
|
|
case float64:
|
|||
|
|
return fmt.Sprintf("%.0f", v)
|
|||
|
|
case bool:
|
|||
|
|
return fmt.Sprintf("%v", v)
|
|||
|
|
default:
|
|||
|
|
return fmt.Sprint(v)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// DataArray 取 data 里的数组字段(如 list/items),返回原始对象切片。
|
|||
|
|
func (r *Response) DataArray(keys ...string) []map[string]any {
|
|||
|
|
if r == nil || len(r.Data) == 0 {
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
var cur any
|
|||
|
|
if err := json.Unmarshal(r.Data, &cur); err != nil {
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
for _, k := range keys {
|
|||
|
|
m, ok := cur.(map[string]any)
|
|||
|
|
if !ok {
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
cur = m[k]
|
|||
|
|
}
|
|||
|
|
arr, ok := cur.([]any)
|
|||
|
|
if !ok {
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
out := make([]map[string]any, 0, len(arr))
|
|||
|
|
for _, item := range arr {
|
|||
|
|
if m, ok := item.(map[string]any); ok {
|
|||
|
|
out = append(out, m)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return out
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Set 持有四端 baseURL 与登录凭证。
|
|||
|
|
type Set struct {
|
|||
|
|
cfg env.Config
|
|||
|
|
tokens env.Tokens
|
|||
|
|
hc *http.Client
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func New(cfg env.Config, tk env.Tokens) *Set {
|
|||
|
|
return &Set{cfg: cfg, tokens: tk, hc: &http.Client{Timeout: cfg.HTTPTimeout}}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *Set) baseURL(svc env.Service) string {
|
|||
|
|
switch svc {
|
|||
|
|
case env.MES:
|
|||
|
|
return s.cfg.MESURL
|
|||
|
|
case env.WMS:
|
|||
|
|
return s.cfg.WMSURL
|
|||
|
|
case env.WS:
|
|||
|
|
return s.cfg.WSURL
|
|||
|
|
case env.Board:
|
|||
|
|
return s.cfg.BoardURL
|
|||
|
|
}
|
|||
|
|
return ""
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *Set) token(svc env.Service) string {
|
|||
|
|
switch svc {
|
|||
|
|
case env.MES:
|
|||
|
|
return s.tokens.MES
|
|||
|
|
case env.WMS:
|
|||
|
|
return s.tokens.WMS
|
|||
|
|
case env.WS:
|
|||
|
|
return s.tokens.WS
|
|||
|
|
}
|
|||
|
|
return ""
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// stationNoFromBody 从请求体取工位号:test 自己持有工位号,不依赖工位终端 yaml 配置。
|
|||
|
|
func stationNoFromBody(body any) int {
|
|||
|
|
m, ok := body.(map[string]any)
|
|||
|
|
if !ok {
|
|||
|
|
return 0
|
|||
|
|
}
|
|||
|
|
switch v := m["stationNo"].(type) {
|
|||
|
|
case int:
|
|||
|
|
return v
|
|||
|
|
case float64:
|
|||
|
|
return int(v)
|
|||
|
|
case string:
|
|||
|
|
n, _ := strconv.Atoi(v)
|
|||
|
|
return n
|
|||
|
|
}
|
|||
|
|
return 0
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Do 发起真实 HTTP 调用。
|
|||
|
|
// MES /api/internal/* 同时受 InternalAuth(X-API-TOKEN) 与 StationSessionAuth(Bearer) 保护,
|
|||
|
|
// 二者互补而非二选一;工位号由 X-Station-No 头显式指定(默认取 body.stationNo)。
|
|||
|
|
func (s *Set) Do(ctx context.Context, svc env.Service, method, path string, body any) (*Response, error) {
|
|||
|
|
return s.DoWithStation(ctx, svc, method, path, body, stationNoFromBody(body))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// DoWithStation 同 Do,但显式指定工位号(工位 3/6/12 可在同一进程内切换)。
|
|||
|
|
func (s *Set) DoWithStation(ctx context.Context, svc env.Service, method, path string, body any, stationNo int) (*Response, error) {
|
|||
|
|
var reader io.Reader
|
|||
|
|
if body != nil {
|
|||
|
|
b, err := json.Marshal(body)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
reader = bytes.NewReader(b)
|
|||
|
|
}
|
|||
|
|
url := s.baseURL(svc) + path
|
|||
|
|
req, err := http.NewRequestWithContext(ctx, method, url, reader)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
if body != nil {
|
|||
|
|
req.Header.Set("Content-Type", "application/json")
|
|||
|
|
}
|
|||
|
|
if strings.HasPrefix(path, "/api/internal/") {
|
|||
|
|
req.Header.Set("X-API-TOKEN", s.cfg.InternalToken)
|
|||
|
|
if svc == env.MES {
|
|||
|
|
if t := s.token(svc); t != "" {
|
|||
|
|
req.Header.Set("Authorization", "Bearer "+t)
|
|||
|
|
}
|
|||
|
|
if stationNo > 0 {
|
|||
|
|
req.Header.Set("X-Station-No", strconv.Itoa(stationNo))
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
} else if t := s.token(svc); t != "" {
|
|||
|
|
req.Header.Set("Authorization", "Bearer "+t)
|
|||
|
|
}
|
|||
|
|
started := time.Now()
|
|||
|
|
resp, err := s.hc.Do(req)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, fmt.Errorf("%s %s:%w(耗时 %s)", method, url, err, time.Since(started).Round(time.Millisecond))
|
|||
|
|
}
|
|||
|
|
defer resp.Body.Close()
|
|||
|
|
raw, _ := io.ReadAll(resp.Body)
|
|||
|
|
r := &Response{HTTPStatus: resp.StatusCode, Raw: string(raw), URL: url}
|
|||
|
|
var parsed struct {
|
|||
|
|
Code int `json:"code"`
|
|||
|
|
Message string `json:"message"`
|
|||
|
|
Data json.RawMessage `json:"data"`
|
|||
|
|
}
|
|||
|
|
if err := json.Unmarshal(raw, &parsed); err == nil {
|
|||
|
|
r.Code, r.Message, r.Data = parsed.Code, parsed.Message, parsed.Data
|
|||
|
|
} else {
|
|||
|
|
r.Code, r.Message = -1, "响应非 JSON"
|
|||
|
|
}
|
|||
|
|
return r, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *Set) Get(ctx context.Context, svc env.Service, path string) (*Response, error) {
|
|||
|
|
return s.Do(ctx, svc, http.MethodGet, path, nil)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *Set) Post(ctx context.Context, svc env.Service, path string, body any) (*Response, error) {
|
|||
|
|
return s.Do(ctx, svc, http.MethodPost, path, body)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Brief 生成人类可读的「实际」摘要,用于报告。
|
|||
|
|
func (r *Response) Brief() string {
|
|||
|
|
if r == nil {
|
|||
|
|
return "无响应"
|
|||
|
|
}
|
|||
|
|
raw := r.Raw
|
|||
|
|
if len(raw) > 400 {
|
|||
|
|
raw = raw[:400] + "…"
|
|||
|
|
}
|
|||
|
|
return fmt.Sprintf("HTTP %d / code=%d / message=%q / body=%s", r.HTTPStatus, r.Code, r.Message, raw)
|
|||
|
|
}
|