Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
fe385d07
提交
fe385d07
authored
9月 05, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
4ec77d7e
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
108 行增加
和
82 行删除
+108
-82
ctxUtils.go
utils/ctxUtils/ctxUtils.go
+71
-26
webLogUtils.go
utils/webLogUtils/webLogUtils.go
+22
-56
weblog.sql
utils/webLogUtils/weblog.sql
+15
-0
没有找到文件。
utils/ctxUtils/ctxUtils.go
浏览文件 @
fe385d07
...
...
@@ -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
{
...
...
utils/webLogUtils/webLogUtils.go
浏览文件 @
fe385d07
...
...
@@ -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
:=
pkgC
txUtils
.
WrapCtx
(
c
)
ctx
:=
c
txUtils
.
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
(
pkgC
txUtils
.
KeyRspCode
)
respCode
=
c
.
GetInt
(
c
txUtils
.
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
(),
pkgC
txUtils
.
ContextTraceIDKey
,
xsf
.
GenerateID
()
.
String
())
ctx
:=
context
.
WithValue
(
context
.
Background
(),
c
txUtils
.
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
()),
pkgC
txUtils
.
CtxTraceIDField
(
ctx
))
logger
.
Error
(
"[webLog] insert fail"
,
logger
.
String
(
"err"
,
err
.
Error
()),
c
txUtils
.
CtxTraceIDField
(
ctx
))
for
_
,
or
:=
range
ors
{
logger
.
Error
(
"[webLog] insert fail detail"
,
logger
.
String
(
"user_id"
,
or
.
UserID
.
String
()),
logger
.
String
(
"operate"
,
or
.
Operate
),
pkgC
txUtils
.
CtxTraceIDField
(
ctx
))
logger
.
Error
(
"[webLog] insert fail detail"
,
logger
.
String
(
"user_id"
,
or
.
UserID
.
String
()),
logger
.
String
(
"operate"
,
or
.
Operate
),
c
txUtils
.
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
{
...
...
utils/webLogUtils/weblog.sql
0 → 100644
浏览文件 @
fe385d07
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论