heritage-api/handler/base-handler.go

52 lines
900 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 这是handler层的基础函数不必要求规范的方法名格式
package handler
import (
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"myschools.me/heritage/heritage-api/model"
)
// 获取当前用户
func currentUser(c *gin.Context) *model.User {
usr, ok := c.Get("user")
if !ok {
return nil
}
u, ok := usr.(*model.User)
if !ok || u == nil || u.ID == "" {
return nil
}
return u
}
func pageAndSize(c *gin.Context) (int, int) {
page, _ := strconv.Atoi(c.Query("page"))
size, _ := strconv.Atoi(c.Query("size"))
if page <= 0 {
page = 1
}
if size <= 0 {
size = 20
}
if size > 200 {
size = 200
}
return page, size
}
func parseDateOnly(s *string) (*time.Time, error) {
*s = strings.TrimSpace(*s)
if *s == "" {
return nil, nil
}
t, err := time.ParseInLocation("2006-01-02", *s, time.Local)
if err != nil {
return nil, err
}
return &t, nil
}