新增 ErrorListDialog 组件用于统一展示结构化校验错误信息,实现报错一次收全;新增工位报警语音播报功能,通过浏览器 SpeechSynthesis 播放本工位相关报警;调整物料类别及序列号字段文案,移除「其他」类别;新增工单实际开始字段及采集方式配置;优化 AGV 及 PlcSend 页签刷新逻辑;新增看板物料追溯及工位报警接口;删除 PLC 强制写 M 位及相关测试代码;同步数据库字段注释及补丁。
82 lines
2.4 KiB
Go
82 lines
2.4 KiB
Go
package httpx
|
||
|
||
import (
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"net/http"
|
||
|
||
"bj_power_mes/internal/verr"
|
||
)
|
||
|
||
// Resp 统一返回结构
|
||
type Resp struct {
|
||
Code int `json:"code"`
|
||
Message string `json:"message"`
|
||
Data any `json:"data,omitempty"`
|
||
}
|
||
|
||
func writeJSON(w http.ResponseWriter, status int, body any) {
|
||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||
w.WriteHeader(status)
|
||
_ = json.NewEncoder(w).Encode(body)
|
||
}
|
||
|
||
// Ok 成功
|
||
func Ok(w http.ResponseWriter, data any) {
|
||
writeJSON(w, http.StatusOK, Resp{Code: 0, Message: "success", Data: data})
|
||
}
|
||
|
||
// OkCode 成功但自定义 message
|
||
func OkMessage(w http.ResponseWriter, message string, data any) {
|
||
writeJSON(w, http.StatusOK, Resp{Code: 0, Message: message, Data: data})
|
||
}
|
||
|
||
// Fail 业务失败(HTTP 200,业务码非0)
|
||
func Fail(w http.ResponseWriter, code int, message string) {
|
||
writeJSON(w, http.StatusOK, Resp{Code: code, Message: message})
|
||
}
|
||
|
||
// FailHTTP 直接返回 HTTP 错误
|
||
func FailHTTP(w http.ResponseWriter, status int, message string) {
|
||
writeJSON(w, status, Resp{Code: status, Message: message})
|
||
}
|
||
|
||
// FailValidation 收集式校验失败(HTTP 200,code=3015,data.errors 结构化清单;
|
||
// 前端拦截器识别 3015 后用 ErrorListDialog 一次弹全,不用 ElMessage 单条)。
|
||
func FailValidation(w http.ResponseWriter, errs []verr.FieldError) {
|
||
writeJSON(w, http.StatusOK, Resp{
|
||
Code: 3015,
|
||
Message: fmt.Sprintf("保存失败,共 %d 项需要处理", len(errs)),
|
||
Data: map[string]any{"errors": errs},
|
||
})
|
||
}
|
||
|
||
// FailErr 统一错误出口:*verr.ValidationError → FailValidation 结构化;其他 error → 普通 Fail(3010)。
|
||
func FailErr(w http.ResponseWriter, err error) {
|
||
var ve *verr.ValidationError
|
||
if errors.As(err, &ve) {
|
||
FailValidation(w, ve.Errors)
|
||
return
|
||
}
|
||
Fail(w, 3010, err.Error())
|
||
}
|
||
|
||
// BadRequest 参数错误
|
||
func BadRequest(w http.ResponseWriter, message string) {
|
||
writeJSON(w, http.StatusBadRequest, Resp{Code: 400, Message: message})
|
||
}
|
||
|
||
func InternalError(w http.ResponseWriter, message string) {
|
||
writeJSON(w, http.StatusInternalServerError, Resp{Code: 500, Message: message})
|
||
}
|
||
|
||
func Unauthorized(w http.ResponseWriter, message string) {
|
||
writeJSON(w, http.StatusUnauthorized, Resp{Code: 401, Message: message})
|
||
}
|
||
|
||
// ParseJSON 解析请求体
|
||
func ParseJSON(r *http.Request, v any) error {
|
||
dec := json.NewDecoder(r.Body)
|
||
return dec.Decode(v)
|
||
} |