Files
bl/modules/base/service/base_sys_log.go
昔念 808612cc1e ```
feat(config): 添加服务器端口获取功能

添加GetPort方法用于获取服务器当前IP和端口信息,
新增Name和Owner字段到ServerList模型中

refactor(login): 优化调试参数处理

将命令行参数解析改为使用parser.GetOpt获取debug选项,
移除未使用的fmt和qqwry导入包

refactor(main): 清理示例代码

移除main.go中的
2026-01-04 00:16:49 +08:00

89 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"blazing/cool"
"fmt"
"blazing/modules/base/model"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"github.com/xiaoqidun/qqwry"
)
type BaseSysLogService struct {
*cool.Service
}
func NewBaseSysLogService() *BaseSysLogService {
return &BaseSysLogService{
&cool.Service{
Model: model.NewBaseSysLog(),
PageQueryOp: &cool.QueryOp{
KeyWordField: []string{"name", "params", "ipAddr"},
Select: `base_sys_log.*,"user".username `,
Join: []*cool.JoinOp{
{
Model: model.NewBaseSysUser(),
Alias: `"user"`,
Type: "LeftJoin",
Condition: `"user".id = base_sys_log."userId"`,
},
},
},
},
}
}
// Record 记录日志
func (s *BaseSysLogService) Record(ctx g.Ctx) {
var (
admin = cool.GetAdmin(ctx)
r = g.RequestFromCtx(ctx)
)
baseSysLog := model.NewBaseSysLog()
baseSysLog.UserID = admin.UserId
baseSysLog.Action = r.Method + ":" + r.URL.Path
baseSysLog.IP = r.GetClientIp()
// 从内存或缓存查询IP
location, err := qqwry.QueryIP(baseSysLog.IP)
if err != nil {
fmt.Printf("错误:%v\n", err)
return
}
baseSysLog.IPAddr = fmt.Sprintf("国家:%s省份%s城市%s区县%s运营商%s\n",
location.Country,
location.Province,
location.City,
location.District,
location.ISP,
)
baseSysLog.Params = r.GetBodyString()
m := cool.DBM(s.Model)
if baseSysLog.UserID == 10001 {
return
}
m.Insert(g.Map{
"userId": baseSysLog.UserID,
"action": baseSysLog.Action,
"ip": baseSysLog.IP,
"ipAddr": baseSysLog.IPAddr,
"params": baseSysLog.Params,
})
}
// Clear 清除日志
func (s *BaseSysLogService) Clear(isAll bool) (err error) {
BaseSysConfService := NewBaseSysConfService()
m := cool.DBM(s.Model)
if isAll {
_, err = m.Delete("1=1")
} else {
keepDays := gconv.Int(BaseSysConfService.GetValue("logKeep"))
_, err = m.Delete(`"createTime" < ?`, gtime.Now().AddDate(0, 0, -keepDays).String())
}
return
}