2026-08-28 15:06:01 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-10-06 19:36:22 +08:00
|
|
|
|
"encoding/json"
|
2026-10-09 13:46:33 +08:00
|
|
|
|
"errors"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"net/http"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2026-10-17 17:49:47 +08:00
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
|
2026-10-06 19:36:22 +08:00
|
|
|
|
"bj_power_workstation/internal/auth"
|
2026-10-09 13:46:33 +08:00
|
|
|
|
"bj_power_workstation/internal/mes"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"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-11-09 18:58:47 +08:00
|
|
|
|
// 2026-11-09 拍板:配置固定工位(>0)时忽略前端传值、强制用配置工位(防串位);
|
|
|
|
|
|
// 未配置(<=0)时接受前端选择的工位(实体+虚拟 0/13 都可选,MES 端对 <=0 免工位权限校验)。
|
|
|
|
|
|
if ctx.Config.Station.StationNo > 0 {
|
|
|
|
|
|
req.StationNo = ctx.Config.Station.StationNo
|
|
|
|
|
|
}
|
2026-08-28 15:06:01 +08:00
|
|
|
|
|
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-10-09 13:46:33 +08:00
|
|
|
|
var bizErr *mes.BizError
|
|
|
|
|
|
if errors.As(err, &bizErr) {
|
|
|
|
|
|
// MES 明确拒绝(密码错误/账号停用/工位不符等):透传拒绝原因,不伪装成服务不可达
|
|
|
|
|
|
fail(w, http.StatusUnauthorized, bizErr.Message)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-10-17 17:49:47 +08:00
|
|
|
|
// MES 不可达(连接拒绝/超时/DNS 失败等):技术细节记日志,面向终端只给业务化提示,不外泄 dial tcp
|
|
|
|
|
|
logx.Errorf("登录代理 MES /api/internal/station/login 不可达:user=%s stationNo=%d err=%v",
|
|
|
|
|
|
strings.TrimSpace(req.Username), req.StationNo, err)
|
|
|
|
|
|
fail(w, http.StatusBadGateway, "MES 服务不可达,请检查网络或联系管理员")
|
|
|
|
|
|
return
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
2026-10-06 19:36:22 +08:00
|
|
|
|
// 方案 B:落盘工位登录态 JWT,作为后续报工/拧紧的可信身份锚点
|
|
|
|
|
|
var loginResp struct {
|
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
|
Data struct {
|
|
|
|
|
|
AccessToken string `json:"accessToken"`
|
|
|
|
|
|
ExpireAt int64 `json:"expireAt"`
|
|
|
|
|
|
StationNo int `json:"stationNo"`
|
|
|
|
|
|
User struct {
|
|
|
|
|
|
Username string `json:"username"`
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
} `json:"user"`
|
|
|
|
|
|
} `json:"data"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := json.Unmarshal(body, &loginResp); err == nil && loginResp.Data.AccessToken != "" {
|
|
|
|
|
|
// 解析 MES JWT 取出用户 ID,随登录态保存:离线数据落库 operator_id(按操作人隔离发送)
|
|
|
|
|
|
userID := int64(0)
|
|
|
|
|
|
if claims, perr := auth.ParseToken(ctx.Config.Auth.AccessSecret, loginResp.Data.AccessToken); perr == nil {
|
|
|
|
|
|
userID = claims.UserID
|
|
|
|
|
|
}
|
|
|
|
|
|
_ = ctx.Store.SaveStationSession(loginResp.Data.AccessToken, loginResp.Data.User.Username,
|
|
|
|
|
|
loginResp.Data.User.Name, loginResp.Data.StationNo, loginResp.Data.ExpireAt, userID)
|
|
|
|
|
|
ctx.Mes.SetStationToken(loginResp.Data.AccessToken)
|
2026-11-09 18:58:47 +08:00
|
|
|
|
// 拧紧枪配置快照补报(2026-11-09):仅 MES 展示记录,不参与数据收集;
|
|
|
|
|
|
// 启动时若 MES 未就绪,登录这一刻补上,失败只记日志不影响登录。
|
|
|
|
|
|
go func(no int) {
|
|
|
|
|
|
if no <= 0 {
|
|
|
|
|
|
no = ctx.Config.Station.StationNo
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := ctx.Mes.ReportTighteningConfig(ctx.Config.Tightening, no); err != nil {
|
|
|
|
|
|
logx.Infof("拧紧枪配置上报 MES 失败(仅展示记录,不影响登录): %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}(loginResp.Data.StationNo)
|
2026-10-06 19:36:22 +08:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// 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)
|
|
|
|
|
|
}
|
2026-10-06 19:36:22 +08:00
|
|
|
|
// 方案 B:退出即清除本地登录态与 Bearer 凭证
|
|
|
|
|
|
_ = ctx.Store.ClearStationSession()
|
|
|
|
|
|
ctx.Mes.SetStationToken("")
|
2026-11-07 15:06:00 +08:00
|
|
|
|
ok(w, map[string]any{"ok": true})
|
2026-10-06 19:36:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// touchHandler POST /api/auth/touch 滑动续签心跳(修订需求):任何人类触屏行为触发,
|
|
|
|
|
|
// 立即刷新本地登录态空闲计时 last_active_at(零成本本地写),防止作业中误登出。
|
|
|
|
|
|
// 由前端根组件全局事件监听(click/touchstart/keydown/scroll 捕获阶段)调用,节流由前端控制。
|
|
|
|
|
|
func touchHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
if err := ctx.Store.TouchStationSession(); err != nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, "刷新登录态失败:"+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
ok(w, nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-06 19:36:22 +08:00
|
|
|
|
// refreshTokenHandler POST /api/auth/refresh-token 滑动续签 Bearer:
|
|
|
|
|
|
// 用当前有效 MES JWT 向 MES 换取新签发的 JWT(延长有效期),避免作业中令牌过期被登出。
|
|
|
|
|
|
// 前置 Interceptor 已校验前端登录态;此处再代理 MES 刷新并回写本地 Bearer。
|
|
|
|
|
|
func refreshTokenHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
ss, err := ctx.Store.GetStationSession()
|
|
|
|
|
|
if err != nil || ss == nil || ss.Token == "" {
|
|
|
|
|
|
fail(w, http.StatusUnauthorized, "未登录或登录态已失效")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
respBody, _, err := ctx.Mes.Post("/api/internal/station/refresh-token", nil)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(w, http.StatusUnauthorized, "MES 续签失败:"+errString(err))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var rr struct {
|
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
|
Data struct {
|
|
|
|
|
|
AccessToken string `json:"accessToken"`
|
|
|
|
|
|
ExpireAt int64 `json:"expireAt"`
|
|
|
|
|
|
} `json:"data"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := json.Unmarshal(respBody, &rr); err == nil && rr.Data.AccessToken != "" {
|
|
|
|
|
|
userID := ss.UserID
|
|
|
|
|
|
if claims, perr := auth.ParseToken(ctx.Config.Auth.AccessSecret, rr.Data.AccessToken); perr == nil {
|
|
|
|
|
|
userID = claims.UserID
|
|
|
|
|
|
}
|
|
|
|
|
|
_ = ctx.Store.SaveStationSession(rr.Data.AccessToken, ss.Username, ss.Name, ss.StationNo, rr.Data.ExpireAt, userID)
|
|
|
|
|
|
ctx.Mes.SetStationToken(rr.Data.AccessToken)
|
|
|
|
|
|
ok(w, map[string]any{"accessToken": rr.Data.AccessToken, "expireAt": rr.Data.ExpireAt})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
fail(w, http.StatusUnauthorized, "MES 续签响应异常")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-09 18:58:47 +08:00
|
|
|
|
// configHandler GET /api/config 返回工位配置(yaml 加载,终端不可修改):
|
|
|
|
|
|
// stationNo>0=固定工位(登录页不允许选择);<=0=未配置(登录页可选工位,实体+虚拟都可选)。
|
|
|
|
|
|
// 2026-11-09 拍板:固定接驳台 dock 配置已删除(叫料期望接驳台工人当次可选,留空=配送时库房定)。
|
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-09-17 12:49:07 +08:00
|
|
|
|
"stationNo": ctx.Config.Station.StationNo,
|
2026-08-28 15:06:01 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2026-10-06 19:36:22 +08:00
|
|
|
|
}
|
2026-11-09 18:58:47 +08:00
|
|
|
|
|
|
|
|
|
|
// stationOptionsHandler GET /api/station-options(免鉴权,登录前调用):
|
|
|
|
|
|
// 代理 MES 启用工位列表(实体 LINE + 虚拟 OFFLINE 0/13),供未配置工位的终端登录页选择。
|
|
|
|
|
|
func stationOptionsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
body, _, err := ctx.Mes.Get("/api/internal/station/options", nil)
|
|
|
|
|
|
mesJSON(w, body, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|