Files
bl/modules/base/service/base_sys_log.go
昔念 1bd6840e98 ```
feat(base): 添加IP数据库文件加载功能

- 移除qqwry.go中被注释的embed代码和init函数
- 在base_sys_log.go的init函数中实现IP数据库文件加载逻辑
- 添加从public/qqwry.ipdb文件加载IP数据库的功能
- 当IP数据库加载失败时panic处理
```
2026-01-04 01:31:11 +08:00

97 lines
2.1 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
}
func init() {
// 从文件加载IP数据库
if err := qqwry.LoadFile("public/qqwry.ipdb"); err != nil {
panic(err)
}
}