wireguard-dashboard/service/log.go
2024-08-08 17:26:50 +08:00

50 lines
1.1 KiB
Go

package service
import (
"gorm.io/gorm"
gdb "wireguard-ui/global/client"
"wireguard-ui/global/constant"
"wireguard-ui/http/param"
"wireguard-ui/http/vo"
"wireguard-ui/model"
)
type log struct {
*gorm.DB
}
func Log() log {
return log{
gdb.DB,
}
}
// CreateLog
// @description: 创建日志
// @receiver l
// @param log
// @return error
func (l log) CreateLog(log model.RequestLog) error {
return l.Create(&log).Error
}
// List
// @description: 分页列表
// @receiver s
// @param p
// @return data
// @return total
// @return err
func (l log) List(p param.Page, loginUser *vo.User) (data []vo.SystemLogItem, total int64, err error) {
sel := l.Scopes(Paginate(p.Current, p.Size)).Table("t_request_log as tsl").
Joins("LEFT JOIN t_user as tu ON tu.id = tsl.user_id").
Select("tsl.id", "tu.nickname as username", "tsl.client_ip", "tsl.method", "tsl.status_code", "tsl.host", "tsl.uri", "tsl.created_at").
Order("tsl.created_at DESC")
if loginUser.IsAdmin == constant.NormalAdmin {
sel.Where("tsl.user_id = ?", loginUser.Id)
}
err = sel.Find(&data).Offset(-1).Limit(-1).Count(&total).Error
return
}