54 lines
756 B
Go
54 lines
756 B
Go
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
|
|
}
|
|
|
|
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
|
|
}
|