Files
Quincy_admin/services/com_service.go
2026-03-26 22:13:03 +08:00

60 lines
1.2 KiB
Go

package services
import (
"Quincy_admin/repositories"
"Quincy_admin/schemas"
"bufio"
"log"
"os"
"strings"
)
type CommonService struct {
repo *repositories.CommonRepository
}
func NewCommonService(repo *repositories.CommonRepository) *CommonService {
return &CommonService{repo: repo}
}
// GetLoginLogList 获取登录日志列表
func (s *CommonService) GetLoginLogList(req *schemas.LoginLogListRequest) ([]*schemas.LoginLog, int64, error) {
return s.repo.GetLoginLogList(req)
}
// ReadLastLines 读取文件最后几行
func (s *CommonService) ReadLastLines(filepath string, lines int) (string, error) {
file, err := os.Open(filepath)
if err != nil {
return "", err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Println("Error closing file:", err)
}
}(file)
// 使用Scanner按行读取
scanner := bufio.NewScanner(file)
var allLines []string
for scanner.Scan() {
allLines = append(allLines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return "", err
}
// 获取最后几行
totalLines := len(allLines)
start := totalLines - lines
if start < 0 {
start = 0
}
result := allLines[start:]
return strings.Join(result, "\n"), nil
}