first commit
This commit is contained in:
59
services/com_service.go
Normal file
59
services/com_service.go
Normal file
@@ -0,0 +1,59 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user