fix admin login log
CI / changes (push) Successful in 4s
CI / docker-bff (push) Successful in 3m21s
CI / docker-product (push) Successful in 3m39s
CI / docker-admin (push) Successful in 3m34s
CI / docker-user (push) Successful in 3m29s
CI / docker-ad (push) Successful in 3m29s

This commit is contained in:
2026-08-21 16:07:03 +08:00
parent ccba568457
commit c33fbdb422
30 changed files with 2131 additions and 74 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ type ActionLog struct {
ModuleName string `gorm:"column:module_name;type:varchar(255);comment:操作模块名,如管理员;NOT NULL" json:"module_name"`
Ip string `gorm:"column:ip;type:varchar(20);comment:IP;NOT NULL" json:"ip"`
BrowserInfo string `gorm:"column:browser_info;type:varchar(255);comment:浏览器详细信息" json:"browser_info"`
BrowserName string `gorm:"column:browser_name;type:varchar(50);comment:浏览器名称" json:"browser_name"`
BrowserName string `gorm:"column:browser_name;type:varchar(255);comment:浏览器名称" json:"browser_name"`
BrowserVersion string `gorm:"column:browser_version;type:varchar(50);comment:版本" json:"browser_version"`
Reason string `gorm:"column:reason;type:varchar(255);comment:原因" json:"reason"`
CreateTime utils.CustomTime `gorm:"column:create_time;type:datetime;default:NULL;comment:添加时间;NOT NULL" json:"create_time"`
+1 -1
View File
@@ -8,7 +8,7 @@ type LoginLog struct {
AdminName string `gorm:"column:admin_name;type:varchar(255);comment:登录人姓名" json:"admin_name"`
Ip string `gorm:"column:ip;type:varchar(50);comment:IP;NOT NULL" json:"ip"`
BrowserInfo string `gorm:"column:browser_info;type:varchar(255);comment:浏览器详细信息" json:"browser_info"`
BrowserName string `gorm:"column:browser_name;type:varchar(50);comment:浏览器名称" json:"browser_name"`
BrowserName string `gorm:"column:browser_name;type:varchar(255);comment:浏览器名称" json:"browser_name"`
BrowserVersion string `gorm:"column:browser_version;type:varchar(50);comment:浏览器版本" json:"browser_version"`
Status uint8 `gorm:"column:status;type:tinyint(1);default:1;comment:状态,1为成功,2为失败" json:"status"`
CreateTime utils.CustomTime `gorm:"column:create_time;type:datetime;comment:添加时间;NOT NULL" json:"create_time"`
@@ -0,0 +1,64 @@
package logic
import (
"context"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/services/lonelog/internal/dao"
"lone-services/services/lonelog/internal/model"
"strconv"
"lone-services/rpc/lonelog/pb"
"lone-services/services/lonelog/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type LoginItemsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
BaseLogic
}
func NewLoginItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginItemsLogic {
return &LoginItemsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *LoginItemsLogic) LoginItems(in *lonelog.ItemsRequest) (*lonelog.Response, error) {
modelObj := model.LoginModel{}.Init()
w := modelbase.Params{
Page: int(in.Page),
Size: int(in.Size),
Order: "id desc",
}
w.Like = make(map[string]string)
eq := make(map[string]string)
w.Other = make(map[string]string)
if in.AdminId > utils.NumberZero {
eq["admin_id"] = strconv.FormatInt(in.AdminId, utils.NumberTen)
}
if len(in.Ip) > utils.NumberZero {
w.Like["ip LIKE ? "] = "%%" + in.Ip + "%%"
}
if len(in.Time) > utils.NumberZero {
w.Other["create_time > ?"] = in.Time[utils.NumberZero]
w.Other["create_time < ?"] = in.Time[utils.NumberOne]
}
w.Eq = eq
var data []dao.LoginLog
items, err := modelObj.Page(w, &data)
if err != nil {
l.Logger.Error(err)
return l.fail(utils.Fail)
}
return l.ok(items)
}
@@ -28,6 +28,11 @@ func (s *LonelogServer) Items(ctx context.Context, in *lonelog.ItemsRequest) (*l
return l.Items(in)
}
func (s *LonelogServer) LoginItems(ctx context.Context, in *lonelog.ItemsRequest) (*lonelog.Response, error) {
l := logic.NewLoginItemsLogic(ctx, s.svcCtx)
return l.LoginItems(in)
}
func (s *LonelogServer) ActionAdd(ctx context.Context, in *lonelog.ActionLog) (*lonelog.Response, error) {
l := logic.NewActionAddLogic(ctx, s.svcCtx)
return l.ActionAdd(in)
@@ -81,7 +81,6 @@ func (w *LogWatcher) loopActionLog() {
continue
}
ctxBg := context.Background()
logTime, _ := utils.ParseCustomTime(action.CreateTime)
edit := dao.ActionLog{
Content: action.Content,
@@ -100,7 +99,7 @@ func (w *LogWatcher) loopActionLog() {
err = model.ActionModel{}.Init().Create(&edit)
if err != nil {
logx.Errorf("insert actionlog mysql err=%v payload=%s", err, payload)
_ = logRedis.LogRedisClient.LPush(ctxBg, utils.LogActionQueueKey, payload).Err()
// 删除 LPush 重试逻辑,报错仅打印日志直接丢弃,不再回写队列
time.Sleep(500 * time.Millisecond)
}
}
@@ -147,8 +146,6 @@ func (w *LogWatcher) loopLoginLog() {
continue
}
ctxBg := context.Background()
logTime, _ := utils.ParseCustomTime(login.CreateTime)
edit := dao.LoginLog{
AdminId: login.AdminId,
@@ -164,7 +161,7 @@ func (w *LogWatcher) loopLoginLog() {
err = model.LoginModel{}.Init().Create(&edit)
if err != nil {
logx.Errorf("insert loginlog mysql err=%v payload=%s", err, payload)
_ = logRedis.LogRedisClient.LPush(ctxBg, utils.LogLoginQueueKey, payload).Err()
// 删除 LPush 重试逻辑,报错仅打印日志直接丢弃,不再回写队列
time.Sleep(500 * time.Millisecond)
}
}
+11 -4
View File
@@ -14,13 +14,15 @@ import (
)
type (
ActionLog = lonelog.ActionLog
ItemsRequest = lonelog.ItemsRequest
LoginLog = lonelog.LoginLog
Response = lonelog.Response
ActionLog = lonelog.ActionLog
ItemsLoginRequest = lonelog.ItemsLoginRequest
ItemsRequest = lonelog.ItemsRequest
LoginLog = lonelog.LoginLog
Response = lonelog.Response
Lonelog interface {
Items(ctx context.Context, in *ItemsRequest, opts ...grpc.CallOption) (*Response, error)
LoginItems(ctx context.Context, in *ItemsRequest, opts ...grpc.CallOption) (*Response, error)
ActionAdd(ctx context.Context, in *ActionLog, opts ...grpc.CallOption) (*Response, error)
LoginAdd(ctx context.Context, in *LoginLog, opts ...grpc.CallOption) (*Response, error)
}
@@ -41,6 +43,11 @@ func (m *defaultLonelog) Items(ctx context.Context, in *ItemsRequest, opts ...gr
return client.Items(ctx, in, opts...)
}
func (m *defaultLonelog) LoginItems(ctx context.Context, in *ItemsRequest, opts ...grpc.CallOption) (*Response, error) {
client := lonelog.NewLonelogClient(m.cli.Conn())
return client.LoginItems(ctx, in, opts...)
}
func (m *defaultLonelog) ActionAdd(ctx context.Context, in *ActionLog, opts ...grpc.CallOption) (*Response, error) {
client := lonelog.NewLonelogClient(m.cli.Conn())
return client.ActionAdd(ctx, in, opts...)