2026-08-28 15:06:01 +08:00
|
|
|
|
package httpx
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"encoding/json"
|
2026-10-20 16:36:26 +08:00
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"net/http"
|
2026-10-20 16:36:26 +08:00
|
|
|
|
|
|
|
|
|
|
"bj_power_mes/internal/verr"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 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})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-20 16:36:26 +08:00
|
|
|
|
// 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())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
|
// 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)
|
|
|
|
|
|
}
|