提交 fe385d07 authored 作者: mooncake9527's avatar mooncake9527

update

上级 4ec77d7e
......@@ -7,7 +7,7 @@ import (
"strings"
"time"
xsf "gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
......@@ -120,25 +120,6 @@ func NewCtx(ctx context.Context) context.Context {
return context.WithValue(context.Background(), ContextTraceIDKey, ctx.Value(ContextTraceIDKey))
}
func GetGinUserName(c *gin.Context) string {
return getGinVal[string](c, KeyUName)
}
func GetGinUserID(c *gin.Context) xsf.ID {
return getGinVal[xsf.ID](c, KeyUID)
}
func GetGinShopID(c *gin.Context) xsf.ID {
return getGinVal[xsf.ID](c, KeyShopID)
}
func GetCtxUserID(c context.Context) xsf.ID {
return getCtxVal[xsf.ID](c, KeyUID)
}
func GetCtxShopID(c context.Context) xsf.ID {
return getCtxVal[xsf.ID](c, KeyShopID)
}
func GetCtxString(c context.Context, key string) string {
val := c.Value(key)
if val != nil {
......@@ -217,16 +198,80 @@ func GetGinCtx(ctx context.Context) (*gin.Context, error) {
return nil, ErrorGinContextNotFound
}
func GetCtxUID(c context.Context) uint {
return getCtxVal[uint](c, KeyUID)
func GetGinUID(c context.Context) xsf.ID {
if val := c.Value(KeyCompanyID); val != nil {
if v, ok := val.(xsf.ID); ok {
return v
}
v := cast.ToInt64(val)
return xsf.ParseInt64(v)
}
return xsf.ID(-1)
}
func GetCtxUType(c context.Context) uint {
return getCtxVal[uint](c, KeyUType)
func GetCtxUID(c context.Context) xsf.ID {
if val := c.Value(KeyCompanyID); val != nil {
if v, ok := val.(xsf.ID); ok {
return v
}
v := cast.ToInt64(val)
return xsf.ParseInt64(v)
}
return xsf.ID(-1)
}
func GetGinUType(c *gin.Context) int {
if val := c.Value(KeyUType); val != nil {
if v, ok := val.(int); ok {
return v
}
return cast.ToInt(val)
}
return -1
}
func GetCtxCompanyID(c context.Context) uint {
return getCtxVal[uint](c, KeyCompanyID)
func GetCtxUType(c context.Context) int {
if val := c.Value(KeyUType); val != nil {
if v, ok := val.(int); ok {
return v
}
return cast.ToInt(val)
}
return -1
}
func GetGinUserName(c *gin.Context) string {
return getGinVal[string](c, KeyUName)
}
func GetGinShopID(c *gin.Context) xsf.ID {
return getGinVal[xsf.ID](c, KeyShopID)
}
func GetCtxShopID(c context.Context) xsf.ID {
return getCtxVal[xsf.ID](c, KeyShopID)
}
func GetGinCompanyID(c *gin.Context) xsf.ID {
if val := c.Value(KeyCompanyID); val != nil {
if v, ok := val.(xsf.ID); ok {
return v
}
v := cast.ToInt64(val)
return xsf.ParseInt64(v)
}
return xsf.ID(-1)
}
func GetCtxCompanyID(c context.Context) xsf.ID {
if val := c.Value(KeyCompanyID); val != nil {
if v, ok := val.(xsf.ID); ok {
return v
}
v := cast.ToInt64(val)
return xsf.ParseInt64(v)
}
return xsf.ID(-1)
}
func getGinVal[T any](c *gin.Context, key string) T {
......
......@@ -16,7 +16,6 @@ import (
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
merge "gitlab.wanzhuangkj.com/tush/xpkg/merger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
pkgCtxUtils "gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/jsonUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/retryUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
......@@ -32,35 +31,28 @@ func WebLogInterceptor() func(c *gin.Context) {
rsp *entity.CopyHttpRsp
respCode int
)
ctx := pkgCtxUtils.WrapCtx(c)
ctx := ctxUtils.WrapCtx(c)
URL := strings.ToLower(c.Request.Method) + "#" + c.Request.URL.Path
xApi, ok := api.XApi.M[URL]
if ok {
uid := ctxUtils.GetGinUserID(c)
companyID := ctxUtils.GetCtxCompanyID(c)
uid := ctxUtils.GetGinUID(c)
uname := ctxUtils.GetGinUserName(c)
uType := User_UserType_Enum(getUserType(ctx)).Desc()
uType := getUserType(c)
req = entity.GetCopyReq(c)
if uid > 0 {
defer func() {
if respCode > 0 {
AddAsync(ctx, "", fmt.Sprintf("%s[%s]%s", uType, uname, xApi.Summary), nil, nil)
} else {
AddAsync(ctx, "", fmt.Sprintf("%s[%s]%s", uType, uname, xApi.Summary), req, rsp)
}
AddAsync(ctx, "", fmt.Sprintf("%d-%d-%s %s", companyID, uType, uname, xApi.Summary), req, rsp)
}()
} else {
defer func() {
if respCode > 0 {
AddAsync(ctx, "", xApi.Summary, nil, nil)
} else {
AddAsync(ctx, "", xApi.Summary, req, rsp)
}
AddAsync(ctx, "", xApi.Summary, req, rsp)
}()
}
}
c.Next()
if ok {
respCode = c.GetInt(pkgCtxUtils.KeyRspCode)
respCode = c.GetInt(ctxUtils.KeyRspCode)
if respCode <= 0 {
rsp = entity.GetCopyRsp(c)
}
......@@ -96,7 +88,7 @@ func Init(schema string, isProd bool) {
if l.Len() == 0 {
return nil
}
ctx := context.WithValue(context.Background(), pkgCtxUtils.ContextTraceIDKey, xsf.GenerateID().String())
ctx := context.WithValue(context.Background(), ctxUtils.ContextTraceIDKey, xsf.GenerateID().String())
ors := make([]*WebLog, 0, l.Len())
for e := l.Front(); e != nil; e = e.Next() {
le, ok := e.Value.(*WebLog)
......@@ -110,9 +102,9 @@ func Init(schema string, isProd bool) {
insertFn := func() error { return WebLogDao.CreateSliceSilent(ctx, ors) }
go func() {
if err := retryUtils.Retry(insertFn, retryUtils.RetryTimes(10), retryUtils.RetryWithLinearBackoff(32*time.Minute)); err != nil {
logger.Error("[webLog] insert fail", logger.String("err", err.Error()), pkgCtxUtils.CtxTraceIDField(ctx))
logger.Error("[webLog] insert fail", logger.String("err", err.Error()), ctxUtils.CtxTraceIDField(ctx))
for _, or := range ors {
logger.Error("[webLog] insert fail detail", logger.String("user_id", or.UserID.String()), logger.String("operate", or.Operate), pkgCtxUtils.CtxTraceIDField(ctx))
logger.Error("[webLog] insert fail detail", logger.String("user_id", or.UserID.String()), logger.String("operate", or.Operate), ctxUtils.CtxTraceIDField(ctx))
}
}
}()
......@@ -127,7 +119,8 @@ func Init(schema string, isProd bool) {
func AddAsync(ctx context.Context, traceNo, operate string, req *entity.CopyHttpReq, rsp *entity.CopyHttpRsp) {
op := &WebLog{}
op.ID = xsf.GenerateID()
op.UserID = ctxUtils.GetCtxUserID(ctx)
op.MerchantID = ctxUtils.GetCtxCompanyID(ctx)
op.UserID = ctxUtils.GetCtxUID(ctx)
op.TraceID = ctxUtils.GetCtxTid(ctx)
op.Operate = operate
op.CreatedAt = xtime.Now()
......@@ -152,36 +145,8 @@ func (x *webLogDao) CreateSliceSilent(ctx context.Context, ors []*WebLog) error
return x.db.WithContext(ctx).Session(&gorm.Session{Logger: glogger.Discard}).Create(ors).Error
}
type User_UserType_Enum int8
var (
User_UserType_Operator User_UserType_Enum = 2 // 运营商
User_UserType_Share User_UserType_Enum = 3 // 合伙人
User_UserType_Agent User_UserType_Enum = 9 // 代理商
)
func (x User_UserType_Enum) Uint() uint {
return uint(x)
}
func (x User_UserType_Enum) Int() int {
return int(x)
}
func (x User_UserType_Enum) Desc() string {
desc := "未知"
switch x {
case User_UserType_Operator:
desc = "运营商"
case User_UserType_Share:
desc = "合伙人"
case User_UserType_Agent:
desc = "代理商"
}
return desc
}
func getUserType(ctx context.Context) int {
val := ctx.Value(pkgCtxUtils.KeyUType)
func getUserType(c *gin.Context) int {
val := c.Value(ctxUtils.KeyUType)
if val != nil {
return cast.ToInt(val)
}
......@@ -189,13 +154,14 @@ func getUserType(ctx context.Context) int {
}
type WebLog struct {
ID xsf.ID `json:"id" form:"id" swaggertype:"string" gorm:"column:id;type:bigint(20) unsigned;primaryKey;autoIncrement;comment:主键" example:"101"` //主键
UserID xsf.ID `json:"userID" form:"userID" swaggertype:"string" gorm:"column:user_id;type:bigint(20) unsigned;comment:用户ID" example:"102"` //用户ID
Operate string `json:"operate" form:"operate" gorm:"column:operate;type:varchar(128);comment:操作" example:"用户登出"` //操作
TraceID string `json:"traceID" form:"traceID" gorm:"column:trace_id;type:varchar(64);comment:溯源id" example:"TRACE_ID12345"` //溯源id
Req *string `json:"req" form:"req" gorm:"column:req;type:json;comment:请求" example:""` //请求
Rsp *string `json:"rsp" form:"rsp" gorm:"column:rsp;type:json;comment:响应" example:""` //响应
CreatedAt xtime.DateTime `json:"createdAt" form:"createdAt" gorm:"column:created_at;type:datetime;comment:创建时间" example:"2025-01-07 12:20:43"` //创建时间
ID xsf.ID `json:"id" form:"id" swaggertype:"string" gorm:"column:id;type:bigint(20) unsigned;primaryKey;autoIncrement;comment:主键" example:"1000"` //主键
MerchantID xsf.ID `json:"merchantID" form:"merchantID" swaggertype:"string" gorm:"column:merchant_id;type:bigint(20) unsigned;comment:商户ID" example:"1008"` //商户ID
UserID xsf.ID `json:"userID" form:"userID" swaggertype:"string" gorm:"column:user_id;type:bigint(20) unsigned;comment:用户ID" example:"2008"` //用户ID
Operate string `json:"operate" form:"operate" gorm:"column:operate;type:varchar(128);comment:操作" example:"用户登出"` //操作
TraceID string `json:"traceID" form:"traceID" gorm:"column:trace_id;type:varchar(64);comment:溯源id" example:"TRACE_ID12345"` //溯源id
Req *string `json:"req" form:"req" gorm:"column:req;type:json;comment:请求" example:""` //请求
Rsp *string `json:"rsp" form:"rsp" gorm:"column:rsp;type:json;comment:响应" example:""` //响应
CreatedAt xtime.DateTime `json:"createdAt" form:"createdAt" gorm:"column:created_at;type:datetime;comment:创建时间" example:"2025-01-07 12:20:43"` //创建时间
}
func (x WebLog) GetID() xsf.ID {
......
DROP TABLE if EXISTS `web_log`;
CREATE TABLE `web_log` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID eg[10001]',
`company_id` bigint(20) unsigned DEFAULT NULL COMMENT '公司ID eg[20001]',
`user_id` bigint(20) unsigned DEFAULT NULL COMMENT '用户ID eg[30001]',
`operate` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL DEFAULT '' COMMENT '操作 eg[用户登出]',
`trace_id` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL DEFAULT '' COMMENT '溯源id eg[TRACE_ID12345]',
`req` json DEFAULT NULL COMMENT '请求 eg[]',
`rsp` json DEFAULT NULL COMMENT '响应 eg[]',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间 eg[2025-01-07 12:20:43]',
PRIMARY KEY (`id`),
KEY `idx_uid_ct` (`user_id`, `created_at`)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = DYNAMIC COMMENT = '操作记录';
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论