Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
62016f9a
提交
62016f9a
authored
5月 15, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
c0316c80
隐藏空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
206 行增加
和
80 行删除
+206
-80
response.go
gin/response/response.go
+15
-9
ctx.go
gin/xctx/ctx.go
+9
-9
httpcli.go
httpcli/httpcli.go
+8
-4
xsf.go
utils/xsf/xsf.go
+104
-0
xsf_test.go
utils/xsf/xsf_test.go
+35
-0
xslice.go
utils/xslice/xslice.go
+21
-20
xsf.go
utils/xsnowflake/xsf.go
+0
-25
ws.go
ws/ws.go
+14
-13
没有找到文件。
gin/response/response.go
浏览文件 @
62016f9a
...
...
@@ -5,10 +5,12 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/errcode"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/xctx"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin/xctx"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
...
...
@@ -18,15 +20,19 @@ const CustomErrorCode = 0
// Result output data format
type
Result
struct
{
Code
int
`json:"code"`
Msg
string
`json:"message"`
Data
interface
{}
`json:"data"`
Code
int
`json:"code" example:"1"`
Msg
string
`json:"message" example:"OK"`
Data
interface
{}
`json:"data"`
TimeStamp
int64
`json:"timestamp" example:"1718850053"`
TraceID
string
`json:"traceID" example:"7f0000016673920576f0659c620086b0"`
}
func
newResp
(
code
int
,
msg
string
,
data
interface
{})
*
Result
{
func
newResp
(
c
*
gin
.
Context
,
c
ode
int
,
msg
string
,
data
interface
{})
*
Result
{
resp
:=
&
Result
{
Code
:
code
,
Msg
:
msg
,
Code
:
code
,
Msg
:
msg
,
TimeStamp
:
time
.
Now
()
.
Unix
(),
TraceID
:
xctx
.
GetGinCtxTid
(
c
),
}
// ensure that the data field is not nil on return, note that it is not nil when resp.data=[]interface {}, it is serialized to null
...
...
@@ -64,7 +70,7 @@ func respJSONWithStatusCode(c *gin.Context, code int, msg string, data ...interf
if
len
(
data
)
>
0
{
firstData
=
data
[
0
]
}
resp
:=
newResp
(
code
,
msg
,
firstData
)
resp
:=
newResp
(
c
,
c
ode
,
msg
,
firstData
)
writeJSON
(
c
,
code
,
resp
)
}
...
...
@@ -131,10 +137,10 @@ func Out(c *gin.Context, err *errcode.Error, data ...interface{}) {
// status code flat 200, custom error codes in data.code
func
respJSONWith200
(
c
*
gin
.
Context
,
code
int
,
msg
string
,
data
...
interface
{})
{
if
len
(
data
)
>
0
{
writeJSON
(
c
,
http
.
StatusOK
,
newResp
(
code
,
msg
,
data
[
0
]))
writeJSON
(
c
,
http
.
StatusOK
,
newResp
(
c
,
c
ode
,
msg
,
data
[
0
]))
return
}
resp
:=
newResp
(
code
,
msg
,
nil
)
resp
:=
newResp
(
c
,
c
ode
,
msg
,
nil
)
writeJSON
(
c
,
http
.
StatusOK
,
resp
)
}
...
...
gin/xctx/ctx.go
浏览文件 @
62016f9a
...
...
@@ -3,7 +3,7 @@ package xctx
import
(
"context"
xsf
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xs
nowflake
"
xsf
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xs
f
"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
...
...
@@ -75,34 +75,34 @@ func CtxTraceIDField(c context.Context) zap.Field {
return
zap
.
String
(
ContextRequestIDKey
,
CtxRequestID
(
c
))
}
func
GetGinUserID
(
c
*
gin
.
Context
)
uint64
{
func
GetGinUserID
(
c
*
gin
.
Context
)
xsf
.
ID
{
uid
,
exists
:=
c
.
Get
(
KeyUID
)
if
exists
{
return
cast
.
ToUint64
(
uid
)
return
xsf
.
ParseInt64
(
cast
.
ToInt64
(
uid
)
)
}
return
0
}
func
GetGinShopID
(
c
*
gin
.
Context
)
uint64
{
func
GetGinShopID
(
c
*
gin
.
Context
)
xsf
.
ID
{
uid
,
exists
:=
c
.
Get
(
KeyShopID
)
if
exists
{
return
cast
.
ToUint64
(
uid
)
return
xsf
.
ParseInt64
(
cast
.
ToInt64
(
uid
)
)
}
return
0
}
func
GetCtxUserID
(
c
context
.
Context
)
uint64
{
func
GetCtxUserID
(
c
context
.
Context
)
xsf
.
ID
{
uid
:=
c
.
Value
(
KeyUID
)
if
uid
!=
nil
{
return
cast
.
ToUint64
(
uid
)
return
xsf
.
ParseInt64
(
cast
.
ToInt64
(
uid
)
)
}
return
0
}
func
GetCtxShopID
(
c
context
.
Context
)
uint64
{
func
GetCtxShopID
(
c
context
.
Context
)
xsf
.
ID
{
uid
:=
c
.
Value
(
KeyShopID
)
if
uid
!=
nil
{
return
cast
.
ToUint64
(
uid
)
return
xsf
.
ParseInt64
(
cast
.
ToInt64
(
uid
)
)
}
return
0
}
...
...
httpcli/httpcli.go
浏览文件 @
62016f9a
...
...
@@ -7,16 +7,17 @@ import (
"encoding/json"
"errors"
"fmt"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin/xctx"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xjson"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
"io"
"net/http"
"net/url"
"strings"
"time"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin/xctx"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xjson"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
"github.com/duke-git/lancet/v2/retry"
)
...
...
@@ -315,6 +316,9 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
logger
.
Info
(
"http call req"
,
logger
.
String
(
"req"
,
req
.
ReqString
(
ctx
)),
ctxUtil
.
CtxTraceIDField
(
ctx
))
var
response
*
http
.
Response
response
,
resp
.
err
=
client
.
Do
(
req
.
request
)
if
resp
.
err
!=
nil
{
return
xerror
.
New
(
resp
.
err
.
Error
())
}
resp
.
Status
=
response
.
Status
resp
.
StatusCode
=
response
.
StatusCode
for
k
,
v
:=
range
response
.
Header
{
...
...
utils/xsf/xsf.go
0 → 100644
浏览文件 @
62016f9a
package
xsf
import
(
"fmt"
"strconv"
"github.com/bwmarrin/snowflake"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
)
var
node
*
snowflake
.
Node
type
ID
snowflake
.
ID
func
(
f
ID
)
MarshalJSON
()
([]
byte
,
error
)
{
buff
:=
make
([]
byte
,
0
,
22
)
buff
=
append
(
buff
,
'"'
)
buff
=
strconv
.
AppendInt
(
buff
,
int64
(
f
),
10
)
buff
=
append
(
buff
,
'"'
)
return
buff
,
nil
}
func
(
f
*
ID
)
UnmarshalJSON
(
b
[]
byte
)
error
{
if
len
(
b
)
==
0
{
return
nil
}
if
len
(
b
)
==
1
{
i
,
err
:=
strconv
.
ParseInt
(
string
(
b
),
10
,
64
)
if
err
!=
nil
{
return
err
}
*
f
=
ID
(
i
)
return
nil
}
if
len
(
b
)
==
2
{
if
b
[
0
]
==
'"'
&&
b
[
1
]
==
'"'
{
*
f
=
ID
(
0
)
}
i
,
err
:=
strconv
.
ParseInt
(
string
(
b
),
10
,
64
)
if
err
!=
nil
{
return
err
}
*
f
=
ID
(
i
)
return
nil
}
if
b
[
0
]
==
'"'
&&
b
[
len
(
b
)
-
1
]
==
'"'
{
i
,
err
:=
strconv
.
ParseInt
(
string
(
b
[
1
:
len
(
b
)
-
1
]),
10
,
64
)
if
err
!=
nil
{
return
err
}
*
f
=
ID
(
i
)
return
nil
}
i
,
err
:=
strconv
.
ParseInt
(
string
(
b
),
10
,
64
)
if
err
!=
nil
{
return
err
}
*
f
=
ID
(
i
)
return
nil
}
type
JSONSyntaxError
struct
{
original
[]
byte
}
func
(
j
JSONSyntaxError
)
Error
()
string
{
return
fmt
.
Sprintf
(
"invalid xsf ID %q"
,
string
(
j
.
original
))
}
// n [1,1023]
func
Init
(
n
int64
)
(
err
error
)
{
node
,
err
=
snowflake
.
NewNode
(
n
)
if
err
!=
nil
{
return
err
}
return
nil
}
func
Generate
()
ID
{
return
ID
(
node
.
Generate
())
}
func
(
x
ID
)
String
()
string
{
return
snowflake
.
ID
(
x
)
.
String
()
}
func
(
x
ID
)
ToInt64
()
int64
{
return
snowflake
.
ID
(
x
)
.
Int64
()
}
func
GenerateID
()
ID
{
sid
:=
Generate
()
return
ID
(
sid
)
}
func
ParseInt64
(
val
int64
)
ID
{
return
ID
(
snowflake
.
ParseInt64
(
val
))
}
func
ParseString
(
val
string
)
(
ID
,
error
)
{
v
,
err
:=
snowflake
.
ParseString
(
val
)
if
err
!=
nil
{
return
0
,
xerror
.
New
(
err
.
Error
())
}
return
ID
(
v
),
nil
}
utils/xsf/xsf_test.go
0 → 100644
浏览文件 @
62016f9a
package
xsf
import
(
"reflect"
"testing"
)
func
TestID_MarshalJSON
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
f
ID
want
[]
byte
wantErr
bool
}{
// TODO: Add test cases.
{
name
:
"test1"
,
f
:
ID
(
1002
),
want
:
[]
byte
(
`"1002"`
),
wantErr
:
false
,
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
got
,
err
:=
tt
.
f
.
MarshalJSON
()
if
(
err
!=
nil
)
!=
tt
.
wantErr
{
t
.
Errorf
(
"ID.MarshalJSON() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
return
}
if
!
reflect
.
DeepEqual
(
got
,
tt
.
want
)
{
t
.
Errorf
(
"ID.MarshalJSON() = %v, want %v"
,
got
,
tt
.
want
)
}
})
}
}
utils/xslice/xslice.go
浏览文件 @
62016f9a
...
...
@@ -2,12 +2,13 @@ package xslice
import
(
"fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
"gitlab.wanzhuangkj.com/tush/xpkg/xset"
"strings"
"github.com/jinzhu/copier"
"github.com/spf13/cast"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
"gitlab.wanzhuangkj.com/tush/xpkg/xset"
)
// connector 连接符
...
...
@@ -48,31 +49,31 @@ func StrToUint64(rs []string) []uint64 {
}
type
IID
interface
{
GetID
()
uint64
GetID
()
xsf
.
ID
}
type
IIDTable
interface
{
GetID
()
uint64
GetID
()
xsf
.
ID
TableName
()
string
}
type
ITable
interface
{
TableName
()
uint64
TableName
()
string
}
func
GetIDs
[
T
IID
](
rs
[]
*
T
)
[]
uint64
{
func
GetIDs
[
T
IID
](
rs
[]
*
T
)
[]
xsf
.
ID
{
if
len
(
rs
)
==
0
{
return
nil
}
set
:=
xset
.
NewSet
[
uint64
]()
set
:=
xset
.
NewSet
[
xsf
.
ID
]()
for
_
,
r
:=
range
rs
{
set
.
Add
((
*
r
)
.
GetID
())
}
return
set
.
ToList
()
}
func
SliceToIDMap
[
T
IID
](
rs
[]
*
T
)
map
[
uint64
]
*
T
{
m
:=
make
(
map
[
uint64
]
*
T
)
func
SliceToIDMap
[
T
IID
](
rs
[]
*
T
)
map
[
xsf
.
ID
]
*
T
{
m
:=
make
(
map
[
xsf
.
ID
]
*
T
)
for
_
,
r
:=
range
rs
{
m
[(
*
r
)
.
GetID
()]
=
r
}
...
...
@@ -169,15 +170,15 @@ func StrMapToSlice[T any](m map[string]*T) []*T {
return
rs
}
func
GetStrMapIDs
[
T
IID
](
m
map
[
string
]
*
T
)
[]
uint64
{
st
:=
xset
.
NewSet
[
uint64
]()
func
GetStrMapIDs
[
T
IID
](
m
map
[
string
]
*
T
)
[]
xsf
.
ID
{
st
:=
xset
.
NewSet
[
xsf
.
ID
]()
for
_
,
v
:=
range
m
{
st
.
Add
((
*
v
)
.
GetID
())
}
return
st
.
ToList
()
}
func
IDMapToSlice
[
T
any
](
m
map
[
uint64
]
*
T
)
[]
*
T
{
func
IDMapToSlice
[
T
any
](
m
map
[
xsf
.
ID
]
*
T
)
[]
*
T
{
var
rs
[]
*
T
for
_
,
r
:=
range
m
{
rs
=
append
(
rs
,
r
)
...
...
@@ -185,8 +186,8 @@ func IDMapToSlice[T any](m map[uint64]*T) []*T {
return
rs
}
func
GetIDMapIDs
[
T
IID
](
m
map
[
uint64
]
*
T
)
[]
uint64
{
st
:=
xset
.
NewSet
[
uint64
]()
func
GetIDMapIDs
[
T
IID
](
m
map
[
xsf
.
ID
]
*
T
)
[]
xsf
.
ID
{
st
:=
xset
.
NewSet
[
xsf
.
ID
]()
for
_
,
v
:=
range
m
{
st
.
Add
((
*
v
)
.
GetID
())
}
...
...
@@ -201,8 +202,8 @@ func StrMapSliceToSlice[T any](m map[string][]*T) []*T {
return
rs
}
func
GetStrMapSliceIDs
[
T
IID
](
m
map
[
string
][]
*
T
)
[]
uint64
{
st
:=
xset
.
NewSet
[
uint64
]()
func
GetStrMapSliceIDs
[
T
IID
](
m
map
[
string
][]
*
T
)
[]
xsf
.
ID
{
st
:=
xset
.
NewSet
[
xsf
.
ID
]()
for
_
,
rs
:=
range
m
{
for
_
,
r
:=
range
rs
{
st
.
Add
((
*
r
)
.
GetID
())
...
...
@@ -211,7 +212,7 @@ func GetStrMapSliceIDs[T IID](m map[string][]*T) []uint64 {
return
st
.
ToList
()
}
func
IDMapSliceToSlice
[
T
any
](
m
map
[
uint64
][]
*
T
)
[]
*
T
{
func
IDMapSliceToSlice
[
T
any
](
m
map
[
xsf
.
ID
][]
*
T
)
[]
*
T
{
var
rs
[]
*
T
for
_
,
r
:=
range
m
{
rs
=
append
(
rs
,
r
...
)
...
...
@@ -219,8 +220,8 @@ func IDMapSliceToSlice[T any](m map[uint64][]*T) []*T {
return
rs
}
func
GetIDMapSliceIDs
[
T
IID
](
m
map
[
uint64
][]
*
T
)
[]
uint64
{
st
:=
xset
.
NewSet
[
uint64
]()
func
GetIDMapSliceIDs
[
T
IID
](
m
map
[
xsf
.
ID
][]
*
T
)
[]
xsf
.
ID
{
st
:=
xset
.
NewSet
[
xsf
.
ID
]()
for
_
,
rs
:=
range
m
{
for
_
,
r
:=
range
rs
{
st
.
Add
((
*
r
)
.
GetID
())
...
...
@@ -229,7 +230,7 @@ func GetIDMapSliceIDs[T IID](m map[uint64][]*T) []uint64 {
return
st
.
ToList
()
}
func
CompareSlice
[
T
IIDTable
,
BizSlice
~
[]
*
T
](
ids
[]
uint64
,
bizSlice
BizSlice
,
errCode
int
)
error
{
func
CompareSlice
[
T
IIDTable
,
BizSlice
~
[]
*
T
](
ids
[]
xsf
.
ID
,
bizSlice
BizSlice
,
errCode
int
)
error
{
if
len
(
ids
)
==
0
{
return
nil
}
...
...
utils/xsnowflake/xsf.go
deleted
100644 → 0
浏览文件 @
c0316c80
package
xsf
import
(
"github.com/bwmarrin/snowflake"
)
var
node
*
snowflake
.
Node
// n [1,1023]
func
Init
(
n
int64
)
(
err
error
)
{
node
,
err
=
snowflake
.
NewNode
(
n
)
if
err
!=
nil
{
return
err
}
return
nil
}
func
Generate
()
snowflake
.
ID
{
return
node
.
Generate
()
}
func
GenerateID
()
uint64
{
sid
:=
Generate
()
return
uint64
(
sid
.
Int64
())
}
ws/ws.go
浏览文件 @
62016f9a
...
...
@@ -3,17 +3,18 @@ package ws
import
(
"context"
"encoding/json"
"net/http"
"sync"
"time"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin/xctx"
"gitlab.wanzhuangkj.com/tush/xpkg/jwt"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/spf13/cast"
"go.uber.org/zap"
)
...
...
@@ -27,7 +28,7 @@ const (
type
wsConn
struct
{
manager
*
WsConnManager
id
uint64
id
xsf
.
ID
conn
*
websocket
.
Conn
sendBuff
chan
[]
byte
log
*
zap
.
Logger
...
...
@@ -35,7 +36,7 @@ type wsConn struct {
}
type
WsConnManager
struct
{
pool
map
[
uint64
]
*
wsConn
pool
map
[
xsf
.
ID
]
*
wsConn
upgrader
*
websocket
.
Upgrader
sync
.
RWMutex
log
*
zap
.
Logger
...
...
@@ -47,14 +48,14 @@ var (
)
type
Hook
interface
{
AfterSend
(
ctx
context
.
Context
,
receiverID
uint64
,
payload
*
WsMessage
)
error
AfterAccept
(
ctx
context
.
Context
,
userID
uint64
)
error
AfterUnregister
(
ctx
context
.
Context
,
userID
uint64
)
error
AfterSend
(
ctx
context
.
Context
,
receiverID
xsf
.
ID
,
payload
*
WsMessage
)
error
AfterAccept
(
ctx
context
.
Context
,
userID
xsf
.
ID
)
error
AfterUnregister
(
ctx
context
.
Context
,
userID
xsf
.
ID
)
error
}
func
NewWsConnManager
(
logger
*
zap
.
Logger
,
upgrader
*
websocket
.
Upgrader
,
hooks
...
Hook
)
*
WsConnManager
{
return
&
WsConnManager
{
pool
:
make
(
map
[
uint64
]
*
wsConn
),
pool
:
make
(
map
[
xsf
.
ID
]
*
wsConn
),
log
:
logger
,
upgrader
:
upgrader
,
hooks
:
hooks
}
}
...
...
@@ -107,7 +108,7 @@ func (m *WsConnManager) accept(c *gin.Context) error {
return
xerror
.
Wrap
(
err
,
"parse jwt"
)
}
userIDStr
:=
claims
.
UID
userID
:=
cast
.
ToUint64
(
userIDStr
)
userID
,
_
:=
xsf
.
ParseString
(
userIDStr
)
conn
,
err
:=
m
.
upgrader
.
Upgrade
(
w
,
r
,
nil
)
if
err
!=
nil
{
return
xerror
.
Wrap
(
err
,
"ws upgrade failed"
)
...
...
@@ -136,14 +137,14 @@ func (m *WsConnManager) accept(c *gin.Context) error {
return
nil
}
func
(
m
*
WsConnManager
)
getConn
(
userID
uint64
)
(
*
wsConn
,
bool
)
{
func
(
m
*
WsConnManager
)
getConn
(
userID
xsf
.
ID
)
(
*
wsConn
,
bool
)
{
m
.
RLock
()
defer
m
.
RUnlock
()
client
,
exists
:=
m
.
pool
[
userID
]
return
client
,
exists
}
func
(
m
*
WsConnManager
)
Send
(
ctx
context
.
Context
,
receiverID
uint64
,
payload
*
WsMessage
)
error
{
func
(
m
*
WsConnManager
)
Send
(
ctx
context
.
Context
,
receiverID
xsf
.
ID
,
payload
*
WsMessage
)
error
{
if
payload
==
nil
{
return
nil
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论