Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Z
zy_test
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张瑜
zy_test
Commits
60e61605
Commit
60e61605
authored
Sep 28, 2021
by
张瑜
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
转链所需接口
parent
c5237299
Changes
15
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
414 additions
and
174 deletions
+414
-174
README.md
README.md
+1
-1
api/app/turn_chain.go
api/app/turn_chain.go
+64
-10
application.yaml.example
application.yaml.example
+1
-1
cmd/app.go
cmd/app.go
+17
-16
go.mod
go.mod
+2
-0
go.sum
go.sum
+11
-0
internal/global/core_struct.go
internal/global/core_struct.go
+6
-4
internal/request/HomeClipboardRequest.go
internal/request/HomeClipboardRequest.go
+0
-3
internal/service/turn_chain.go
internal/service/turn_chain.go
+0
-5
internal/utils/ants.go
internal/utils/ants.go
+20
-0
internal/utils/dtk/item_detail.go
internal/utils/dtk/item_detail.go
+14
-3
internal/utils/taobao/client.go
internal/utils/taobao/client.go
+0
-8
internal/utils/taobao/common.go
internal/utils/taobao/common.go
+117
-0
internal/utils/taobao/gy_info.go
internal/utils/taobao/gy_info.go
+98
-0
internal/utils/taobao/item_detail.go
internal/utils/taobao/item_detail.go
+63
-123
No files found.
README.md
View file @
60e61605
...
...
@@ -6,5 +6,5 @@
###### 运行前
配置文件
cp application
-local.yaml.example application-local
.yaml
cp application
.yaml.example application
.yaml
api/app/turn_chain.go
View file @
60e61605
...
...
@@ -3,24 +3,78 @@ package appServer
import
(
"fmt"
"github.com/kataras/iris/v12"
"sync"
"time"
"xiaoxiong/internal/global"
"xiaoxiong/internal/utils/dtk"
"xiaoxiong/internal/utils/taobao"
)
type
HomeClipboardRequest
struct
{
Message
string
`json:"message"`
ItemId
string
`json:"item_id"`
Pid
string
`json:"pid"`
RelationId
string
`json:"relation_id"`
DtkAppType
int8
`json:"dtk_app_type"`
TaobaoAppType
int8
`json:"taobao_app_type"`
}
type
HomeClipboardResponse
struct
{
global
.
Response
DtkItemDetail
*
dtk
.
ItemDetailData
`json:"dtk_item_detail"`
TaobaoItemDetail
*
taobao
.
TbkItemInfoGet
`json:"taobao_item_detail"`
GyInfo
*
taobao
.
TbkCouponConvert
`json:"gy_info"`
}
// HomeClipboard curl -H "Content-Type: application/json" -X POST -d '{"item_id": "630305425763", "pid":"111476400316", "relation_id":"", "dtk_app_type":"0" ,"taobao_app_type":"0"}' "http://127.0.0.1:8090/turnchain.homeClipboard"
func
HomeClipboard
(
ctx
iris
.
Context
)
{
//req := &HomeClipboardRequest{}
//resp := &HomeClipboardResponse{}
//if err := ctx.ReadJSON(req); err != nil {
// return
//}
//service.HomeClipboard(*req, *resp)
fmt
.
Println
(
"asd"
)
req
:=
&
HomeClipboardRequest
{}
resp
:=
&
HomeClipboardResponse
{}
err
:=
ctx
.
ReadJSON
(
req
)
if
err
==
nil
{
_
,
_
=
ctx
.
JSON
(
iris
.
Map
{
"code"
:
2001
,
"msg"
:
"参数错误"
,
"data"
:
""
})
return
}
global
.
Log
.
Debug
(
fmt
.
Sprintf
(
"req:%s
\n
"
,
req
))
var
dtkItemDetail
*
dtk
.
ItemDetailData
var
dtkErr
error
var
taobaoItemDetail
*
taobao
.
TbkItemInfoGet
var
taobaoItemDetailErr
error
var
gyInfo
*
taobao
.
TbkCouponConvert
var
gyInfoErr
error
var
wg
sync
.
WaitGroup
wg
.
Add
(
1
)
_
=
global
.
GoPool
.
Submit
(
func
()
{
global
.
Log
.
Debug
(
"dtkItemDetail:"
+
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05.999999999"
))
dtkItemDetail
,
dtkErr
=
dtk
.
ItemDetail
(
req
.
ItemId
,
req
.
DtkAppType
)
wg
.
Done
()
})
wg
.
Add
(
1
)
_
=
global
.
GoPool
.
Submit
(
func
()
{
global
.
Log
.
Debug
(
"taobaoItemDetail:"
+
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05.999999999"
))
taobaoItemDetail
,
taobaoItemDetailErr
=
taobao
.
ItemDetail
(
req
.
ItemId
,
req
.
TaobaoAppType
)
wg
.
Done
()
})
wg
.
Add
(
1
)
_
=
global
.
GoPool
.
Submit
(
func
()
{
global
.
Log
.
Debug
(
"gyInfo:"
+
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05.999999999"
))
gyInfo
,
gyInfoErr
=
taobao
.
GyInfo
(
req
.
ItemId
,
req
.
Pid
,
req
.
RelationId
,
req
.
TaobaoAppType
)
wg
.
Done
()
})
wg
.
Wait
()
resp
.
TaobaoItemDetail
=
taobaoItemDetail
resp
.
GyInfo
=
gyInfo
resp
.
DtkItemDetail
=
dtkItemDetail
global
.
Log
.
Debug
(
fmt
.
Sprintf
(
"taobaoItemDetailErr:%s
\n
"
,
taobaoItemDetailErr
))
global
.
Log
.
Debug
(
fmt
.
Sprintf
(
"dtkErr:%s
\n
"
,
dtkErr
))
global
.
Log
.
Debug
(
fmt
.
Sprintf
(
"gyInfoErr:%s
\n
"
,
gyInfoErr
))
_
,
_
=
ctx
.
JSON
(
iris
.
Map
{
"code"
:
2000
,
"msg"
:
""
,
"data"
:
resp
})
}
application
-local
.yaml.example
→
application.yaml.example
View file @
60e61605
...
...
@@ -13,7 +13,7 @@ mysql:
system:
debug: false
env: 'public'
addr: 80
8
0
addr: 80
9
0
db-type: 'mysql'
# zap logger configuration
...
...
cmd/app.go
View file @
60e61605
...
...
@@ -8,34 +8,35 @@ import (
)
var
app
=
&
cli
.
App
{
Name
:
"小熊有好货"
,
Name
:
"小熊有好货"
,
Usage
:
"xiaoxiong"
,
}
var
c
=
cli
.
Commands
{}
func
Run
(){
initApp
()
app
.
Commands
=
c
err
:=
app
.
Run
(
os
.
Args
)
if
err
!=
nil
{
panic
(
err
)
}
}
// 初始化核心组件
func
initApp
(){
func
Run
()
{
// 配置文件
_
=
utils
.
Viper
()
// log
global
.
Log
=
utils
.
Zap
()
// goroutine pool
var
poolReleaseFunc
func
()
global
.
GoPool
,
poolReleaseFunc
=
utils
.
InitPool
()
defer
poolReleaseFunc
()
// 数据库
//
global.GORM = utils.GormInit()
global
.
GORM
=
utils
.
GormInit
()
// redis
//
global.Redis = utils.InitRedis()
global
.
Redis
=
utils
.
InitRedis
()
}
\ No newline at end of file
app
.
Commands
=
c
err
:=
app
.
Run
(
os
.
Args
)
if
err
!=
nil
{
panic
(
err
)
}
}
go.mod
View file @
60e61605
...
...
@@ -21,9 +21,11 @@ require (
github.com/moul/http2curl v1.0.0 // indirect
github.com/onsi/gomega v1.14.0 // indirect
github.com/oschwald/geoip2-golang v1.5.0
github.com/panjf2000/ants/v2 v2.4.6 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/spf13/viper v1.8.1
github.com/tidwall/gjson v1.9.1 // indirect
github.com/urfave/cli/v2 v2.3.0
github.com/valyala/fasthttp v1.28.0 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
...
...
go.sum
View file @
60e61605
...
...
@@ -414,6 +414,10 @@ github.com/oschwald/geoip2-golang v1.5.0 h1:igg2yQIrrcRccB1ytFXqBfOHCjXWIoMv85lV
github.com/oschwald/geoip2-golang v1.5.0/go.mod h1:xdvYt5xQzB8ORWFqPnqMwZpCpgNagttWdoZLlJQzg7s=
github.com/oschwald/maxminddb-golang v1.8.0 h1:Uh/DSnGoxsyp/KYbY1AuP0tYEwfs0sCph9p/UMXK/Hk=
github.com/oschwald/maxminddb-golang v1.8.0/go.mod h1:RXZtst0N6+FY/3qCNmZMBApR19cdQj43/NM9VkrNAis=
github.com/panjf2000/ants v1.2.1 h1:IlhLREssFi+YFOITnHdH3FHhulY6WDS0OB9e7+3fMHk=
github.com/panjf2000/ants v1.2.1/go.mod h1:AaACblRPzq35m1g3enqYcxspbbiOJJYaxU2wMpm1cXY=
github.com/panjf2000/ants/v2 v2.4.6 h1:drmj9mcygn2gawZ155dRbo+NfXEfAssjZNU1qoIb4gQ=
github.com/panjf2000/ants/v2 v2.4.6/go.mod h1:f6F0NZVFsGCp5A7QW/Zj/m92atWwOkY0OIhFxRNFr4A=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ=
...
...
@@ -482,6 +486,12 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tidwall/gjson v1.9.1 h1:wrrRk7TyL7MmKanNRck/Mcr3VU1sdMvJHvJXzqBIUNo=
github.com/tidwall/gjson v1.9.1/go.mod h1:jydLKE7s8J0+1/5jC4eXcuFlzKizGrCKvLmBVX/5oXc=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
...
...
@@ -918,6 +928,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
...
...
internal/global/core_struct.go
View file @
60e61605
...
...
@@ -2,14 +2,16 @@ package global
import
(
"github.com/go-redis/redis"
"github.com/panjf2000/ants/v2"
"go.uber.org/zap"
"gorm.io/gorm"
"xiaoxiong/config"
)
var
(
GORM
*
gorm
.
DB
CONFIG
config
.
Config
Log
*
zap
.
Logger
Redis
*
redis
.
Client
GORM
*
gorm
.
DB
CONFIG
config
.
Config
Log
*
zap
.
Logger
Redis
*
redis
.
Client
GoPool
*
ants
.
Pool
)
internal/request/HomeClipboardRequest.go
deleted
100644 → 0
View file @
c5237299
package
request
internal/service/turn_chain.go
deleted
100644 → 0
View file @
c5237299
package
service
func
HomeClipboard
(
itemId
string
,
relationId
string
,
openId
string
,
turnType
int
)
{
}
internal/utils/ants.go
0 → 100644
View file @
60e61605
package
utils
import
(
"fmt"
"github.com/panjf2000/ants/v2"
"xiaoxiong/internal/global"
)
func
InitPool
()(
*
ants
.
Pool
,
func
())
{
p
,
err
:=
ants
.
NewPool
(
1000
)
if
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"ants pool 初始化失败,err:%s"
,
err
.
Error
()))
}
return
p
,
func
()
{
p
.
Release
()
global
.
Log
.
Debug
(
"ants池释放"
)
}
}
\ No newline at end of file
internal/utils/dtk/item_detail.go
View file @
60e61605
...
...
@@ -103,13 +103,24 @@ type ItemDetailData struct {
}
`json:"data"`
}
func
ItemDetail
(
itemId
string
,
turn
Type
int8
)
(
*
ItemDetailData
,
error
)
{
func
ItemDetail
(
itemId
string
,
appKey
Type
int8
)
(
*
ItemDetailData
,
error
)
{
var
err
error
var
appKey
string
var
secret
string
t
:=
time
.
Now
()
.
UnixNano
()
/
1000000
nonce
:=
utils
.
RandNum
(
100000
,
999999
)
appKey
:=
"5d00ab04a9f3a"
secret
:=
"8de67b31a8b08167253b718fa565eb04"
if
appKeyType
==
1
{
appKey
=
"5f5726d7b7747"
secret
=
"0fd064738c4bd39eb790f91be66aa2b0"
}
else
if
appKeyType
==
3
{
appKey
=
"602f96351f26d"
secret
=
"c6ab0d1ff94b0895ed749ca0b27c3af6"
}
else
{
appKey
=
"5d00ab04a9f3a"
secret
=
"8de67b31a8b08167253b718fa565eb04"
}
d
:=
&
CommonData
{
appKey
,
"v1.3.0"
,
...
...
internal/utils/taobao/client.go
deleted
100644 → 0
View file @
c5237299
package
taobao
func
newClient
()
{
}
\ No newline at end of file
internal/utils/taobao/common.go
0 → 100644
View file @
60e61605
package
taobao
import
(
"crypto/md5"
"encoding/hex"
"fmt"
"net/url"
"reflect"
"sort"
"strings"
"time"
)
const
TaobaoUrl
=
"http://gw.api.taobao.com/router/rest"
type
CommonRequest
struct
{
AppKey
string
`json:"app_key"`
V
string
`json:"v"`
Format
string
`json:"format"`
SignMethod
string
`json:"sign_method"`
Method
string
`json:"method"`
Timestamp
string
`json:"timestamp"`
//PartnerId string `json:"partner_id"`
Sign
string
`json:"sign"`
}
func
newCommonRequest
(
appKeyType
int8
)
*
CommonRequest
{
r
:=
&
CommonRequest
{}
var
appKey
string
if
appKeyType
==
2
{
appKey
=
"28173405"
}
else
{
appKey
=
"25623446"
}
r
.
AppKey
=
appKey
r
.
V
=
"2.0"
r
.
Format
=
"json"
r
.
SignMethod
=
"md5"
r
.
Timestamp
=
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
)
return
r
}
// 获取请求参数,公共参数在url中,业务参数post
func
getPostData
(
c
interface
{})
(
url
.
Values
,
url
.
Values
)
{
t
:=
reflect
.
TypeOf
(
c
)
v
:=
reflect
.
ValueOf
(
c
)
res
:=
url
.
Values
{}
get
:=
url
.
Values
{}
for
k
:=
0
;
k
<
t
.
NumField
();
k
++
{
if
_
,
ok
:=
v
.
Field
(
k
)
.
Interface
()
.
(
CommonRequest
);
ok
{
tt
:=
reflect
.
TypeOf
(
v
.
Field
(
k
)
.
Interface
())
vv
:=
reflect
.
ValueOf
(
v
.
Field
(
k
)
.
Interface
())
for
kk
:=
0
;
kk
<
tt
.
NumField
();
kk
++
{
if
value
,
ok
:=
vv
.
Field
(
kk
)
.
Interface
()
.
(
string
);
ok
{
get
.
Set
(
tt
.
Field
(
kk
)
.
Tag
.
Get
(
"json"
),
value
)
}
}
}
else
{
if
value
,
ok
:=
v
.
Field
(
k
)
.
Interface
()
.
(
string
);
ok
{
res
.
Set
(
t
.
Field
(
k
)
.
Tag
.
Get
(
"json"
),
value
)
}
}
}
return
res
,
get
}
func
generateSign
(
c
interface
{},
appKeyType
int8
)
string
{
t
:=
reflect
.
TypeOf
(
c
)
v
:=
reflect
.
ValueOf
(
c
)
m
:=
make
(
map
[
string
]
interface
{})
for
k
:=
0
;
k
<
t
.
NumField
();
k
++
{
if
_
,
ok
:=
v
.
Field
(
k
)
.
Interface
()
.
(
CommonRequest
);
ok
{
tt
:=
reflect
.
TypeOf
(
v
.
Field
(
k
)
.
Interface
())
vv
:=
reflect
.
ValueOf
(
v
.
Field
(
k
)
.
Interface
())
for
kk
:=
0
;
kk
<
tt
.
NumField
();
kk
++
{
m
[
tt
.
Field
(
kk
)
.
Tag
.
Get
(
"json"
)]
=
vv
.
Field
(
kk
)
.
Interface
()
}
}
else
{
m
[
t
.
Field
(
k
)
.
Tag
.
Get
(
"json"
)]
=
v
.
Field
(
k
)
.
Interface
()
}
}
var
arr
[]
string
for
k
,
_
:=
range
m
{
arr
=
append
(
arr
,
k
)
}
sort
.
Strings
(
arr
)
var
secret
string
if
appKeyType
==
2
{
secret
=
"2299f4ce4d0c78ae5dec005f004e3444"
}
else
{
secret
=
"e835d91edfb5487125f754d831a02fba"
}
str
:=
secret
for
_
,
k
:=
range
arr
{
if
k
!=
"sign"
{
str
+=
fmt
.
Sprintf
(
"%s%s"
,
k
,
m
[
k
])
}
}
str
+=
secret
println
(
"asdasdsad-"
+
str
)
h
:=
md5
.
New
()
h
.
Write
([]
byte
(
str
))
return
strings
.
ToTitle
(
hex
.
EncodeToString
(
h
.
Sum
(
nil
)))
}
func
httpPost
(
req
interface
{},
resp
*
interface
{})
{
}
internal/utils/taobao/gy_info.go
0 → 100644
View file @
60e61605
package
taobao
import
(
"encoding/json"
"errors"
"fmt"
"github.com/tidwall/gjson"
"io/ioutil"
"net/http"
"xiaoxiong/internal/global"
)
type
TbkCouponConvertRequest
struct
{
CommonRequest
AdzoneId
string
`json:"adzone_id"`
ItemId
string
`json:"item_id"`
RelationId
string
`json:"relation_id"`
}
type
TbkCouponConvert
struct
{
TbkCouponConvertResponse
struct
{
Result
struct
{
ResultCode
int
`json:"result_code"`
Results
struct
{
CampaignType
int
`json:"campaign_type"`
CategoryId
int
`json:"category_id"`
CouponClickUrl
string
`json:"coupon_click_url"`
CouponEndTime
string
`json:"coupon_end_time"`
CouponInfo
string
`json:"coupon_info"`
CouponRemainCount
int
`json:"coupon_remain_count"`
CouponStartTime
string
`json:"coupon_start_time"`
CouponTotalCount
int
`json:"coupon_total_count"`
ItemId
int64
`json:"item_id"`
ItemUrl
string
`json:"item_url"`
MaxCommissionRate
string
`json:"max_commission_rate"`
RewardInfo
int
`json:"reward_info"`
}
`json:"results"`
}
`json:"result"`
RequestId
string
`json:"request_id"`
}
`json:"tbk_coupon_convert_response"`
}
func
newTbkCouponConvertRequest
(
itemId
string
,
Pid
string
,
RelationId
string
,
appKeyType
int8
)
*
TbkCouponConvertRequest
{
c
:=
newCommonRequest
(
appKeyType
)
c
.
Method
=
"taobao.tbk.coupon.convert"
r
:=
&
TbkCouponConvertRequest
{
*
c
,
Pid
,
itemId
,
RelationId
,
}
r
.
Sign
=
generateSign
(
*
r
,
appKeyType
)
if
r
.
Sign
==
""
{
return
nil
}
return
r
}
func
GyInfo
(
itemId
string
,
Pid
string
,
RelationId
string
,
appKeyType
int8
)
(
*
TbkCouponConvert
,
error
)
{
var
err
error
req
:=
newTbkCouponConvertRequest
(
itemId
,
Pid
,
RelationId
,
appKeyType
)
//fmt.Printf("%s", req)
if
req
==
nil
{
return
nil
,
errors
.
New
(
"签名失败"
)
}
res
:=
&
TbkCouponConvert
{}
postData
,
getData
:=
getPostData
(
*
req
)
urlPath
:=
TaobaoUrl
+
"?"
+
getData
.
Encode
()
// TODO 复用client
resp
,
err
:=
http
.
PostForm
(
urlPath
,
postData
)
defer
func
()
{
err
=
resp
.
Body
.
Close
()
if
err
!=
nil
{
global
.
Log
.
Info
(
fmt
.
Sprintf
(
"resp close err %s"
,
err
.
Error
()))
}
}()
body
,
_
:=
ioutil
.
ReadAll
(
resp
.
Body
)
errResp
:=
gjson
.
ParseBytes
(
body
)
.
Get
(
"error_response"
)
if
errResp
.
Exists
()
{
return
nil
,
errors
.
New
(
errResp
.
Raw
)
}
//fmt.Printf("%s\n", body)
err
=
json
.
Unmarshal
(
body
,
res
)
return
res
,
nil
}
internal/utils/taobao/item_detail.go
View file @
60e61605
package
taobao
import
(
"
crypto/md5
"
"e
ncoding/hex
"
"
encoding/json
"
"e
rrors
"
"fmt"
"github.com/tidwall/gjson"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"sort"
"strings"
"time"
"xiaoxiong/internal/global"
)
const
TaobaoUrl
=
"http://gw.api.taobao.com/router/rest"
type
TbkItemInfoGetRequest
struct
{
CommonRequest
Platform
string
`json:"platform"`
NumIids
string
`json:"num_iids"`
}
type
CommonRequest
struct
{
AppKey
string
`json:"app_key"`
V
string
`json:"v"`
Format
string
`json:"format"`
SignMethod
string
`json:"sign_method"`
Method
string
`json:"method"`
Timestamp
string
`json:"timestamp"`
//PartnerId string `json:"partner_id"`
Sign
string
`json:"sign"`
}
func
getPostData
(
c
interface
{})
(
url
.
Values
,
url
.
Values
)
{
t
:=
reflect
.
TypeOf
(
c
)
v
:=
reflect
.
ValueOf
(
c
)
res
:=
url
.
Values
{}
get
:=
url
.
Values
{}
for
k
:=
0
;
k
<
t
.
NumField
();
k
++
{
if
_
,
ok
:=
v
.
Field
(
k
)
.
Interface
()
.
(
CommonRequest
);
ok
{
tt
:=
reflect
.
TypeOf
(
v
.
Field
(
k
)
.
Interface
())
vv
:=
reflect
.
ValueOf
(
v
.
Field
(
k
)
.
Interface
())
for
kk
:=
0
;
kk
<
tt
.
NumField
();
kk
++
{
if
value
,
ok
:=
vv
.
Field
(
kk
)
.
Interface
()
.
(
string
);
ok
{
//res.Set(tt.Field(kk).Tag.Get("json"), value)
get
.
Set
(
tt
.
Field
(
kk
)
.
Tag
.
Get
(
"json"
),
value
)
}
}
}
else
{
if
value
,
ok
:=
v
.
Field
(
k
)
.
Interface
()
.
(
string
);
ok
{
res
.
Set
(
t
.
Field
(
k
)
.
Tag
.
Get
(
"json"
),
value
)
}
}
}
return
res
,
get
}
func
generateSign
(
c
interface
{},
appKeyType
int8
)
string
{
t
:=
reflect
.
TypeOf
(
c
)
v
:=
reflect
.
ValueOf
(
c
)
m
:=
make
(
map
[
string
]
interface
{})
for
k
:=
0
;
k
<
t
.
NumField
();
k
++
{
if
_
,
ok
:=
v
.
Field
(
k
)
.
Interface
()
.
(
CommonRequest
);
ok
{
tt
:=
reflect
.
TypeOf
(
v
.
Field
(
k
)
.
Interface
())
vv
:=
reflect
.
ValueOf
(
v
.
Field
(
k
)
.
Interface
())
for
kk
:=
0
;
kk
<
tt
.
NumField
();
kk
++
{
m
[
tt
.
Field
(
kk
)
.
Tag
.
Get
(
"json"
)]
=
vv
.
Field
(
kk
)
.
Interface
()
}
}
else
{
m
[
t
.
Field
(
k
)
.
Tag
.
Get
(
"json"
)]
=
v
.
Field
(
k
)
.
Interface
()
}
}
var
arr
[]
string
for
k
,
_
:=
range
m
{
arr
=
append
(
arr
,
k
)
}
sort
.
Strings
(
arr
)
secret
:=
"e835d91edfb5487125f754d831a02fba"
str
:=
secret
for
_
,
k
:=
range
arr
{
str
+=
fmt
.
Sprintf
(
"%s%s"
,
k
,
m
[
k
])
}
str
+=
secret
println
(
str
)
h
:=
md5
.
New
()
h
.
Write
([]
byte
(
str
))
return
strings
.
ToTitle
(
hex
.
EncodeToString
(
h
.
Sum
(
nil
)))
type
TbkItemInfoGet
struct
{
TbkItemInfoGetResponse
struct
{
Results
struct
{
NTbkItem
[]
struct
{
CatLeafName
string
`json:"cat_leaf_name"`
CatName
string
`json:"cat_name"`
FreeShipment
bool
`json:"free_shipment"`
HotFlag
string
`json:"hot_flag"`
ItemUrl
string
`json:"item_url"`
JuOnlineEndTime
string
`json:"ju_online_end_time"`
JuOnlineStartTime
string
`json:"ju_online_start_time"`
JuPreShowEndTime
string
`json:"ju_pre_show_end_time"`
JuPreShowStartTime
string
`json:"ju_pre_show_start_time"`
MaterialLibType
string
`json:"material_lib_type"`
Nick
string
`json:"nick"`
NumIid
string
`json:"num_iid"`
PictUrl
string
`json:"pict_url"`
PresaleDeposit
string
`json:"presale_deposit"`
PresaleEndTime
int
`json:"presale_end_time"`
PresaleStartTime
int
`json:"presale_start_time"`
PresaleTailEndTime
int
`json:"presale_tail_end_time"`
PresaleTailStartTime
int
`json:"presale_tail_start_time"`
Provcity
string
`json:"provcity"`
ReservePrice
string
`json:"reserve_price"`
SellerId
int64
`json:"seller_id"`
SmallImages
struct
{
String
[]
string
`json:"string"`
}
`json:"small_images"`
SuperiorBrand
string
`json:"superior_brand"`
Title
string
`json:"title"`
TmallPlayActivityEndTime
int
`json:"tmall_play_activity_end_time"`
TmallPlayActivityStartTime
int
`json:"tmall_play_activity_start_time"`
UserType
int
`json:"user_type"`
Volume
int
`json:"volume"`
ZkFinalPrice
string
`json:"zk_final_price"`
}
`json:"n_tbk_item"`
}
`json:"results"`
RequestId
string
`json:"request_id"`
}
`json:"tbk_item_info_get_response"`
}
func
newTbkItemInfoGetRequest
(
itemId
string
,
appKeyType
int8
)
*
TbkItemInfoGetRequest
{
c
:=
newCommonRequest
(
appKeyType
)
c
.
Method
=
"taobao.tbk.item.info.get"
r
:=
&
TbkItemInfoGetRequest
{
*
c
,
"2"
,
...
...
@@ -107,51 +69,27 @@ func newTbkItemInfoGetRequest(itemId string, appKeyType int8) *TbkItemInfoGetReq
r
.
Sign
=
generateSign
(
*
r
,
appKeyType
)
return
r
}
func
newCommonRequest
(
appKeyType
int8
)
*
CommonRequest
{
r
:=
&
CommonRequest
{}
appKey
:=
"25623446"
r
.
AppKey
=
appKey
r
.
V
=
"2.0"
r
.
Format
=
"json"
r
.
SignMethod
=
"md5"
r
.
Method
=
"taobao.tbk.item.info.get"
r
.
Timestamp
=
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
)
if
r
.
Sign
==
""
{
return
nil
}
return
r
}
type
TbkItemInfoGetResponse
struct
{
}
func
ItemDetail
(
itemId
string
,
appKeyType
int8
)
(
*
TbkItemInfoGetResponse
,
error
)
{
func
ItemDetail
(
itemId
string
,
appKeyType
int8
)
(
*
TbkItemInfoGet
,
error
)
{
var
err
error
req
:=
newTbkItemInfoGetRequest
(
itemId
,
appKeyType
)
if
req
==
nil
{
return
nil
,
errors
.
New
(
"签名失败"
)
}
res
:=
&
TbkItemInfoGet
{}
a
:=
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
)
println
()
println
()
sss
:=
fmt
.
Sprintf
(
"%s%s"
,
"asdsad"
,
a
)
println
(
sss
)
println
()
println
()
v
,
getV
:=
getPostData
(
*
req
)
println
(
v
.
Encode
())
println
(
getV
.
Encode
())
urlPath
:=
TaobaoUrl
+
"?"
+
getV
.
Encode
()
println
(
urlPath
)
postData
,
getData
:=
getPostData
(
*
req
)
resp
,
err
:=
http
.
PostForm
(
urlPath
,
v
)
urlPath
:=
TaobaoUrl
+
"?"
+
getData
.
Encode
()
// TODO 复用client
resp
,
err
:=
http
.
PostForm
(
urlPath
,
postData
)
defer
func
()
{
err
=
resp
.
Body
.
Close
()
if
err
!=
nil
{
...
...
@@ -160,13 +98,15 @@ func ItemDetail(itemId string, appKeyType int8) (*TbkItemInfoGetResponse, error)
}()
body
,
_
:=
ioutil
.
ReadAll
(
resp
.
Body
)
//fmt.Printf("%s", body)
//res := &ItemDetailData{}
//_ = json.Unmarshal(body, res)
errResp
:=
gjson
.
ParseBytes
(
body
)
.
Get
(
"error_response"
)
if
errResp
.
Exists
()
{
return
nil
,
errors
.
New
(
errResp
.
Raw
)
}
fmt
.
Printf
(
"%s"
,
body
)
_
=
err
println
()
err
=
json
.
Unmarshal
(
body
,
res
)
return
nil
,
nil
return
res
,
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment