Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
b1567cdc
提交
b1567cdc
authored
7月 04, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
hotfix
上级
7f5f8a24
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
28 行增加
和
32 行删除
+28
-32
ctx.go
gin/ctxUtils/ctx.go
+1
-0
response.go
gin/response/response.go
+5
-0
httpcli.go
httpcli/httpcli.go
+22
-32
没有找到文件。
gin/ctxUtils/ctx.go
浏览文件 @
b1567cdc
...
...
@@ -19,6 +19,7 @@ const (
HeaderXTimestampKey
=
"Timestamp"
KeyReqBody
=
"reqBody"
KeyRspBody
=
"rspBody"
KeyRspCode
=
"rspCode"
KeyOApiReqBody
=
"oApiReqBody%s"
KeyOApiRspBody
=
"oApiRspBody%s"
...
...
gin/response/response.go
浏览文件 @
b1567cdc
...
...
@@ -158,11 +158,13 @@ func respJSONWith200(c *gin.Context, code int, msg string, data ...interface{})
// Success return success
func
Success
(
c
*
gin
.
Context
,
data
...
interface
{})
{
c
.
Set
(
ctxUtils
.
KeyRspCode
,
1
)
respJSONWith200
(
c
,
errcode
.
Success
.
Code
(),
errcode
.
Success
.
Msg
(),
data
...
)
}
// SuccessWithPage return success
func
SuccessWithPage
[
T
any
](
c
*
gin
.
Context
,
list
[]
*
T
,
total
int64
)
{
c
.
Set
(
ctxUtils
.
KeyRspCode
,
1
)
if
list
==
nil
{
list
=
[]
*
T
{}
}
...
...
@@ -171,6 +173,7 @@ func SuccessWithPage[T any](c *gin.Context, list []*T, total int64) {
// SuccessWithList return success
func
SuccessWithList
[
T
any
](
c
*
gin
.
Context
,
list
[]
*
T
)
{
c
.
Set
(
ctxUtils
.
KeyRspCode
,
1
)
if
list
==
nil
{
list
=
[]
*
T
{}
}
...
...
@@ -179,6 +182,7 @@ func SuccessWithList[T any](c *gin.Context, list []*T) {
// ErrorE return error
func
ErrorE
(
c
*
gin
.
Context
,
err
*
errcode
.
Error
,
data
...
interface
{})
{
c
.
Set
(
ctxUtils
.
KeyRspCode
,
0
)
respJSONWith200
(
c
,
err
.
Code
(),
err
.
Msg
(),
data
...
)
}
...
...
@@ -190,6 +194,7 @@ func ErrorE(c *gin.Context, err *errcode.Error, data ...interface{}) {
// - msg: string,错误消息。
// - data: ...interface{},可选的附加数据。
func
Error
(
c
*
gin
.
Context
,
err
error
)
{
c
.
Set
(
ctxUtils
.
KeyRspCode
,
0
)
// 如果原始错误不为空,则记录错误日志。
msg
:=
""
if
err
!=
nil
{
...
...
httpcli/httpcli.go
浏览文件 @
b1567cdc
...
...
@@ -259,37 +259,6 @@ func (req *Request) push(ctx context.Context) (*Response, error) {
return
req
.
send
(
ctx
,
buf
,
buf
)
}
func
(
req
*
Request
)
ReqString
(
ctx
context
.
Context
)
string
{
request
:=
make
(
map
[
string
]
any
)
request
[
"method"
]
=
req
.
request
.
Method
request
[
"url"
]
=
req
.
request
.
URL
.
String
()
request
[
"headers"
]
=
req
.
request
.
Header
if
req
.
request
.
Method
==
http
.
MethodPost
||
req
.
request
.
Method
==
http
.
MethodPut
||
req
.
request
.
Method
==
http
.
MethodPatch
||
req
.
request
.
Method
==
http
.
MethodDelete
{
if
req
.
request
.
Body
!=
nil
{
buf
:=
bytes
.
Buffer
{}
n
,
_
:=
buf
.
ReadFrom
(
req
.
request
.
Body
)
request
[
"size"
]
=
buf
.
Len
()
if
n
>
0
{
request
[
"body"
]
=
getRequestBody
(
&
buf
,
256
)
}
}
}
return
xjson
.
ToJsonString
(
request
)
}
func
getRequestBody
(
buf
*
bytes
.
Buffer
,
maxLen
int
)
[]
byte
{
l
:=
buf
.
Len
()
if
l
==
0
{
return
[]
byte
(
""
)
}
else
if
l
<
maxLen
{
return
buf
.
Bytes
()
}
body
:=
make
([]
byte
,
maxLen
)
copy
(
body
,
buf
.
Bytes
())
return
append
(
body
[
:
maxLen
-
len
(
contentMark
)],
contentMark
...
)
}
func
(
req
*
Request
)
send
(
ctx
context
.
Context
,
body
io
.
Reader
,
buf
*
bytes
.
Buffer
)
(
*
Response
,
error
)
{
req
.
request
,
req
.
err
=
http
.
NewRequest
(
req
.
method
,
req
.
url
,
body
)
if
req
.
err
!=
nil
{
...
...
@@ -341,7 +310,7 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
defer
response
.
Body
.
Close
()
body
,
err
:=
io
.
ReadAll
(
response
.
Body
)
if
err
!=
nil
{
return
xerror
.
New
(
err
.
Error
()
)
return
xerror
.
Wrap
(
err
,
"read response body"
)
}
bodyBuf
:=
bytes
.
NewBuffer
(
body
)
if
c
,
_
:=
ctxUtils
.
GetGinCtx
(
ctx
);
c
!=
nil
{
...
...
@@ -413,6 +382,27 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
return
resp
,
resp
.
err
}
func
copyResponse
(
resp
*
Response
,
response
*
http
.
Response
)
{
if
resp
==
nil
||
response
==
nil
{
return
}
resp
.
Status
=
response
.
Status
resp
.
StatusCode
=
response
.
StatusCode
for
k
,
v
:=
range
response
.
Header
{
resp
.
Header
[
k
]
=
append
(
resp
.
Header
[
k
],
v
...
)
}
if
response
.
Body
!=
nil
{
defer
response
.
Body
.
Close
()
body
,
err
:=
io
.
ReadAll
(
response
.
Body
)
if
err
!=
nil
{
resp
.
err
=
xerror
.
Wrap
(
err
,
"read response body"
)
}
if
len
(
body
)
>
0
{
resp
.
Body
=
io
.
NopCloser
(
bytes
.
NewBuffer
(
body
))
}
}
}
// Response return response
func
(
req
*
Request
)
Response
()
(
*
Response
,
error
)
{
if
req
.
err
!=
nil
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论