2026-08-28 15:06:01 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// parseJSON 解析请求体 JSON,失败返回错误。
|
|
|
|
|
func parseJSON(r *http.Request, v any) error {
|
|
|
|
|
dec := json.NewDecoder(r.Body)
|
|
|
|
|
return dec.Decode(v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func atoi(s string, def int) int {
|
|
|
|
|
if s == "" {
|
|
|
|
|
return def
|
|
|
|
|
}
|
|
|
|
|
n, err := strconv.Atoi(s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return def
|
|
|
|
|
}
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-31 08:06:55 +08:00
|
|
|
func itoa(n int) string {
|
|
|
|
|
return strconv.Itoa(n)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 16:59:15 +08:00
|
|
|
// currentUsername 取登录中间件注入的用户名(X-Username),用于补齐上报/叫料操作人。
|
|
|
|
|
func currentUsername(r *http.Request) string {
|
|
|
|
|
return r.Header.Get("X-Username")
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
// errString 压缩错误信息长度便于前端展示。
|
|
|
|
|
func errString(err error) string {
|
|
|
|
|
s := err.Error()
|
|
|
|
|
const maxLen = 180
|
|
|
|
|
if len(s) > maxLen {
|
|
|
|
|
return s[:maxLen] + "..."
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|