Files
bj_power/bj_power_wms/internal/handler/common.go
T

76 lines
1.3 KiB
Go
Raw Normal View History

2026-08-28 15:06:01 +08:00
package handler
import (
"context"
"encoding/json"
"net/http"
"strconv"
"time"
)
func timeNow() time.Time {
return time.Now()
}
func ctx0() context.Context {
return context.Background()
}
// parseJSON 解析请求体 JSON,失败返回错误
func parseJSON(r *http.Request, v any) error {
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
return dec.Decode(v)
}
func strPtr(s string) *string {
if s == "" {
return nil
}
return &s
}
// int64Ptr 0 视为未传(nil),>0 才落库
func int64Ptr(v int64) *int64 {
if v <= 0 {
return nil
}
return &v
}
// normalizePage 列表真分页参数归一:page 从 1 起,pageSize 1~200,默认 20。
// 仅用于「显式带 page/pageSize」的新调用方;旧调用方不带参数时保持原全量结构。
func normalizePage(pageStr, pageSizeStr string) (int, int) {
page := atoi(pageStr, 1)
if page < 1 {
page = 1
}
pageSize := atoi(pageSizeStr, 20)
if pageSize < 1 || pageSize > 200 {
pageSize = 20
}
return page, pageSize
}
2026-08-28 15:06:01 +08:00
func atoi(s string, def int) int {
if s == "" {
return def
}
n, err := strconv.Atoi(s)
if err != nil {
return def
}
return n
}
func atoi64(s string, def int64) int64 {
if s == "" {
return def
}
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return def
}
return n
}