init: web_hgcg_server 初始化提交

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-10-27 16:44:55 +08:00
co-authored by Claude Opus 4.7
commit 189556e702
627 changed files with 126228 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
package oplog
import (
"videoweb/pub/mongos"
)
var (
opLogColl *mongos.Collection
)
+175
View File
@@ -0,0 +1,175 @@
package oplog
import (
"context"
"fmt"
"time"
"videoweb/pub/common/log"
"videoweb/pub/common/srvdef"
"videoweb/pub/common/utils"
"videoweb/pub/mongos"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Init 初始化数据
func Init(ctx context.Context) {
opLogColl = mongos.ShopLog.Coll(TN.OpLog)
initIndex(ctx)
return
}
func initIndex(ctx context.Context) {
indexes := []mongo.IndexModel{
{
Keys: bson.D{
{FN.OpLog.ActId, 1},
{FN.OpLog.Action, 1},
{FN.OpLog.Path, 1},
},
},
{
Keys: bson.D{
{FN.OpLog.CreatedAt, -1},
},
},
}
if _, err := opLogColl.CreateIndex(ctx, indexes); err != nil {
panic(fmt.Sprintf("(oplog:initIndex) Create adLog Index failed! err:%+v:", err))
}
}
// 添加日志
func AddLog(ctx context.Context, path string, desc string, actId int64, rawData, ip string) (err error) {
_, err = opLogColl.InsertOne(ctx, primitive.M{
FN.OpLog.Id: mongos.Lid.MwOpLog.Get(),
FN.OpLog.Action: desc,
FN.OpLog.Path: path,
FN.OpLog.ActId: actId,
FN.OpLog.RawData: rawData,
FN.OpLog.CreatedAt: utils.Now(),
FN.OpLog.IP: ip,
})
return
}
type LogReq struct {
PageNum int64 `json:"pageNum"` // 页码
PageSize int64 `json:"pageSize"` //
Path *string `json:"path"` // 请求接口
Action *string `json:"action"` // 操作目的
ActId *int64 `json:"actId"` // 操作人Id
StartTime *time.Time `json:"startTime"` // 开始时间
EndTime *time.Time `json:"endTime"` // 开始时间
}
// 操作日志信息
type OpLogInfo struct {
Path string `json:"path" bson:"path"` // 请求接口
ActId int64 `json:"actId" bson:"actId"` // 操作人Id
Action string `json:"action" bson:"action"` // 操作目的
Param string `json:"param" bson:"rawData"` // 操作内容
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 操作时间
ActName string `json:"actName" bson:"actName"` // 操作人
}
type OpPageRes struct {
Total int64 `json:"total" bson:"total"`
List []*OpLogInfo `json:"list" bson:"list"`
}
// 获取日志列表
func PageLogList(ctx context.Context, req *LogReq) (res *OpPageRes, err error) {
filter := bson.M{}
if req.Path != nil && *req.Path != "" {
filter[FN.OpLog.Path] = bson.M{"$regex": req.Path}
}
if req.Action != nil && *req.Action != "" {
filter[FN.OpLog.Action] = req.Action
}
if req.ActId != nil && *req.ActId > 0 {
filter[FN.OpLog.ActId] = req.ActId
}
if req.StartTime != nil && req.EndTime != nil {
filter[FN.OpLog.CreatedAt] = bson.M{"$gte": req.StartTime, "$lt": req.EndTime}
}
res = new(OpPageRes)
res.Total, err = mongos.ShopLog.Coll(TN.OpLog).Count(ctx, filter)
if err != nil {
return
}
sort := bson.M{FN.OpLog.CreatedAt: -1}
opt := &options.FindOptions{}
opt.SetSort(sort)
skip := (req.PageNum - 1) * req.PageSize
opt.SetSkip(skip)
opt.SetLimit(req.PageSize)
list := make(OpLogList, 0)
err = mongos.ShopLog.Coll(TN.OpLog).Find(ctx, &list, filter, opt)
if err != nil {
return
}
actIds := make([]int64, 0)
for _, v := range list {
actIds = append(actIds, v.ActId)
}
// err = opLogColl.Aggregate(ctx, &res.List, []bson.M{
// {"$match": conf},
// {"$sort": bson.M{FN.OpLog.CreatedAt: -1}},
// {"$skip": (req.PageNum - 1) * req.PageSize},
// {"$limit": req.PageSize},
// {"$lookup": bson.M{
// "localField": FN.OpLog.ActId,
// "from": mwacc.TN.MwAcc,
// "foreignField": mwacc.FN.MwAcc.Id,
// "as": "admins",
// }},
// {"$unwind": "$admins"},
// {"$project": bson.M{
// FN.OpLog.Path: 1,
// FN.OpLog.Action: 1,
// FN.OpLog.ActId: 1,
// FN.OpLog.RawData: 1,
// FN.OpLog.CreatedAt: 1,
// "actName": "$admins.userName",
// }},
// })
return
}
// CountOpLogs 计算某个查询的数量
func CountOpLogs(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
total, err := mongos.ShopLog.Coll(TN.OpLog).Count(ctx, filter)
if err != nil {
log.Info("oplog:CountOpLogs db.Count failed",
log.E(err), log.Any("filter", filter))
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
}
return total, nil
}
// InsertOneUser 插入一个用户
func InsertOneUser(ctx context.Context, opLog *OpLog) *srvdef.SrvErr {
if _, err := mongos.ShopLog.Coll(TN.OpLog).InsertOne(ctx, opLog); err != nil {
log.Warn("oplog:InsertOneUser db.InsertOne failed", log.E(err))
return srvdef.New(srvdef.ErrDbInsertError, "", "")
}
return nil
}
// FindManyOpLogs 查找一批数据
func FindManyOpLogs(ctx context.Context,
filter bson.M, opts ...*options.FindOptions) (
OpLogList, *srvdef.SrvErr) {
data := make(OpLogList, 0)
if err := mongos.ShopLog.Coll(TN.OpLog).Find(ctx, &data, filter, opts...); err != nil {
log.Warn("oplog:FindManyOpLogs db.Find failed",
log.E(err), log.Any("filter", filter))
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
}
return data, nil
}
+34
View File
@@ -0,0 +1,34 @@
package oplog
type TagOpLog struct {
ActId string
Action string
CreatedAt string
IP string
Id string
IsWhite string
OpType string
Path string
RawData string
}
var FN struct {
OpLog TagOpLog
}
var TN struct {
OpLog string
}
func init() {
FN.OpLog.ActId = "actId"
FN.OpLog.Action = "action"
FN.OpLog.CreatedAt = "createdAt"
FN.OpLog.IP = "ip"
FN.OpLog.Id = "_id"
FN.OpLog.IsWhite = "isWhite"
FN.OpLog.OpType = "opType"
FN.OpLog.Path = "path"
FN.OpLog.RawData = "rawData"
TN.OpLog = "mw_op_log"
}
+22
View File
@@ -0,0 +1,22 @@
package oplog
import (
"time"
"videoweb/pub/common"
)
// 操作日志
// @table:mw_op_log
type OpLog struct {
Id int64 `bson:"_id"` // id
Path string `bson:"path"` // 请求接口
OpType common.ApiOpType `bson:"opType"` // 操作类型
IsWhite bool `bson:"isWhite"` // 是否白名单
ActId int64 `bson:"actId"` // 操作人Id
Action string `bson:"action"` // 操作目的
RawData string `bson:"rawData"` // 原始数据
CreatedAt time.Time `bson:"createdAt"` // 操作时间
IP string `bson:"ip"` // 操作人IP
}
type OpLogList []*OpLog