feat&refactor: 完成多模块功能迭代与配置优化
本次提交覆盖多个业务模块的功能完善与体验优化:
1. **鉴权与配置调整**:
- 统一JWT滑动续签逻辑,简化Token存储,移除RefreshToken相关冗余代码
- 调整多项目配置文件中JWT过期时间为3600秒,统一会话闲置窗口
- 工位配置放开1~12限制,改为仅校验大于0
2. **术语统一替换**:全链路将"精密件"替换为"电气件",修正物料管理描述
3. **功能新增**:
- 新增工位类型、工艺路线与产线点位台账模块
- 添加工艺PDF预览面板、工位终端代理转发接口
- 新增操作日志按操作人列表筛选、工位登出日志记录
- 新增PLC移料指令与产线点位状态管理
4. **业务流程优化**:
- 调整BOM物料删除校验逻辑,优化工单备料计算
- 补充物料图号、检测单号等追溯字段
- 完善工艺流程图与工位绑定关系说明
- 优化前端页面文案与交互细节
5. **代码规范与维护**:
- 新增通用工具函数与前端静态资源
- 整理路由权限与中间件逻辑
- 修复部分接口与配置的不兼容问题
This commit is contained in:
@@ -31,10 +31,10 @@ type SqliteConfig struct {
|
||||
Path string `json:",default=workstation.db"`
|
||||
}
|
||||
|
||||
// StationConfig 工位配置:启动时由 yaml 加载到内存,固定本终端工位号(1~12)。
|
||||
// StationConfig 工位配置:启动时由 yaml 加载到内存,固定本终端工位号。
|
||||
// 一个工位一个终端,终端不允许修改工位;变更工位只能改配置并重启服务。
|
||||
type StationConfig struct {
|
||||
StationNo int `json:",default=1"` // 固定工位号(1~12),启动时校验范围
|
||||
StationNo int `json:",default=1"` // 固定工位号,启动时校验范围
|
||||
ScrewCount int `json:",default=10"` // 本工位需拧紧的螺丝颗数(进度 9/10 用)
|
||||
}
|
||||
|
||||
|
||||
@@ -56,8 +56,30 @@ func loginHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// configHandler GET /api/config 返回工位配置:
|
||||
// 固定工位号 stationNo(1~12,由 yaml 配置在启动时加载,终端不可修改)、本工位需拧紧的螺丝颗数。
|
||||
// logoutHandler POST /api/station/logout 退出登录:转发 MES 记录"工位终端退出"操作日志。
|
||||
// MES 不可达时静默忽略(日志不阻塞本地退出)。
|
||||
func logoutHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Username string `json:"username"`
|
||||
StationNo int `json:"stationNo"`
|
||||
}
|
||||
_ = parseJSON(r, &req)
|
||||
username := strings.TrimSpace(req.Username)
|
||||
if username != "" {
|
||||
no := req.StationNo
|
||||
if no <= 0 {
|
||||
no = ctx.Config.Station.StationNo
|
||||
}
|
||||
go func(u string, n int) {
|
||||
_, _, _ = ctx.Mes.Post("/api/internal/station/logout", map[string]any{"username": u, "stationNo": n})
|
||||
}(username, no)
|
||||
}
|
||||
ok(w, nil)
|
||||
}
|
||||
}
|
||||
|
||||
// configHandler GET /api/config 返回工位配置:// 固定工位号 stationNo(由 yaml 配置在启动时加载,终端不可修改)、本工位需拧紧的螺丝颗数。
|
||||
func configHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ok(w, map[string]any{
|
||||
|
||||
@@ -33,7 +33,7 @@ func bindPanelHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
|
||||
// bindRecordHandler POST /api/bind/record —— 工位扫/录一个物料(SN/批次)→ MES 装机绑定。
|
||||
// 透传原 body,MES 负责校验(料是否属于本产品本工序 / 精密件SN防重 / 幂等)。
|
||||
// 透传原 body,MES 负责校验(料是否属于本产品本工序 / 电气件SN防重 / 幂等)。
|
||||
func bindRecordHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
payload, err := io.ReadAll(r.Body)
|
||||
|
||||
@@ -27,6 +27,11 @@ func itoa(n int) string {
|
||||
return strconv.Itoa(n)
|
||||
}
|
||||
|
||||
// currentUsername 取登录中间件注入的用户名(X-Username),用于补齐上报/叫料操作人。
|
||||
func currentUsername(r *http.Request) string {
|
||||
return r.Header.Get("X-Username")
|
||||
}
|
||||
|
||||
// errString 压缩错误信息长度便于前端展示。
|
||||
func errString(err error) string {
|
||||
s := err.Error()
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"bj_power_workstation/internal/svc"
|
||||
)
|
||||
|
||||
// ============ 产线点位 / 上位机移料 / 叫料(代理 MES 内部 API) ============
|
||||
// MES 侧对应接口均以 X-API-TOKEN 保护(/api/internal/line/*),本服务用配置 token 转发,
|
||||
// 前端只与本服务交互(JWT 登录)。MES 响应体原样透传({code,message,data})。
|
||||
|
||||
// mesJSON 透传 MES 响应体;网络不可达时返回 502 中文提示。
|
||||
func mesJSON(w http.ResponseWriter, body []byte, err error) bool {
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadGateway, "MES 服务不可达:"+errString(err))
|
||||
return false
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(body)
|
||||
return true
|
||||
}
|
||||
|
||||
// linePointsHandler GET /api/line/points(产线点位台账:当前每个点位上的工件)
|
||||
func linePointsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _, err := ctx.Mes.Get("/api/internal/line/points", nil)
|
||||
mesJSON(w, body, err)
|
||||
}
|
||||
}
|
||||
|
||||
// lineMovesHandler GET /api/line/moves?status=&sn=&limit=(移料指令列表)
|
||||
func lineMovesHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
q := url.Values{}
|
||||
for _, k := range []string{"status", "sn", "limit"} {
|
||||
if v := strings.TrimSpace(r.URL.Query().Get(k)); v != "" {
|
||||
q.Set(k, v)
|
||||
}
|
||||
}
|
||||
body, _, err := ctx.Mes.Get("/api/internal/line/moves", q)
|
||||
mesJSON(w, body, err)
|
||||
}
|
||||
}
|
||||
|
||||
// lineMoveHandler POST /api/line/move(下发移料指令:起点位 → 目标点位)
|
||||
// 请求体原样转发:{sn, fromPoint, toPoint, orderNo}
|
||||
func lineMoveHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
payload, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "请求体读取失败")
|
||||
return
|
||||
}
|
||||
var probe struct {
|
||||
Sn string `json:"sn"`
|
||||
FromPoint string `json:"fromPoint"`
|
||||
ToPoint string `json:"toPoint"`
|
||||
}
|
||||
_ = json.Unmarshal(payload, &probe)
|
||||
if probe.Sn == "" || probe.FromPoint == "" || probe.ToPoint == "" {
|
||||
fail(w, http.StatusBadRequest, "工件SN、起点位、目标点位必填")
|
||||
return
|
||||
}
|
||||
body, _, err := ctx.Mes.Post("/api/internal/line/move", json.RawMessage(payload))
|
||||
mesJSON(w, body, err)
|
||||
}
|
||||
}
|
||||
|
||||
// lineArriveHandler POST /api/line/move/arrive(工件到位:回写台账并闭环指令)
|
||||
func lineArriveHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
payload, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "请求体读取失败")
|
||||
return
|
||||
}
|
||||
var probe struct {
|
||||
CmdNo string `json:"cmdNo"`
|
||||
}
|
||||
_ = json.Unmarshal(payload, &probe)
|
||||
if probe.CmdNo == "" {
|
||||
fail(w, http.StatusBadRequest, "缺少指令号 cmdNo")
|
||||
return
|
||||
}
|
||||
body, _, err := ctx.Mes.Post("/api/internal/line/move/arrive", json.RawMessage(payload))
|
||||
mesJSON(w, body, err)
|
||||
}
|
||||
}
|
||||
|
||||
// lineAckHandler POST /api/line/move/ack(PLC 应答:通常由 PLC 侧调用,终端手动应答也走这里)
|
||||
func lineAckHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
payload, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "请求体读取失败")
|
||||
return
|
||||
}
|
||||
body, _, err := ctx.Mes.Post("/api/internal/line/move/ack", json.RawMessage(payload))
|
||||
mesJSON(w, body, err)
|
||||
}
|
||||
}
|
||||
|
||||
// lineOccupyHandler POST /api/line/occupy(点位人工记账:IN 上料 / OUT 下料)
|
||||
// 请求体:{pointNo, sn, orderNo, action} —— operator 由本服务补当前登录人。
|
||||
func lineOccupyHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
payload, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "请求体读取失败")
|
||||
return
|
||||
}
|
||||
var req map[string]any
|
||||
if err := json.Unmarshal(payload, &req); err != nil {
|
||||
fail(w, http.StatusBadRequest, "请求体解析失败")
|
||||
return
|
||||
}
|
||||
if req["operator"] == nil || req["operator"] == "" {
|
||||
if u := currentUsername(r); u != "" {
|
||||
req["operator"] = u
|
||||
}
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
resp, _, err := ctx.Mes.Post("/api/internal/line/occupy", json.RawMessage(body))
|
||||
mesJSON(w, resp, err)
|
||||
}
|
||||
}
|
||||
|
||||
// lineCallMaterialHandler POST /api/line/call-material(工位终端叫料:选本工位接驳台)
|
||||
// 请求体原样转发:{stationNo, dock, orderNo, items:[{materialCode, materialName, unit, manageMode, qty}]}
|
||||
func lineCallMaterialHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
payload, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "请求体读取失败")
|
||||
return
|
||||
}
|
||||
var req map[string]any
|
||||
if err := json.Unmarshal(payload, &req); err != nil {
|
||||
fail(w, http.StatusBadRequest, "请求体解析失败")
|
||||
return
|
||||
}
|
||||
if req["operator"] == nil || req["operator"] == "" {
|
||||
if u := currentUsername(r); u != "" {
|
||||
req["operator"] = u
|
||||
}
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
resp, _, err := ctx.Mes.Post("/api/internal/line/call-material", json.RawMessage(body))
|
||||
mesJSON(w, resp, err)
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,20 @@ func workloadHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// stationsHandler 代理 MES GET /stations(工位数以数据库为准,不写死 12),原样透传响应体。
|
||||
func stationsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _, err := ctx.Mes.Get("/stations", nil)
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadGateway, "MES 服务不可达:"+errString(err))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(body)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 工艺 PDF 直通代理(含本地预缓存) ----------
|
||||
//
|
||||
// 前端 iframe/window.open 无法携带 Authorization,故提供免 JWT 前缀 /pdfopen/<name>,
|
||||
|
||||
@@ -37,8 +37,8 @@ func mesRespCode(respBody []byte) string {
|
||||
// {sn, orderNo, processCode, stationNo, steps:[{stepId,name,value,text}], binds:[{materialCode,bindValue}], operator}
|
||||
// 流程:先冲刷一轮积压 → 实时报 MES /api/internal/station/report;
|
||||
// MES 不可达则写入 report_queue(kind=process_done) 并返回 data.queued=true(离线缓存稍后自动重传);
|
||||
// MES 业务拒绝(HTTP200+code!=0,如装配物料未绑齐/精密件SN重复)直接失败回前端,绝不入离线队。
|
||||
// binds 为本次工序的装机绑定(结构件批次/精密件SN),MES 端落库 + 齐套强校验;离线重传时原样携带。
|
||||
// MES 业务拒绝(HTTP200+code!=0,如装配物料未绑齐/电气件SN重复)直接失败回前端,绝不入离线队。
|
||||
// binds 为本次工序的装机绑定(结构件批次/电气件SN),MES 端落库 + 齐套强校验;离线重传时原样携带。
|
||||
func processDoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
|
||||
@@ -33,13 +33,24 @@ func RegisterHandlers(server *rest.Server, ctx *svc.ServiceContext) {
|
||||
{Method: http.MethodPost, Path: "/api/bind/record", Handler: bindRecordHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/bind/remove", Handler: bindRemoveHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/workload", Handler: workloadHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/stations", Handler: stationsHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/pdf", Handler: pdfQueryHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/pdfopen/:name", Handler: pdfOpenHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/tightening/list", Handler: tighteningListHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/report/process-done", Handler: processDoneHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/report/temp-store", Handler: tempStoreHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/station/logout", Handler: logoutHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/sync/flush", Handler: syncFlushHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/sync/stats", Handler: syncStatsHandler(ctx)},
|
||||
|
||||
// 产线点位 / 上位机移料 / 叫料(代理 MES 内部 API)
|
||||
{Method: http.MethodGet, Path: "/api/line/points", Handler: linePointsHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/line/moves", Handler: lineMovesHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/line/move", Handler: lineMoveHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/line/move/arrive", Handler: lineArriveHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/line/move/ack", Handler: lineAckHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/line/occupy", Handler: lineOccupyHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/line/call-material", Handler: lineCallMaterialHandler(ctx)},
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -28,7 +28,7 @@ type MockSource struct {
|
||||
dockCode string // 模拟枪挂载的工位
|
||||
}
|
||||
|
||||
// NewMockSource 模拟枪挂载在固定工位(stationNo 1~12),DOCK 编码为 DOCK01~DOCK12。
|
||||
// NewMockSource 模拟枪挂载在固定工位(stationNo 由 yaml 配置),DOCK 编码为 DOCK01~DOCK12。
|
||||
func NewMockSource(db *store.Store, mc *mes.Client, stationNo int) *MockSource {
|
||||
dockCode := fmt.Sprintf("DOCK%02d", stationNo)
|
||||
return &MockSource{db: db, mes: mc, dockCode: dockCode}
|
||||
|
||||
Reference in New Issue
Block a user