2026-08-28 15:06:01 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"bj_power_workstation/internal/svc"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func healthHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
if err := ctx.Store.Ping(); err == nil {
|
|
|
|
|
|
ok(w, map[string]any{
|
|
|
|
|
|
"status": "UP",
|
|
|
|
|
|
"time": time.Now().Format("2006-01-02 15:04:05"),
|
|
|
|
|
|
"service": "bj_power_workstation",
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
fail(w, http.StatusServiceUnavailable, "本地数据库不可用")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-31 08:06:55 +08:00
|
|
|
|
// loginHandler 登录代理 MES /api/internal/station/login(块1:用户体系来源于 MES)。
|
|
|
|
|
|
// 前端提交 {username, password, stationNo},MES 校验工位终端密码 + 工位权限并签发同密钥 JWT。
|
|
|
|
|
|
// 响应体原样透传({code,message,data:{accessToken,expireAt,user,stations,stationNo}})。
|
2026-08-28 15:06:01 +08:00
|
|
|
|
func loginHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req struct {
|
2026-08-31 08:06:55 +08:00
|
|
|
|
Username string `json:"username"`
|
|
|
|
|
|
Password string `json:"password"`
|
|
|
|
|
|
StationNo int `json:"stationNo"`
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
if err := parseJSON(r, &req); err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "参数错误")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if strings.TrimSpace(req.Username) == "" || req.Password == "" {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "用户名和密码必填")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-31 08:06:55 +08:00
|
|
|
|
body, _, err := ctx.Mes.Post("/api/internal/station/login", map[string]any{
|
|
|
|
|
|
"username": strings.TrimSpace(req.Username),
|
|
|
|
|
|
"password": req.Password,
|
|
|
|
|
|
"stationNo": req.StationNo,
|
|
|
|
|
|
})
|
2026-08-28 15:06:01 +08:00
|
|
|
|
if err != nil {
|
2026-08-31 08:06:55 +08:00
|
|
|
|
fail(w, http.StatusBadGateway, "MES 服务不可达:"+errString(err))
|
2026-08-28 15:06:01 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
|
_, _ = w.Write(body)
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// configHandler GET /api/config 返回工位配置:
|
2026-08-31 12:58:28 +08:00
|
|
|
|
// 固定工位号 stationNo(1~12,由 yaml 配置在启动时加载,终端不可修改)、本工位需拧紧的螺丝颗数。
|
2026-08-28 15:06:01 +08:00
|
|
|
|
func configHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
ok(w, map[string]any{
|
2026-08-31 12:58:28 +08:00
|
|
|
|
"stationNo": ctx.Config.Station.StationNo,
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"screwCount": ctx.Config.Station.ScrewCount,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|