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-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-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)
|
2026-10-06 19:36:22 +08:00
|
|
|
|
// 退出前先统计「当前登录人」尚未成功上报的本地数据条数,供前端提醒本人处理
|
|
|
|
|
|
pendingQueue, pendingTorque := int64(0), int64(0)
|
2026-10-17 17:49:47 +08:00
|
|
|
|
deadQueue := int64(0)
|
2026-10-06 19:36:22 +08:00
|
|
|
|
if ss, sErr := ctx.Store.GetStationSession(); sErr == nil && ss != nil {
|
|
|
|
|
|
pendingQueue, _ = ctx.Store.PendingQueueCountForOperator(ss.UserID)
|
|
|
|
|
|
pendingTorque, _ = ctx.Store.PendingTorqueCountForOperator(ss.UserID)
|
2026-10-17 17:49:47 +08:00
|
|
|
|
deadQueue, _ = ctx.Store.DeadQueueCountForOperator(ss.UserID)
|
2026-10-06 19:36:22 +08:00
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
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("")
|
|
|
|
|
|
ok(w, map[string]any{
|
|
|
|
|
|
"pendingQueue": pendingQueue,
|
|
|
|
|
|
"pendingTorque": pendingTorque,
|
|
|
|
|
|
"pendingTotal": pendingQueue + pendingTorque,
|
2026-10-17 17:49:47 +08:00
|
|
|
|
"deadTotal": deadQueue,
|
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-09-30 17:33:11 +08:00
|
|
|
|
// configHandler GET /api/config 返回工位配置:固定工位号 stationNo、固定接驳台 dock
|
|
|
|
|
|
// (均由 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-09-17 12:49:07 +08:00
|
|
|
|
"stationNo": ctx.Config.Station.StationNo,
|
2026-09-30 17:33:11 +08:00
|
|
|
|
"dock": ctx.Config.Station.Dock,
|
2026-08-28 15:06:01 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2026-10-06 19:36:22 +08:00
|
|
|
|
}
|