Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录
切换导航
L
library
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
stbz
library
Commits
79ed5e03
提交
79ed5e03
authored
12月 08, 2022
作者:
zhanglibo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
token redis
上级
37d326a6
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
24 行增加
和
9 行删除
+24
-9
dwd_token.go
upstream/dwd/dwd_token.go
+11
-6
jingdong_token.go
upstream/jingdong/jingdong_token.go
+13
-3
没有找到文件。
upstream/dwd/dwd_token.go
浏览文件 @
79ed5e03
...
...
@@ -48,14 +48,19 @@ func (s *tokenDwd) Get(ctx context.Context) (res *TokenGetRes, err error) {
}
func
(
s
*
tokenDwd
)
Access
(
ctx
context
.
Context
)
(
res
string
,
err
error
)
{
cache
,
_
:=
g
.
Redis
()
.
DoVar
(
"HGETALL"
,
CacheKey
)
conn
:=
g
.
Redis
()
.
Ctx
(
ctx
)
.
Conn
()
defer
func
()
{
_
=
conn
.
Close
()
}()
_
,
_
=
conn
.
DoVar
(
"SELECT"
,
10
)
cache
,
_
:=
conn
.
DoVar
(
"HGETALL"
,
CacheKey
)
if
!
cache
.
IsEmpty
()
{
_
=
cache
.
Scan
(
&
s
)
if
s
.
ExpiresIn
>
gtime
.
Timestamp
()
{
res
=
s
.
AccessToken
return
}
_
,
err
=
g
.
Redis
()
.
DoVar
(
"DEL"
,
CacheKey
)
_
,
err
=
conn
.
DoVar
(
"DEL"
,
CacheKey
)
}
result
,
err
:=
s
.
Get
(
ctx
)
if
err
!=
nil
{
...
...
@@ -68,12 +73,12 @@ func (s *tokenDwd) Access(ctx context.Context) (res string, err error) {
_
=
gjson
.
New
(
result
.
Data
)
.
Scan
(
&
s
)
s
.
ExpiresIn
+=
gtime
.
Timestamp
()
res
=
s
.
AccessToken
_
,
_
=
g
.
Redis
()
.
Do
(
"HMSET"
,
append
(
g
.
Slice
{
CacheKey
},
gutil
.
MapToSlice
(
gconv
.
Map
(
s
))
...
)
...
)
_
,
_
=
conn
.
Do
(
"HMSET"
,
append
(
g
.
Slice
{
CacheKey
},
gutil
.
MapToSlice
(
gconv
.
Map
(
s
))
...
)
...
)
return
}
func
(
s
*
tokenDwd
)
post
(
ctx
context
.
Context
,
req
interface
{})
(
res
string
,
err
error
)
{
//
Start := gtime.TimestampMilli()
Start
:=
gtime
.
TimestampMilli
()
var
request
=
gjson
.
New
(
req
)
_
=
request
.
Set
(
"signature"
,
sign
(
req
))
...
...
@@ -85,9 +90,9 @@ func (s *tokenDwd) post(ctx context.Context, req interface{}) (res string, err e
ctx
=
context
.
WithValue
(
ctx
,
"Method"
,
"POST"
)
ctx
=
context
.
WithValue
(
ctx
,
"URI"
,
"token"
)
if
err
!=
nil
{
//
g.Log().Ctx(ctx).Infof("参数【%v】错误【%v】响应时间【%v】", request.MustToJsonString(), err.Error(), gtime.TimestampMilli()-Start)
g
.
Log
()
.
Ctx
(
ctx
)
.
Infof
(
"参数【%v】错误【%v】响应时间【%v】"
,
request
.
MustToJsonString
(),
err
.
Error
(),
gtime
.
TimestampMilli
()
-
Start
)
}
else
{
//
g.Log().Ctx(ctx).Cat(PkgName).Infof("参数【%v】响应【%v】响应时间【%v】", request.MustToJsonString(), res, gtime.TimestampMilli()-Start)
g
.
Log
()
.
Ctx
(
ctx
)
.
Cat
(
PkgName
)
.
Infof
(
"参数【%v】响应【%v】响应时间【%v】"
,
request
.
MustToJsonString
(),
res
,
gtime
.
TimestampMilli
()
-
Start
)
}
}()
res
=
resp
.
ReadAllString
()
...
...
upstream/jingdong/jingdong_token.go
浏览文件 @
79ed5e03
...
...
@@ -33,7 +33,12 @@ type accessToken struct {
//getAccessToken 获取token
func
(
s
*
client
)
getAccessToken
(
ctx
context
.
Context
)
(
err
error
)
{
var
token
*
accessToken
cache
,
_
:=
g
.
Redis
()
.
DoVar
(
"HGETALL"
,
CacheKey
)
conn
:=
g
.
Redis
()
.
Ctx
(
ctx
)
.
Conn
()
defer
func
()
{
_
=
conn
.
Close
()
}()
_
,
_
=
conn
.
DoVar
(
"SELECT"
,
10
)
cache
,
_
:=
conn
.
DoVar
(
"HGETALL"
,
CacheKey
)
if
!
cache
.
IsEmpty
()
{
_
=
cache
.
Scan
(
&
token
)
if
token
.
Time
+
token
.
ExpiresIn
*
1000
>
gtime
.
TimestampMilli
()
{
...
...
@@ -69,7 +74,7 @@ func (s *client) getAccessToken(ctx context.Context) (err error) {
err
=
errors
.
New
(
jdRes
.
ResultMessage
)
return
}
_
,
_
=
g
.
Redis
()
.
Do
(
"HMSET"
,
append
(
g
.
Slice
{
CacheKey
},
gutil
.
MapToSlice
(
gconv
.
Map
(
jdRes
.
Result
))
...
)
...
)
_
,
_
=
conn
.
Do
(
"HMSET"
,
append
(
g
.
Slice
{
CacheKey
},
gutil
.
MapToSlice
(
gconv
.
Map
(
jdRes
.
Result
))
...
)
...
)
s
.
AccessToken
=
jdRes
.
Result
.
AccessToken
return
}
...
...
@@ -101,7 +106,12 @@ func (s *client) refreshToken(ctx context.Context, req *accessToken) (err error)
err
=
errors
.
New
(
jdRes
.
ResultMessage
)
return
}
_
,
_
=
g
.
Redis
()
.
Do
(
"HMSET"
,
append
(
g
.
Slice
{
CacheKey
},
gutil
.
MapToSlice
(
gconv
.
Map
(
jdRes
.
Result
))
...
)
...
)
conn
:=
g
.
Redis
()
.
Ctx
(
ctx
)
.
Conn
()
defer
func
()
{
_
=
conn
.
Close
()
}()
_
,
_
=
conn
.
DoVar
(
"SELECT"
,
10
)
_
,
_
=
conn
.
Do
(
"HMSET"
,
append
(
g
.
Slice
{
CacheKey
},
gutil
.
MapToSlice
(
gconv
.
Map
(
jdRes
.
Result
))
...
)
...
)
s
.
AccessToken
=
jdRes
.
Result
.
AccessToken
return
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论