🎨用户接口处理完毕
This commit is contained in:
@@ -2,7 +2,7 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go.uber.org/zap"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"math/rand"
|
||||
"time"
|
||||
"wireguard-dashboard/client"
|
||||
@@ -21,7 +21,7 @@ func (avatar) GenerateAvatar() (path string, err error) {
|
||||
"eyes=variant01,variant02,variant03,variant04,variant05,variant06,variant07,variant08,variant09,variant10,variant11,variant12&mustache=variant01,variant02,variant03&"+
|
||||
"topColor=000000,0fa958,699bf7", rand.Uint32()))
|
||||
if err != nil {
|
||||
zap.S().Errorf("生成头像失败")
|
||||
log.Errorf("请求头像API失败: %v", err.Error())
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
@@ -6,6 +6,14 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type PageData[T any] struct {
|
||||
Current int `json:"current"` // 当前页码
|
||||
Size int `json:"size"` // 每页数量
|
||||
Total int64 `json:"total"` // 总数
|
||||
TotalPage int `json:"totalPage"` // 总页数
|
||||
Records T `json:"records"` // 返回数据
|
||||
}
|
||||
|
||||
type ginResponse struct {
|
||||
c *gin.Context
|
||||
}
|
||||
@@ -31,7 +39,7 @@ func (r ginResponse) Failed() {
|
||||
func (r ginResponse) FailedWithErr(msg string, err error) {
|
||||
r.c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": http.StatusBadRequest,
|
||||
"message": fmt.Errorf("%s: %s", msg, err.Error()),
|
||||
"message": fmt.Errorf("%s: %s", msg, err.Error()).Error(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -56,3 +64,19 @@ func (r ginResponse) OKWithData(data any) {
|
||||
"data": data,
|
||||
})
|
||||
}
|
||||
|
||||
func (r ginResponse) OkWithPage(data any, total int64, current, size int) {
|
||||
// 处理一下页码、页数量
|
||||
if current == -1 {
|
||||
current = 1
|
||||
size = int(total)
|
||||
}
|
||||
// 计算总页码
|
||||
totalPage := GenTotalPage(total, size)
|
||||
// 返回结果
|
||||
r.c.JSON(http.StatusOK, map[string]any{
|
||||
"code": http.StatusOK,
|
||||
"data": &PageData[any]{Current: current, Size: size, Total: total, TotalPage: totalPage, Records: data},
|
||||
"message": "success",
|
||||
})
|
||||
}
|
||||
|
47
utils/page.go
Normal file
47
utils/page.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package utils
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// Page
|
||||
// @description: 分页组件
|
||||
// @param current
|
||||
// @param size
|
||||
// @return func(db *gorm.DB) *gorm.DB
|
||||
func Page(current, size int) func(db *gorm.DB) *gorm.DB {
|
||||
// 如果页码是-1,就不分页
|
||||
if current == -1 {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
return db
|
||||
}
|
||||
}
|
||||
// 分页
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
if current == 0 {
|
||||
current = 1
|
||||
}
|
||||
if size < 1 {
|
||||
size = 10
|
||||
}
|
||||
// 计算偏移量
|
||||
offset := (current - 1) * size
|
||||
// 返回组装结果
|
||||
return db.Offset(offset).Limit(size)
|
||||
}
|
||||
}
|
||||
|
||||
// GenTotalPage
|
||||
// @description: 计算页码数
|
||||
// @param count
|
||||
// @param size
|
||||
// @return int
|
||||
func GenTotalPage(count int64, size int) int {
|
||||
totalPage := 0
|
||||
if count > 0 {
|
||||
upPage := 0
|
||||
if int(count)%size > 0 {
|
||||
upPage = 1
|
||||
}
|
||||
totalPage = (int(count) / size) + upPage
|
||||
}
|
||||
return totalPage
|
||||
}
|
Reference in New Issue
Block a user