lonelog to record
CI / changes (push) Successful in 35s
CI / ad (push) Successful in 3s
CI / admin (push) Successful in 1s
CI / bff (push) Successful in 1s
CI / chore (push) Successful in 2s
CI / equipment (push) Successful in 2s
CI / express (push) Successful in 2s
CI / product (push) Successful in 2s
CI / record (push) Successful in 27s
CI / sale (push) Successful in 2s
CI / task (push) Successful in 2s
CI / user (push) Successful in 2s
CI / wecom (push) Successful in 2s

This commit is contained in:
2026-09-03 20:12:06 +08:00
parent 9fb4f568d4
commit 322f01998a
20 changed files with 27 additions and 27 deletions
+13
View File
@@ -0,0 +1,13 @@
package config
type Config struct {
Nacos NacosConf
}
type NacosConf struct {
Hosts []string
NamespaceId string `json:",optional"`
Group string `json:",optional"`
RegisterIP string `json:",optional"`
ConfigID string `json:",optional"`
}
@@ -0,0 +1,30 @@
package dao
import "lone-services/pkg/utils"
// ActionLog 操作日志表
type ActionLog struct {
Id int `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT;comment:ID" json:"id"`
Content string `gorm:"column:content;type:text;comment:内容;NOT NULL" json:"content"`
AdminId int64 `gorm:"column:admin_id;type:int(11);comment:操作人;NOT NULL" json:"admin_id"`
AdminName string `gorm:"column:admin_name;type:varchar(255);comment:真实姓名" json:"admin_name"`
Type uint8 `gorm:"column:type;type:smallint(3);comment:类型,以常量配置文件为准,目前,1为添加,2为编辑,3为删除,4为强退,5为下载;NOT NULL" json:"type"`
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(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"`
}
type ActionItems struct {
Items []ActionLog `json:"items"`
Count int64 `json:"count"`
Type []ActionType `json:"type"`
}
type ActionType struct {
Id int `json:"id"`
Name string `json:"name"`
}
+25
View File
@@ -0,0 +1,25 @@
package dao
import "lone-services/pkg/utils"
type LoginLog struct {
Id int64 `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT;comment:ID" json:"id"`
AdminId int64 `gorm:"column:admin_id;type:int(11);default:0;comment:登录人ID;NOT NULL" json:"admin_id"`
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(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"`
Reason string `gorm:"column:reason;type:varchar(50);comment:失败原因" json:"reason"`
}
type LoginLogItems struct {
Items []LoginLog `json:"items"`
Count int64 `json:"count"`
}
type Count struct {
Id int `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT;comment:ID" json:"id"`
}
@@ -0,0 +1,29 @@
package logic
import (
"context"
lonelog "lone-services/rpc/lonelog/pb"
"lone-services/services/record/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type ActionAddLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewActionAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ActionAddLogic {
return &ActionAddLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *ActionAddLogic) ActionAdd(in *lonelog.ActionLog) (*lonelog.Response, error) {
// todo: add your logic here and delete this line
return &lonelog.Response{}, nil
}
+44
View File
@@ -0,0 +1,44 @@
package logic
import (
"lone-services/pkg/utils"
"lone-services/pkg/validate"
lonelog "lone-services/rpc/lonelog/pb"
"reflect"
jsoniter "github.com/json-iterator/go"
)
type BaseLogic struct {
}
func (l *BaseLogic) checkParams(in interface{}, v validate.IValidator) *lonelog.Response {
rv := reflect.ValueOf(in)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return &lonelog.Response{
Code: utils.ErrorParams.Code,
Msg: "request must be nonnil proto pointer",
}
}
resp := validate.ValidateFromProto(in, v)
if resp != utils.StringEmpty {
return &lonelog.Response{
Code: utils.ErrorParams.Code,
Msg: resp,
}
}
return nil
}
func (l *BaseLogic) fail(status utils.Status) (*lonelog.Response, error) {
return l.out(status, status.Msg)
}
func (l *BaseLogic) out(status utils.Status, msg string) (*lonelog.Response, error) {
return &lonelog.Response{Code: status.Code, Msg: msg}, nil
}
func (l *BaseLogic) ok(data any) (*lonelog.Response, error) {
buf, _ := jsoniter.Marshal(data)
return &lonelog.Response{Code: utils.Ok.Code, Msg: utils.Ok.Msg, Data: string(buf)}, nil
}
@@ -0,0 +1,69 @@
package logic
import (
"context"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
lonelog "lone-services/rpc/lonelog/pb"
"lone-services/services/record/internal/dao"
"lone-services/services/record/internal/model"
"lone-services/services/record/internal/svc"
"strconv"
"github.com/zeromicro/go-zero/core/logx"
)
type ItemsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
BaseLogic
}
// NewItemsLogic
func NewItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ItemsLogic {
return &ItemsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *ItemsLogic) Items(in *lonelog.ItemsRequest) (*lonelog.Response, error) {
modelObj := model.ActionModel{}.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 in.Type > utils.NumberZero {
eq["type"] = string(in.Type)
}
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.ActionLog
items, err := modelObj.Page(w, &data)
if err != nil {
l.Logger.Error(err)
return l.fail(utils.Fail)
}
return l.ok(items)
}
@@ -0,0 +1,64 @@
package logic
import (
"context"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/services/record/internal/dao"
"lone-services/services/record/internal/model"
"strconv"
"lone-services/rpc/lonelog/pb"
"lone-services/services/record/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)
}
@@ -0,0 +1,29 @@
package logic
import (
"context"
lonelog "lone-services/rpc/lonelog/pb"
"lone-services/services/record/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type LoginAddLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewLoginAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginAddLogic {
return &LoginAddLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *LoginAddLogic) LoginAdd(in *lonelog.LoginLog) (*lonelog.Response, error) {
// todo: add your logic here and delete this line
return &lonelog.Response{}, nil
}
+23
View File
@@ -0,0 +1,23 @@
package model
import (
"lone-services/pkg/modelbase"
"lone-services/services/record/internal/dao"
)
type ActionModel struct {
modelbase.Base
}
func (m ActionModel) TableName() string {
return modelbase.Prefix() + "admin_action"
}
func (m ActionModel) Init() ActionModel {
m.Table = m.TableName()
return m
}
func (m ActionModel) Create(data *dao.ActionLog) error {
return m.Base.Create(data)
}
+23
View File
@@ -0,0 +1,23 @@
package model
import (
"lone-services/pkg/modelbase"
"lone-services/services/record/internal/dao"
)
type LoginModel struct {
modelbase.Base
}
func (m LoginModel) TableName() string {
return modelbase.Prefix() + "admin_login"
}
func (m LoginModel) Init() LoginModel {
m.Table = m.TableName()
return m
}
func (m LoginModel) Create(data *dao.LoginLog) error {
return m.Base.Create(data)
}
@@ -0,0 +1,44 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.10.2
// Source: lonelog.proto
package server
import (
"context"
"lone-services/rpc/lonelog/pb"
"lone-services/services/record/internal/logic"
"lone-services/services/record/internal/svc"
)
type LonelogServer struct {
svcCtx *svc.ServiceContext
lonelog.UnimplementedLonelogServer
}
func NewLonelogServer(svcCtx *svc.ServiceContext) *LonelogServer {
return &LonelogServer{
svcCtx: svcCtx,
}
}
func (s *LonelogServer) Items(ctx context.Context, in *lonelog.ItemsRequest) (*lonelog.Response, error) {
l := logic.NewItemsLogic(ctx, s.svcCtx)
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)
}
func (s *LonelogServer) LoginAdd(ctx context.Context, in *lonelog.LoginLog) (*lonelog.Response, error) {
l := logic.NewLoginAddLogic(ctx, s.svcCtx)
return l.LoginAdd(in)
}
@@ -0,0 +1,175 @@
package service
import (
"context"
"encoding/json"
"lone-services/pkg/logRedis"
"lone-services/pkg/utils"
"lone-services/services/record/internal/dao"
"lone-services/services/record/internal/model"
"sync"
"time"
"github.com/zeromicro/go-zero/core/logx"
)
// LogWatcher 队列监听器,内部直接使用 logredis.LogRedisClient 包全局变量,不再传入redis参数
type LogWatcher struct {
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
}
// GlobalLogWatcher 全局单例,main初始化之后其它包直接 service.GlobalLogWatcher 访问
var GlobalLogWatcher *LogWatcher
// NewLogWatcher 无入参,直接使用包全局 logredis.LogRedisClient
func NewLogWatcher() *LogWatcher {
ctx, cancel := context.WithCancel(context.Background())
return &LogWatcher{
ctx: ctx,
cancel: cancel,
}
}
// StartWatch 启动两个消费协程
func (w *LogWatcher) StartWatch() {
w.wg.Add(2)
go w.loopActionLog()
go w.loopLoginLog()
logx.Infof("LogWatcher start, watch %s , %s", utils.LogActionQueueKey, utils.LogLoginQueueKey)
}
// loopActionLog 消费action日志队列,带recover自动重启
func (w *LogWatcher) loopActionLog() {
defer func() {
if r := recover(); r != nil {
logx.Errorf("loopActionLog panic recovered, err=%v", r)
time.Sleep(2 * time.Second)
w.wg.Add(1)
go w.loopActionLog()
}
w.wg.Done()
}()
for {
select {
case <-w.ctx.Done():
logx.Info("loopActionLog received exit signal, quit")
return
default:
}
res, err := logRedis.LogRedisClient.BRPop(w.ctx, 5*time.Second, utils.LogActionQueueKey).Result()
if err != nil {
if err.Error() == "redis: nil" {
continue
}
logx.Errorf("BRPop %s redis err: %v", utils.LogActionQueueKey, err)
time.Sleep(1 * time.Second)
continue
}
if len(res) < 2 {
continue
}
payload := res[1]
var action utils.ActionLog
if err := json.Unmarshal([]byte(payload), &action); err != nil {
logx.Errorf("unmarshal actionlog failed payload=%s err=%v", payload, err)
continue
}
logTime, _ := utils.ParseCustomTime(action.CreateTime)
edit := dao.ActionLog{
Content: action.Content,
AdminId: action.AdminId,
AdminName: action.AdminName,
Type: uint8(action.Type),
ModuleName: action.ModuleName,
Ip: action.Ip,
BrowserInfo: action.BrowserInfo,
BrowserName: action.BrowserName,
BrowserVersion: action.BrowserVersion,
Reason: action.Reason,
CreateTime: logTime,
}
err = model.ActionModel{}.Init().Create(&edit)
if err != nil {
logx.Errorf("insert actionlog mysql err=%v payload=%s", err, payload)
// 删除 LPush 重试逻辑,报错仅打印日志直接丢弃,不再回写队列
time.Sleep(500 * time.Millisecond)
}
}
}
// loopLoginLog 消费登录日志队列,带recover自动重启
func (w *LogWatcher) loopLoginLog() {
defer func() {
if r := recover(); r != nil {
logx.Errorf("loopLoginLog panic recovered, err=%v", r)
time.Sleep(2 * time.Second)
w.wg.Add(1)
go w.loopLoginLog()
}
w.wg.Done()
}()
for {
select {
case <-w.ctx.Done():
logx.Info("loopLoginLog received exit signal, quit")
return
default:
}
res, err := logRedis.LogRedisClient.BRPop(w.ctx, 5*time.Second, utils.LogLoginQueueKey).Result()
if err != nil {
if err.Error() == "redis: nil" {
continue
}
logx.Errorf("BRPop %s redis err: %v", utils.LogLoginQueueKey, err)
time.Sleep(1 * time.Second)
continue
}
if len(res) < 2 {
continue
}
payload := res[1]
var login utils.LoginLog
if err := json.Unmarshal([]byte(payload), &login); err != nil {
logx.Errorf("unmarshal loginlog failed payload=%s err=%v", payload, err)
continue
}
logTime, _ := utils.ParseCustomTime(login.CreateTime)
edit := dao.LoginLog{
AdminId: login.AdminId,
AdminName: login.Name,
Ip: login.Ip,
BrowserInfo: login.BrowserInfo,
BrowserName: login.BrowserName,
BrowserVersion: login.BrowserVersion,
Reason: login.Reason,
CreateTime: logTime,
Status: uint8(login.Status),
}
err = model.LoginModel{}.Init().Create(&edit)
if err != nil {
logx.Errorf("insert loginlog mysql err=%v payload=%s", err, payload)
// 删除 LPush 重试逻辑,报错仅打印日志直接丢弃,不再回写队列
time.Sleep(500 * time.Millisecond)
}
}
}
// StopWatch 优雅停止消费协程
func (w *LogWatcher) StopWatch() {
w.cancel()
w.wg.Wait()
logx.Info("LogWatcher stopped complete")
}
@@ -0,0 +1,22 @@
package svc
import (
"lone-services/pkg/utils"
"lone-services/services/record/internal/config"
"gorm.io/gorm"
)
type ServiceContext struct {
Config config.Config
DB *gorm.DB
Prefix string
}
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
return &ServiceContext{
Config: c,
DB: db,
Prefix: utils.GetConfigString("mysql.prefix"),
}
}