init
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PaginationParams 分页参数
|
||||
type PaginationParams struct {
|
||||
Page int `form:"page" json:"page"` // 页码,从1开始
|
||||
PageSize int `form:"page_size" json:"page_size"` // 每页数量
|
||||
Search string `form:"search" json:"search"` // 搜索关键词
|
||||
}
|
||||
|
||||
// WelcomeQueryParams 欢迎语查询参数
|
||||
type WelcomeQueryParams struct {
|
||||
PaginationParams
|
||||
IsActive *bool `form:"is_active" json:"is_active"` // 启用状态筛选
|
||||
MessageType string `form:"message_type" json:"message_type"` // 消息类型筛选
|
||||
}
|
||||
|
||||
// CommonQueryParams 通用查询参数(关键词/快捷回复/群发)
|
||||
type CommonQueryParams struct {
|
||||
PaginationParams
|
||||
IsActive *bool `form:"is_active" json:"is_active"` // 启用状态筛选
|
||||
MessageType string `form:"message_type" json:"message_type"` // 消息类型筛选
|
||||
}
|
||||
|
||||
// PaginationResponse 分页响应
|
||||
type PaginationResponse struct {
|
||||
List interface{} `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
Pages int `json:"pages"`
|
||||
}
|
||||
|
||||
// GetPaginationParams 从请求中获取分页参数
|
||||
func GetPaginationParams(ctx *gin.Context) PaginationParams {
|
||||
params := PaginationParams{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
Search: "",
|
||||
}
|
||||
|
||||
if pageStr := ctx.Query("page"); pageStr != "" {
|
||||
if page, err := strconv.Atoi(pageStr); err == nil && page > 0 {
|
||||
params.Page = page
|
||||
}
|
||||
}
|
||||
|
||||
if pageSizeStr := ctx.Query("page_size"); pageSizeStr != "" {
|
||||
if pageSize, err := strconv.Atoi(pageSizeStr); err == nil && pageSize > 0 && pageSize <= 100 {
|
||||
params.PageSize = pageSize
|
||||
}
|
||||
}
|
||||
|
||||
params.Search = ctx.Query("search")
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
// GetWelcomeQueryParams 从请求中获取欢迎语查询参数
|
||||
func GetWelcomeQueryParams(ctx *gin.Context) WelcomeQueryParams {
|
||||
params := WelcomeQueryParams{
|
||||
PaginationParams: GetPaginationParams(ctx),
|
||||
MessageType: ctx.Query("message_type"),
|
||||
}
|
||||
|
||||
if isActiveStr := ctx.Query("is_active"); isActiveStr != "" {
|
||||
if isActive, err := strconv.ParseBool(isActiveStr); err == nil {
|
||||
params.IsActive = &isActive
|
||||
}
|
||||
}
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
// GetCommonQueryParams 从请求中获取通用查询参数
|
||||
func GetCommonQueryParams(ctx *gin.Context) CommonQueryParams {
|
||||
params := CommonQueryParams{
|
||||
PaginationParams: GetPaginationParams(ctx),
|
||||
MessageType: ctx.Query("message_type"),
|
||||
}
|
||||
|
||||
if isActiveStr := ctx.Query("is_active"); isActiveStr != "" {
|
||||
if isActive, err := strconv.ParseBool(isActiveStr); err == nil {
|
||||
params.IsActive = &isActive
|
||||
}
|
||||
}
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
// ApplyPagination 应用分页到查询
|
||||
func ApplyPagination(db *gorm.DB, params PaginationParams) *gorm.DB {
|
||||
offset := (params.Page - 1) * params.PageSize
|
||||
return db.Offset(offset).Limit(params.PageSize)
|
||||
}
|
||||
|
||||
// CreatePaginationResponse 创建分页响应
|
||||
func CreatePaginationResponse(list interface{}, total int64, params PaginationParams) PaginationResponse {
|
||||
pages := int((total + int64(params.PageSize) - 1) / int64(params.PageSize))
|
||||
if pages == 0 {
|
||||
pages = 1
|
||||
}
|
||||
|
||||
return PaginationResponse{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: params.Page,
|
||||
PageSize: params.PageSize,
|
||||
Pages: pages,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package utils
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
// APIResponse 通用响应结构
|
||||
type APIResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
// Success 返回成功响应
|
||||
func Success(data interface{}) gin.H {
|
||||
return gin.H{
|
||||
"code": 200,
|
||||
"message": "success",
|
||||
"data": data,
|
||||
}
|
||||
}
|
||||
|
||||
// Failure 返回失败响应
|
||||
func Failure(code int, message string) gin.H {
|
||||
return gin.H{
|
||||
"code": code,
|
||||
"message": message,
|
||||
"data": nil,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var localTimezone *time.Location
|
||||
|
||||
// InitTimezone 初始化时区设置
|
||||
func InitTimezone(timezone string) error {
|
||||
loc, err := time.LoadLocation(timezone)
|
||||
if err != nil {
|
||||
return fmt.Errorf("加载时区失败 %s: %w", timezone, err)
|
||||
}
|
||||
|
||||
localTimezone = loc
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCurrentTime 获取当前时间(使用设置的时区)
|
||||
func GetCurrentTime() time.Time {
|
||||
if localTimezone != nil {
|
||||
return time.Now().In(localTimezone)
|
||||
}
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
// GetCurrentTimeString 获取当前时间字符串
|
||||
func GetCurrentTimeString() string {
|
||||
return GetCurrentTime().Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
// GetCurrentDate 获取当前日期字符串
|
||||
func GetCurrentDate() string {
|
||||
return GetCurrentTime().Format("2006-01-02")
|
||||
}
|
||||
|
||||
// ParseTime 解析时间字符串到本地时区
|
||||
func ParseTime(layout, value string) (time.Time, error) {
|
||||
t, err := time.Parse(layout, value)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
|
||||
if localTimezone != nil {
|
||||
return t.In(localTimezone), nil
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// FormatTime 将时间格式化为字符串(使用本地时区)
|
||||
func FormatTime(t time.Time, layout string) string {
|
||||
if localTimezone != nil {
|
||||
return t.In(localTimezone).Format(layout)
|
||||
}
|
||||
return t.Format(layout)
|
||||
}
|
||||
|
||||
// GetTimezone 获取当前时区名称
|
||||
func GetTimezone() string {
|
||||
if localTimezone != nil {
|
||||
return localTimezone.String()
|
||||
}
|
||||
return time.Local.String()
|
||||
}
|
||||
Reference in New Issue
Block a user