add action,login log
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package logRedis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
goredis "github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const Nil = goredis.Nil
|
||||
|
||||
var LogRedisClient *goredis.Client
|
||||
|
||||
type LogRedisConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
Password string
|
||||
DB int
|
||||
}
|
||||
|
||||
func Init(c LogRedisConfig) error {
|
||||
client, err := New(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
LogRedisClient = client
|
||||
return nil
|
||||
}
|
||||
|
||||
func New(c LogRedisConfig) (*goredis.Client, error) {
|
||||
if c.Host == "" {
|
||||
return nil, fmt.Errorf("redis: Host is empty")
|
||||
}
|
||||
if c.Port == 0 {
|
||||
c.Port = 6379
|
||||
}
|
||||
|
||||
client := goredis.NewClient(&goredis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", c.Host, c.Port),
|
||||
Password: c.Password,
|
||||
DB: c.DB,
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
_ = client.Close()
|
||||
return nil, fmt.Errorf("redis ping: %w", err)
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
+53
-6
@@ -1,9 +1,12 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"lone-services/pkg/logRedis"
|
||||
lonelog "lone-services/rpc/lonelog/pb"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -14,6 +17,7 @@ const (
|
||||
LogActionTypeDown = 5 //下载
|
||||
LogActionTypeStatus = 6 //状态
|
||||
LogActionTypePassword = 7 //修改密码
|
||||
LogActionTypeSort = 8 //排序
|
||||
|
||||
//增加、删除、修改时需要改 services.log中的返回值
|
||||
|
||||
@@ -44,6 +48,20 @@ const (
|
||||
LogActionModuleFactory = "工厂管理"
|
||||
)
|
||||
|
||||
type ActionLog struct {
|
||||
Content string `json:"content"`
|
||||
AdminId int64 `json:"admin_id"`
|
||||
AdminName string `json:"admin_name"`
|
||||
Type int32 `json:"type"`
|
||||
ModuleName string `json:"module_name"`
|
||||
Ip string `json:"ip"`
|
||||
BrowserInfo string `json:"browser_info"`
|
||||
BrowserName string `json:"browser_name"`
|
||||
BrowserVersion string `json:"browser_version"`
|
||||
Reason string `json:"reason"`
|
||||
CreateTime string `json:"create_time"`
|
||||
}
|
||||
|
||||
type ActionAdd struct {
|
||||
NewContent interface{} `json:"new_content"`
|
||||
OldContent interface{} `json:"old_content"`
|
||||
@@ -52,15 +70,29 @@ type ActionAdd struct {
|
||||
ModuleName string `json:"module_name"`
|
||||
}
|
||||
|
||||
const LoginReasonOK = "OK"
|
||||
type LoginLog struct {
|
||||
AdminId int64 `json:"admin_id"`
|
||||
Ip string `json:"ip"`
|
||||
Name string `json:"name"`
|
||||
BrowserInfo string `json:"browser_info"`
|
||||
BrowserName string `json:"browser_name"`
|
||||
BrowserVersion string `json:"browser_version"`
|
||||
Status int32 `json:"status"`
|
||||
CreateTime string `json:"create_time"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
func GetActionStruct(adminInfo UserInfo, info ActionAdd) *lonelog.ActionLog {
|
||||
const LoginReasonOK = "OK"
|
||||
const LogActionQueueKey = "log:queue:action"
|
||||
const LogLoginQueueKey = "log:queue:login"
|
||||
|
||||
func SetActionLog(adminInfo UserInfo, info ActionAdd) {
|
||||
content := map[string]interface{}{
|
||||
"old_content": info.OldContent,
|
||||
"new_content": info.NewContent,
|
||||
}
|
||||
jsonData, _ := jsoniter.Marshal(content)
|
||||
return &lonelog.ActionLog{
|
||||
log := ActionLog{
|
||||
Content: string(jsonData),
|
||||
AdminId: adminInfo.ID,
|
||||
AdminName: adminInfo.Name,
|
||||
@@ -74,18 +106,25 @@ func GetActionStruct(adminInfo UserInfo, info ActionAdd) *lonelog.ActionLog {
|
||||
CreateTime: Now().GoString(),
|
||||
}
|
||||
|
||||
logJson, err := jsoniter.Marshal(log)
|
||||
if err != nil {
|
||||
logx.Errorf("marshal ActionLog failed: %v", err)
|
||||
return
|
||||
}
|
||||
logRedis.LogRedisClient.LPush(context.Background(), LogActionQueueKey, string(logJson)).Result()
|
||||
|
||||
}
|
||||
|
||||
func GetLogStruct(adminInfo UserInfo, status bool, reason string) *lonelog.LoginLog {
|
||||
func SetLoginLog(adminInfo UserInfo, status bool, reason string) {
|
||||
st := 2
|
||||
if status {
|
||||
st = 1
|
||||
}
|
||||
|
||||
return &lonelog.LoginLog{
|
||||
log := lonelog.LoginLog{
|
||||
AdminId: adminInfo.ID,
|
||||
Ip: adminInfo.ClientIP,
|
||||
Name: adminInfo.Name,
|
||||
Ip: adminInfo.ClientIP,
|
||||
BrowserInfo: adminInfo.UserAgent,
|
||||
BrowserName: adminInfo.BrowserName,
|
||||
BrowserVersion: adminInfo.BrowserVer,
|
||||
@@ -93,4 +132,12 @@ func GetLogStruct(adminInfo UserInfo, status bool, reason string) *lonelog.Login
|
||||
CreateTime: Now().GoString(),
|
||||
Reason: reason,
|
||||
}
|
||||
|
||||
logJson, err := jsoniter.Marshal(log)
|
||||
if err != nil {
|
||||
logx.Errorf("marshal ActionLog failed: %v", err)
|
||||
return
|
||||
}
|
||||
logRedis.LogRedisClient.LPush(context.Background(), LogLoginQueueKey, string(logJson)).Result()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user