Compare commits
24 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
bbe5e26e95 | ||
![]() |
158bb2542d | ||
![]() |
26bc6b3bcc | ||
![]() |
755c39089b | ||
![]() |
ace2872c2d | ||
![]() |
d2a1182c66 | ||
![]() |
0377692779 | ||
![]() |
2f4e52cac5 | ||
![]() |
f26c317144 | ||
![]() |
2835ad200b | ||
![]() |
8b245587bb | ||
![]() |
f5e8a4b990 | ||
![]() |
754a8910f2 | ||
![]() |
587fd91338 | ||
![]() |
0c1bce5d8c | ||
![]() |
0237a95a43 | ||
![]() |
f1d35cabba | ||
![]() |
f7a1a30203 | ||
![]() |
9eecda8c5f | ||
![]() |
12db943012 | ||
![]() |
3676395e9f | ||
![]() |
2fd998b52d | ||
![]() |
2e8f0cb3f2 | ||
![]() |
992f39109d |
161
ali_cloud_tool/ali_cloud_express_client.go
Normal file
161
ali_cloud_tool/ali_cloud_express_client.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package ali_cloud_tool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AliCloudExpressClient struct {
|
||||
AppCode string
|
||||
Host string
|
||||
cache ICacheAdapter
|
||||
}
|
||||
|
||||
func NewAliCloudExpressClient(appCode string) *AliCloudExpressClient {
|
||||
return &AliCloudExpressClient{
|
||||
AppCode: appCode,
|
||||
Host: "https://qryexpress.market.alicloudapi.com/lundear/expressTracking",
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AliCloudExpressClient) GetLogisticsInfo(mobile, number string) (res *ExpressRes, err error) {
|
||||
if mobile == "" || number == "" {
|
||||
return nil, errors.New("请输入手机号和物流单号")
|
||||
}
|
||||
// 设置参数
|
||||
params := url.Values{}
|
||||
params.Add("mobile", mobile)
|
||||
params.Add("number", number)
|
||||
// 拼接URL
|
||||
var fullURL string
|
||||
fullURL, err = url.JoinPath(a.Host)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "查询物流信息失败, 拼接路径错误! 手机号:%s, 物流单号:%s", mobile, number)
|
||||
}
|
||||
// 拼接参数
|
||||
fullURL = fmt.Sprintf("%s?%s", fullURL, params.Encode())
|
||||
// 创建HTTP客户端
|
||||
client := &http.Client{}
|
||||
// 创建请求
|
||||
var req *http.Request
|
||||
req, err = http.NewRequest(http.MethodGet, fullURL, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "查询物流信息失败, 创建请求失败! 手机号:%s, 物流单号:%s", mobile, number)
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
req.Header.Add("Authorization", "APPCODE "+a.AppCode)
|
||||
|
||||
// 发送请求
|
||||
client.Timeout = 2 * time.Second
|
||||
var resp *http.Response
|
||||
resp, err = client.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "查询物流信息失败, 发送请求失败! 手机号:%s, 物流单号:%s", mobile, number)
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err = Body.Close()
|
||||
if err != nil {
|
||||
log.Printf("查询物流信息失败, 关闭响应体失败! 手机号:%s, 物流单号:%s , %+v\n", mobile, number, err)
|
||||
}
|
||||
}(resp.Body)
|
||||
|
||||
// 读取响应体
|
||||
var body []byte
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "查询物流信息失败, 读取响应体失败! 手机号:%s, 物流单号:%s", mobile, number)
|
||||
}
|
||||
//log.Printf("查询物流信息成功! %s\n", string(body))
|
||||
|
||||
// 解析JSON响应
|
||||
var expressRes ExpressRes
|
||||
err = json.Unmarshal(body, &expressRes)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "查询物流信息失败, 解析JSON响应失败, %s! 手机号:%s, 物流单号:%s", string(body), mobile, number)
|
||||
}
|
||||
|
||||
if expressRes.Code != 0 {
|
||||
return &expressRes, errors.Wrapf(err, "查询物流信息失败! expressRes:%+v 手机号:%s, 物流单号:%s", expressRes, mobile, number)
|
||||
}
|
||||
|
||||
if expressRes.Data == nil {
|
||||
return &expressRes, errors.Wrapf(err, "查询物流信息失败,没有查询到物流信息! expressRes:%+v 手机号:%s, 物流单号:%s", expressRes, mobile, number)
|
||||
}
|
||||
|
||||
return &expressRes, nil
|
||||
}
|
||||
|
||||
func (a *AliCloudExpressClient) Set(cache ICacheAdapter) {
|
||||
a.cache = cache
|
||||
}
|
||||
|
||||
func (a *AliCloudExpressClient) GetLogisticsInfoFormCache(ctx context.Context, mobile, prefix, number string, opt ...time.Duration) (res *ExpressRes, err error) {
|
||||
if a.cache != nil {
|
||||
res, err = a.cache.Get(ctx, a.numberKey(prefix, number))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "获取缓存失败, number:%s", number)
|
||||
}
|
||||
if res != nil {
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
res, err = a.GetLogisticsInfo(mobile, number)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "获取物流信息失败, number:%s", number)
|
||||
}
|
||||
|
||||
var infoJson []byte
|
||||
infoJson, err = json.Marshal(res)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "无法将物流信息转换为JSON, number:%s", number)
|
||||
}
|
||||
|
||||
if len(opt) > 0 {
|
||||
err = a.cache.Set(ctx, a.numberKey(prefix, number), string(infoJson), opt[0])
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "缓存物流信息失败, number:%s", number)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (a *AliCloudExpressClient) DeleteLogisticsInfoCache(ctx context.Context, prefix, number string) (err error) {
|
||||
if a.cache == nil {
|
||||
return errors.New("缓存不能为空")
|
||||
}
|
||||
err = a.cache.Del(ctx, a.numberKey(prefix, number))
|
||||
return err
|
||||
}
|
||||
|
||||
// ipKey 生成Redis key
|
||||
func (a *AliCloudExpressClient) numberKey(prefix, number string) string {
|
||||
return fmt.Sprintf("%s:number:%s", prefix, number)
|
||||
}
|
||||
|
||||
type ExpressRes struct {
|
||||
Code int `json:"code"`
|
||||
Desc string `json:"desc"`
|
||||
Data *Data `json:"data"`
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
State int `json:"state" dc:"物流状态【1在途中,2派件中,3已签收,4派送失败,5揽收,6退回,7转单,8疑难,9退签,10待清关,11清关中,12已清关,13清关异常】"`
|
||||
Name string `json:"name" dc:"物流名"`
|
||||
Com string `json:"com"`
|
||||
Number string `json:"number" dc:"单号"`
|
||||
Logo string `json:"logo" dc:"图标地址"`
|
||||
List []*List `json:"list"`
|
||||
}
|
||||
|
||||
type List struct {
|
||||
Time string `json:"time"`
|
||||
Status string `json:"status"`
|
||||
}
|
130
ali_cloud_tool/ali_cloud_express_client_test.go
Normal file
130
ali_cloud_tool/ali_cloud_express_client_test.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package ali_cloud_tool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"log"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAliCloudExpressClient_GetLogisticsInfo(t *testing.T) {
|
||||
type fields struct {
|
||||
AppCode string
|
||||
Host string
|
||||
}
|
||||
type args struct {
|
||||
mobile string
|
||||
number string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantRes *ExpressRes
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "test1",
|
||||
fields: fields{
|
||||
AppCode: "",
|
||||
Host: "",
|
||||
},
|
||||
args: args{
|
||||
mobile: "",
|
||||
number: "",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
a := &AliCloudExpressClient{
|
||||
AppCode: tt.fields.AppCode,
|
||||
Host: tt.fields.Host,
|
||||
}
|
||||
gotRes, err := a.GetLogisticsInfo(tt.args.mobile, tt.args.number)
|
||||
log.Println(gotRes, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliCloudExpressClient_GetLogisticsInfoFormCache(t *testing.T) {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "",
|
||||
Password: "",
|
||||
DB: 1,
|
||||
})
|
||||
|
||||
// 创建缓存实例
|
||||
cache := NewRedisCache(rdb)
|
||||
a := &AliCloudExpressClient{
|
||||
AppCode: "",
|
||||
Host: "",
|
||||
cache: cache,
|
||||
}
|
||||
gotRes, err := a.GetLogisticsInfoFormCache(context.Background(), "", "", "", time.Minute)
|
||||
log.Println(gotRes, err)
|
||||
}
|
||||
|
||||
func TestAliCloudExpressClient_DeleteLogisticsInfoCache(t *testing.T) {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "",
|
||||
Password: "",
|
||||
DB: 1,
|
||||
})
|
||||
|
||||
// 创建缓存实例
|
||||
cache := NewRedisCache(rdb)
|
||||
a := &AliCloudExpressClient{
|
||||
AppCode: "",
|
||||
Host: "",
|
||||
cache: cache,
|
||||
}
|
||||
err := a.DeleteLogisticsInfoCache(context.Background(), "", "")
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
type RedisCache struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func (r *RedisCache) Del(ctx context.Context, number string) error {
|
||||
return r.client.Del(ctx, number).Err()
|
||||
}
|
||||
|
||||
func NewRedisCache(client *redis.Client) *RedisCache {
|
||||
return &RedisCache{client: client}
|
||||
}
|
||||
|
||||
func (r *RedisCache) Set(ctx context.Context, number string, res string, ttl time.Duration) error {
|
||||
if number == "" {
|
||||
return errors.New("number不能为空")
|
||||
}
|
||||
|
||||
return r.client.Set(ctx, number, res, ttl).Err()
|
||||
}
|
||||
|
||||
// Get 从Redis获取IP信息
|
||||
func (r *RedisCache) Get(ctx context.Context, number string) (*ExpressRes, error) {
|
||||
if number == "" {
|
||||
return nil, errors.New("number不能为空")
|
||||
}
|
||||
|
||||
data, err := r.client.Get(ctx, number).Bytes()
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, nil // 键不存在,返回nil而不是错误
|
||||
}
|
||||
return nil, fmt.Errorf("无法获取物流信息: %w", err)
|
||||
}
|
||||
|
||||
var info ExpressRes
|
||||
if err = json.Unmarshal(data, &info); err != nil {
|
||||
return nil, fmt.Errorf("无法解析物流信息: %w", err)
|
||||
}
|
||||
|
||||
return &info, nil
|
||||
}
|
116
ali_cloud_tool/ali_cloud_realname_client.go
Normal file
116
ali_cloud_tool/ali_cloud_realname_client.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package ali_cloud_tool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AliCloudRealNameClient struct {
|
||||
AppCode string
|
||||
Host string
|
||||
}
|
||||
|
||||
func NewAliCloudRealNameClient(appCode string) *AliCloudRealNameClient {
|
||||
return &AliCloudRealNameClient{
|
||||
AppCode: appCode,
|
||||
Host: "https://zidv2.market.alicloudapi.com/idcheck/Post",
|
||||
}
|
||||
}
|
||||
|
||||
// 调用实名认证API
|
||||
func (s *AliCloudRealNameClient) CallRealNameAuthAPI(ctx context.Context, name, idCard string) (*RealNameAuthData, error) {
|
||||
// 验证输入
|
||||
if strings.TrimSpace(name) == "" || strings.TrimSpace(idCard) == "" {
|
||||
return nil, errors.New("姓名和身份证号不能为空")
|
||||
}
|
||||
|
||||
// 设置参数
|
||||
params := url.Values{}
|
||||
params.Add("realName", name)
|
||||
params.Add("cardNo", idCard)
|
||||
|
||||
// 构建完整URL
|
||||
fullURL, err := url.JoinPath(s.Host)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("构建实名认证URL失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard)
|
||||
}
|
||||
|
||||
// 创建HTTP客户端
|
||||
client := &http.Client{
|
||||
Timeout: 2 * time.Second,
|
||||
}
|
||||
|
||||
// 创建POST请求
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL, strings.NewReader(params.Encode()))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建实名认证请求失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard)
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
req.Header.Set("Authorization", "APPCODE "+s.AppCode)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
||||
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("发送实名认证请求失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard)
|
||||
}
|
||||
defer func() {
|
||||
if closeErr := resp.Body.Close(); closeErr != nil {
|
||||
log.Printf("关闭实名认证响应体失败: %v, 姓名:%s, 身份证号:%s", closeErr, name, idCard)
|
||||
}
|
||||
}()
|
||||
|
||||
// 读取响应体
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取实名认证响应失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard)
|
||||
}
|
||||
|
||||
// 解析JSON响应
|
||||
var result RealNameAuthData
|
||||
if err = json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("解析实名认证响应失败: %w, 响应内容:%s, 姓名:%s, 身份证号:%s", err, string(body), name, idCard)
|
||||
}
|
||||
|
||||
// 检查API返回的错误码
|
||||
if result.ErrorCode != 0 {
|
||||
return &result, fmt.Errorf("实名认证接口返回错误, 错误码:%d, 原因:%s, 姓名:%s, 身份证号:%s",
|
||||
result.ErrorCode, result.Reason, name, idCard)
|
||||
}
|
||||
|
||||
// 检查认证结果
|
||||
if !result.Result.Isok {
|
||||
return &result, fmt.Errorf("实名认证不通过, 原因:%s, 姓名:%s, 身份证号:%s",
|
||||
result.Reason, name, idCard)
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
type RealNameAuthData struct {
|
||||
ErrorCode int `json:"error_code"`
|
||||
Reason string `json:"reason"`
|
||||
Result struct {
|
||||
Realname string `json:"realname"`
|
||||
Idcard string `json:"idcard"`
|
||||
Isok bool `json:"isok"`
|
||||
IdCardInfor struct {
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
District string `json:"district"`
|
||||
Area string `json:"area"`
|
||||
Sex string `json:"sex"`
|
||||
Birthday string `json:"birthday"`
|
||||
} `json:"IdCardInfor"`
|
||||
} `json:"result"`
|
||||
Sn string `json:"sn"`
|
||||
}
|
20
ali_cloud_tool/ali_cloud_realname_client_test.go
Normal file
20
ali_cloud_tool/ali_cloud_realname_client_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package ali_cloud_tool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAliCloudExpressClient_CallRealNameAuthAPI(t *testing.T) {
|
||||
s := &AliCloudRealNameClient{
|
||||
AppCode: "",
|
||||
Host: "",
|
||||
}
|
||||
got, err := s.CallRealNameAuthAPI(context.Background(), "", "")
|
||||
if err != nil {
|
||||
t.Errorf("CallRealNameAuthAPI() error = %v", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("=====", got)
|
||||
}
|
12
ali_cloud_tool/express_cache.go
Normal file
12
ali_cloud_tool/express_cache.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package ali_cloud_tool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ICacheAdapter interface {
|
||||
Set(ctx context.Context, key string, res string, ttl time.Duration) error
|
||||
Get(ctx context.Context, key string) (*ExpressRes, error)
|
||||
Del(ctx context.Context, key string) error
|
||||
}
|
6
go.mod
6
go.mod
@@ -1,6 +1,8 @@
|
||||
module git.ssgfgtfy.com/public/ssgf_utils
|
||||
|
||||
go 1.18.0
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.24.0
|
||||
|
||||
require (
|
||||
github.com/alibabacloud-go/darabonba-openapi/v2 v2.1.7
|
||||
@@ -27,7 +29,7 @@ require (
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/tjfoc/gmsm v1.4.1 // indirect
|
||||
golang.org/x/net v0.26.0 // indirect
|
||||
golang.org/x/net v0.41.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@@ -171,6 +171,10 @@ golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
|
||||
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
|
||||
golang.org/x/net v0.37.1-0.20250305215238-2914f4677317 h1:wneCP+2d9NUmndnyTmY7VwUNYiP26xiN/AtdcojQ1lI=
|
||||
golang.org/x/net v0.37.1-0.20250305215238-2914f4677317/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
||||
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
|
||||
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
|
@@ -19,11 +19,10 @@ type HuaChenIpClient struct {
|
||||
cache ICacheAdapter
|
||||
}
|
||||
|
||||
func NewHuaChenIpClient(appCode string, cache ICacheAdapter) *HuaChenIpClient {
|
||||
func NewHuaChenIpClient(appCode string) *HuaChenIpClient {
|
||||
return &HuaChenIpClient{
|
||||
AppCode: appCode,
|
||||
Host: "https://c2ba.api.huachen.cn",
|
||||
cache: cache,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,13 +89,19 @@ func (h *HuaChenIpClient) GetIpInfo(ip string) (res *ApiResult, err error) {
|
||||
return &apiResult, nil
|
||||
}
|
||||
|
||||
func (h *HuaChenIpClient) GetIpInfoFormCache(ctx context.Context, ip string, ttl time.Duration) (res *ApiResult, err error) {
|
||||
getCache, err := h.cache.Get(ctx, h.ipKey(ip))
|
||||
func (h *HuaChenIpClient) Set(cache ICacheAdapter) {
|
||||
h.cache = cache
|
||||
}
|
||||
|
||||
func (h *HuaChenIpClient) GetIpInfoFormCache(ctx context.Context, ip string, opt ...time.Duration) (res *ApiResult, err error) {
|
||||
if h.cache != nil {
|
||||
res, err = h.cache.Get(ctx, h.ipKey(ip))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "获取缓存失败,ip:%s", ip)
|
||||
}
|
||||
if getCache != nil {
|
||||
return getCache, nil
|
||||
if res != nil {
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
res, err = h.GetIpInfo(ip)
|
||||
if err != nil {
|
||||
@@ -109,10 +114,12 @@ func (h *HuaChenIpClient) GetIpInfoFormCache(ctx context.Context, ip string, ttl
|
||||
return nil, errors.Wrapf(err, "无法将IP信息转换为JSON,ip:%s", ip)
|
||||
}
|
||||
|
||||
err = h.cache.Set(ctx, h.ipKey(ip), string(infoJson), ttl)
|
||||
if len(opt) != 0 {
|
||||
err = h.cache.Set(ctx, h.ipKey(ip), string(infoJson), opt[0])
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "缓存ip:%s信息失败,", ip)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
201
jtt_tool/jtt_client.go
Normal file
201
jtt_tool/jtt_client.go
Normal file
@@ -0,0 +1,201 @@
|
||||
package jtt_tool
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto"
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type JttClient struct {
|
||||
AppId string
|
||||
ApiUrl string
|
||||
PublicKey string
|
||||
PrivateKey string
|
||||
}
|
||||
|
||||
func NewJttClient(appId, apiUrl, publicKey, privateKey string) *JttClient {
|
||||
return &JttClient{
|
||||
AppId: appId,
|
||||
ApiUrl: apiUrl,
|
||||
PublicKey: publicKey,
|
||||
PrivateKey: privateKey,
|
||||
}
|
||||
}
|
||||
|
||||
func (j *JttClient) FindUserForTokenMessage(address string) (res *FindUserForTokenMessageRes, err error) {
|
||||
if address == "" {
|
||||
return nil, fmt.Errorf("address is empty")
|
||||
}
|
||||
fmt.Println("address", address)
|
||||
|
||||
paramMap := make(map[string]any)
|
||||
|
||||
paramMap["appId"] = j.AppId
|
||||
paramMap["timestamp"] = time.Now().Unix()
|
||||
paramMap["publicKey"] = j.PublicKey
|
||||
paramMap["address"] = address
|
||||
|
||||
postRes, err := j.JttPost("/findUserForTokenMessage", paramMap)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(postRes, &res)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("转换FindUserForTokenMessageRes结构体失败: %s", string(postRes))
|
||||
return
|
||||
}
|
||||
|
||||
if res == nil || res.Code != 200 {
|
||||
err = fmt.Errorf("查询交易所数据失败: %w, res: %+v , 地址:%s", err, res, address)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
type FindUserForTokenMessageRes struct {
|
||||
Code int `json:"code"`
|
||||
Data *Data `json:"data"`
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
UserTokenList []*UserToken `json:"userTokenList"`
|
||||
}
|
||||
|
||||
type UserToken struct {
|
||||
Code int `json:"code"`
|
||||
Mes string `json:"mes"`
|
||||
TokenNum float64 `json:"tokenNum"`
|
||||
WalletAddress string `json:"walletAddress"`
|
||||
}
|
||||
|
||||
func (j *JttClient) JttPost(url string, paramMap map[string]any) (res []byte, err error) {
|
||||
|
||||
bodyByte, _ := json.Marshal(paramMap)
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, j.ApiUrl+url, bytes.NewBuffer(bodyByte))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
sign, err := j.ToSign(paramMap)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
paramMap["sign"] = sign
|
||||
|
||||
// 创建 HTTP 客户端
|
||||
client := &http.Client{}
|
||||
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
//log.Printf("WPSPost resp: %+v\n", resp)
|
||||
if err != nil {
|
||||
log.Printf("发送请求失败: %+v\n", err)
|
||||
return
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
// 读取响应体
|
||||
res, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("res: %s\n", string(res))
|
||||
|
||||
if !json.Valid(res) {
|
||||
return nil, errors.New("响应体不是有效的JSON格式")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *JttClient) ToSign(reqData map[string]interface{}) (sign []byte, errs error) {
|
||||
sortedStr, err := s.GetSortedStr(reqData)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
privateKeyStr := "-----BEGIN PRIVATE KEY-----\r\n" + s.PrivateKey + "\r\n-----END PRIVATE KEY-----"
|
||||
sign, err = s.Sign(sortedStr, privateKeyStr)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *JttClient) Sign(signData string, privateKeyStr string) (sign []byte, errs error) {
|
||||
hashedMessage := md5.Sum([]byte(signData))
|
||||
if privateKeyStr == "" {
|
||||
return nil, errors.New("private key is empty")
|
||||
}
|
||||
|
||||
block, _ := pem.Decode([]byte(privateKeyStr))
|
||||
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sign, err = rsa.SignPKCS1v15(rand.Reader, privateKey.(*rsa.PrivateKey), crypto.MD5, hashedMessage[:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sign = []byte(base64.StdEncoding.EncodeToString(sign))
|
||||
return
|
||||
}
|
||||
|
||||
func (s *JttClient) GetSortedStr(data any) (str string, err error) {
|
||||
tmp := map[string]interface{}{}
|
||||
if reflect.ValueOf(data).Kind() == reflect.Struct {
|
||||
jsStr, _ := json.Marshal(data)
|
||||
_ = json.Unmarshal(jsStr, &tmp)
|
||||
} else {
|
||||
ok := false
|
||||
tmp, ok = data.(map[string]interface{})
|
||||
if !ok {
|
||||
err = errors.New("data type error")
|
||||
}
|
||||
}
|
||||
|
||||
keys := []string{}
|
||||
for key := range tmp {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
sortedParams := []string{}
|
||||
|
||||
for _, key := range keys {
|
||||
value := tmp[key]
|
||||
if key == "sign" {
|
||||
continue
|
||||
}
|
||||
|
||||
switch v := value.(type) {
|
||||
case int, uint, int16, int32, int64:
|
||||
sortedParams = append(sortedParams, fmt.Sprintf("%s=%d", key, v))
|
||||
case float64, float32:
|
||||
sortedParams = append(sortedParams, fmt.Sprintf("%s=%f", key, v))
|
||||
default:
|
||||
sortedParams = append(sortedParams, key+"="+value.(string))
|
||||
}
|
||||
}
|
||||
str = strings.Join(sortedParams, "&")
|
||||
return
|
||||
}
|
51
jtt_tool/jtt_client_test.go
Normal file
51
jtt_tool/jtt_client_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package jtt_tool
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestJttClient_FindUserForTokenMessage(t *testing.T) {
|
||||
type fields struct {
|
||||
AppId string
|
||||
ApiUrl string
|
||||
PublicKey string
|
||||
PrivateKey string
|
||||
}
|
||||
type args struct {
|
||||
address string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantRes *FindUserForTokenMessageRes
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "test1",
|
||||
fields: fields{},
|
||||
args: args{
|
||||
address: "0x123456",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
h := &JttClient{
|
||||
AppId: test.fields.AppId,
|
||||
ApiUrl: test.fields.ApiUrl,
|
||||
PublicKey: test.fields.PublicKey,
|
||||
PrivateKey: test.fields.PrivateKey,
|
||||
}
|
||||
|
||||
gotRes, err := h.FindUserForTokenMessage(test.args.address)
|
||||
|
||||
log.Println(gotRes, err)
|
||||
|
||||
if (err != nil) != test.wantErr {
|
||||
t.Errorf("FindUserForTokenMessage() error = %v, wantErr %v", err, test.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -70,7 +70,19 @@ func (c *SmsClient) GetCode(ctx context.Context, key string) (code string, err e
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if value == nil {
|
||||
return "", errors.New("验证码不存在,请重新发送")
|
||||
}
|
||||
switch value.(type) {
|
||||
case string:
|
||||
return value.(string), nil
|
||||
case float64:
|
||||
return fmt.Sprintf("%d", int(value.(float64))), nil
|
||||
case int64:
|
||||
return fmt.Sprintf("%d", int(value.(int64))), nil
|
||||
default:
|
||||
return "", errors.New("验证码类型错误,请联系管理员")
|
||||
}
|
||||
}
|
||||
|
||||
// SaveCode 保存验证码
|
||||
|
@@ -105,3 +105,31 @@ func (c *WeiPinShangClient) GetGoodsStock(req GetGoodsStockReq) (*[]GoodsStock,
|
||||
res := out.Data
|
||||
return &res, err
|
||||
}
|
||||
|
||||
func (c *WeiPinShangClient) GetSaleVenue(req GetSaleVenueReq) (*SaleVenueList, error) {
|
||||
param := StructToMapString(req)
|
||||
out := GetSaleVenueRes{}
|
||||
err := c.PostAndParse(GetSaleVenueURL, param, &out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if out.Code != 0 {
|
||||
return nil, fmt.Errorf(out.Msg)
|
||||
}
|
||||
res := out.Data
|
||||
return &res, err
|
||||
}
|
||||
|
||||
func (c *WeiPinShangClient) GetSaleVenueGoods(req GetSaleVenueGoodsReq) (*SaleVenueGoodsList, error) {
|
||||
param := StructToMapString(req)
|
||||
out := GetSaleVenueGoodsRes{}
|
||||
err := c.PostAndParse(GetSaleVenueGoodsURL, param, &out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if out.Code != 0 {
|
||||
return nil, fmt.Errorf(out.Msg)
|
||||
}
|
||||
res := out.Data
|
||||
return &res, err
|
||||
}
|
||||
|
@@ -113,3 +113,31 @@ func TestGetGoodsStock(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, res)
|
||||
}
|
||||
|
||||
func TestGetSaleVenue(t *testing.T) {
|
||||
ts := setupMockServer(wps.GetSaleVenueURL, http.MethodPost, `{"code":200,"data":{"stocks":[]}}`, t)
|
||||
defer ts.Close()
|
||||
|
||||
client := newClientWithServer(ts)
|
||||
res, err := client.GetSaleVenue(wps.GetSaleVenueReq{
|
||||
PageNo: "1",
|
||||
PageSize: "100",
|
||||
Sort: 0,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, res)
|
||||
}
|
||||
|
||||
func TestGetSaleVenueGoods(t *testing.T) {
|
||||
ts := setupMockServer(wps.GetSaleVenueGoodsURL, http.MethodPost, `{"code":200,"data":{"stocks":[]}}`, t)
|
||||
defer ts.Close()
|
||||
|
||||
client := newClientWithServer(ts)
|
||||
res, err := client.GetSaleVenueGoods(wps.GetSaleVenueGoodsReq{
|
||||
PageNo: "1",
|
||||
PageSize: "100",
|
||||
ExhibitionID: "1829399501611053057",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, res)
|
||||
}
|
||||
|
@@ -169,10 +169,10 @@ type GoodsSkuItem struct {
|
||||
CGoodsColor string `json:"c_goods_color"` // 商品颜色
|
||||
CGoodsSize string `json:"c_goods_size"` // 商品尺寸
|
||||
CGoodsImage string `json:"c_goods_image"` // 商品列表图片
|
||||
CGoodsStockStart int `json:"c_goods_stock_start"` // 初始总库存
|
||||
CGoodsStockValid int `json:"c_goods_stock_valid"` // 有效库存(下单库存参考)
|
||||
CBuyMinNum int `json:"c_buy_min_num"` // 最小购买数量
|
||||
CBuyMaxNum int `json:"c_buy_max_num"` // 最大购买数量
|
||||
CGoodsStockStart interface{} `json:"c_goods_stock_start"` // 初始总库存
|
||||
CGoodsStockValid interface{} `json:"c_goods_stock_valid"` // 有效库存(下单库存参考)
|
||||
CBuyMinNum interface{} `json:"c_buy_min_num"` // 最小购买数量
|
||||
CBuyMaxNum interface{} `json:"c_buy_max_num"` // 最大购买数量
|
||||
}
|
||||
|
||||
type GetGoodsDetailsReq struct {
|
||||
@@ -199,3 +199,66 @@ type GoodsStock struct {
|
||||
CGoodsId string `json:"c_goods_id"`
|
||||
CStock int `json:"c_stock"`
|
||||
}
|
||||
|
||||
type GetSaleVenueReq struct {
|
||||
PageNo string `json:"pageNo"` //页码
|
||||
PageSize string `json:"pageSize"` //条数
|
||||
Sort int `json:"sort"` //传1为c_id desc ,不传或者传0为c_start_datetime asc
|
||||
}
|
||||
|
||||
type GetSaleVenueRes struct {
|
||||
Code int64 `json:"code"` //返回编码[0为成功,其它为失败]
|
||||
Msg string `json:"msg"` //返回信息[请求接口消息]
|
||||
Data SaleVenueList `json:"data"`
|
||||
}
|
||||
|
||||
type SaleVenueList struct {
|
||||
PageIndex string `json:"pageIndex"`
|
||||
PageCount int `json:"pageCount"`
|
||||
DataCount int `json:"dataCount"`
|
||||
List []SaleVenue `json:"list"` // 会场列表
|
||||
}
|
||||
|
||||
type SaleVenue struct {
|
||||
CID int `json:"c_id"` // 序号
|
||||
CExhibitionID string `json:"c_exhibition_id"` // 会场ID
|
||||
CBrandID int `json:"c_brand_id"` // 品牌ID
|
||||
CExhibitionName string `json:"c_exhibition_name"` // 会场名称
|
||||
CIconImageURL string `json:"c_icon_image_url"` // 会场列表图片
|
||||
CBannerImageURL string `json:"c_banner_image_url"` // 会场Banner图
|
||||
CDescription string `json:"c_description"` // 会场描述(可选)
|
||||
CStartDatetime string `json:"c_start_datetime"` // 会场开始时间
|
||||
CEndDatetime string `json:"c_end_datetime"` // 会场结束时间
|
||||
CCompanyCode string `json:"c_company_code"` //供应商编号
|
||||
}
|
||||
|
||||
type GetSaleVenueGoodsReq struct {
|
||||
PageNo string `json:"pageNo"` //页码
|
||||
PageSize string `json:"pageSize"` //条数
|
||||
ExhibitionID string `json:"exhibition_id"` //会场id
|
||||
}
|
||||
|
||||
type GetSaleVenueGoodsRes struct {
|
||||
Code int64 `json:"code"` //返回编码[0为成功,其它为失败]
|
||||
Msg string `json:"msg"` //返回信息[请求接口消息]
|
||||
Data SaleVenueGoodsList `json:"data"`
|
||||
}
|
||||
|
||||
type SaleVenueGoodsList struct {
|
||||
PageIndex string `json:"pageIndex"`
|
||||
PageCount int `json:"pageCount"`
|
||||
DataCount int `json:"dataCount"`
|
||||
List []SaleVenueGoods `json:"list"` // 会场列表
|
||||
}
|
||||
|
||||
type SaleVenueGoods struct {
|
||||
CID int `json:"c_id"` // 序号
|
||||
CExhibitionID string `json:"c_exhibition_id"` // 商品所属会场 ID
|
||||
CFatherGoodsID string `json:"c_father_goods_id"` // 商品编码
|
||||
CGoodsName string `json:"c_goods_name"` // 商品名称
|
||||
CGoodsImage string `json:"c_goods_image"` // 商品主图
|
||||
CGoodsDescription string `json:"c_goods_description"` // 商品描述
|
||||
CGoodsContent string `json:"c_goods_content"` // 商品详情图(可选)
|
||||
CInPrice string `json:"c_in_price"` // 商品进价
|
||||
COriginalPrice string `json:"c_original_price"` // 商品市场价(吊牌价)
|
||||
}
|
||||
|
@@ -29,6 +29,8 @@ const (
|
||||
GetDetailsGoodsUrl = "mcang/Mcang/getDetailsGoods" //获取批量商品详情接口
|
||||
GetGoodsDetailsURL = "mcang/Mcang/getGoodsDetails" //获取单个商品详情接口
|
||||
GetGoodsStockURL = "mcang/Mcang/getGoodsStock" //获取单个商品详情接口
|
||||
GetSaleVenueURL = "mcang/Mcang/getSaleVenue" //获取会场列表接口
|
||||
GetSaleVenueGoodsURL = "mcang/Mcang/getSaleVenueGoods" //获取会场商品列表接口
|
||||
)
|
||||
|
||||
// Config 客户端配置
|
||||
|
@@ -28,7 +28,7 @@ type CreateOrderData struct {
|
||||
ThirdOrderNo string `json:"thirdOrderNo"` // 第三方订单号 本地订单号
|
||||
OrderNo string `json:"orderNo"` // 主订单号
|
||||
McOrderNo string `json:"mcOrderNo"` //蜜仓子订单号
|
||||
OrderAmount float64 `json:"orderAmount"` // 子订单总金额
|
||||
OrderAmount interface{} `json:"orderAmount"` // 子订单总金额
|
||||
Sku []SkuData `json:"sku"` // 订单商品信息
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ type SkuData struct {
|
||||
GoodName string `json:"goodName"` //商品名称
|
||||
Num interface{} `json:"num"` //数量
|
||||
//Num int `json:"num"` //数量
|
||||
Price string `json:"price"` //单价
|
||||
Price interface{} `json:"price"` //单价
|
||||
}
|
||||
|
||||
type GetOrderInfoRes struct {
|
||||
|
@@ -33,7 +33,7 @@ func (w *WeiPinShangClient) GetManyPostage(getManyPostageReq *GetManyPostageReq)
|
||||
fmt.Println("getManyPostageReq", getManyPostageReq)
|
||||
paramMap := make(map[string]any)
|
||||
|
||||
paramMap["goodInfo"] = getManyPostageReq.GoodsInfo
|
||||
paramMap["goodsInfo"] = getManyPostageReq.GoodsInfo
|
||||
paramMap["address"] = getManyPostageReq.Address
|
||||
paramMap["province"] = getManyPostageReq.Province
|
||||
paramMap["county"] = getManyPostageReq.County
|
||||
|
@@ -29,7 +29,7 @@ func TestWeiPinShangClient_GetManyPostage(t *testing.T) {
|
||||
Key: "f654ea5bde7635c3f46191191e5c4c8e",
|
||||
},
|
||||
args: GetManyPostageReq{
|
||||
GoodsInfo: "[{\"goodsId\":\"WPS592_00019\",\"goodSpecId\":\"WPS592_1105165115160944\",\"num\":1},{\"goodsId\":\"WPS505_00007\",\"goodSpecId\":\"WPS505_1007111249857536\",\"num\":1}]",
|
||||
GoodsInfo: "[{\"goodsId\":\"WPS427_adf0008\",\"goodSpecId\":\"WPS427_0715110641454716\",\"num\":1}]",
|
||||
Province: "广东省",
|
||||
Address: "奥园",
|
||||
City: "广州市",
|
||||
@@ -80,14 +80,14 @@ func TestWeiPinShangClient_PreOrder(t *testing.T) {
|
||||
Key: "f654ea5bde7635c3f46191191e5c4c8e",
|
||||
},
|
||||
args: PreOrderReq{
|
||||
GoodsInfo: "[{\"goodsId\":\"WPS2_1231155626421463\",\"goodSpecId\":\"WPS2_12311556265677476\",\"num\":1}]",
|
||||
GoodsInfo: "[{\"goodsId\":\"WPS427_adf0008\",\"goodSpecId\":\"WPS427_0715110641454716\",\"num\":1}]",
|
||||
Province: "广东省",
|
||||
Address: "奥园",
|
||||
City: "广州市",
|
||||
Area: "番禺区",
|
||||
ConsigneePhone: "15375390426",
|
||||
ConsigneeContacts: "张三",
|
||||
LockCode: "L100000004",
|
||||
LockCode: "L100000005",
|
||||
Source: "AILEHUI",
|
||||
},
|
||||
},
|
||||
|
Reference in New Issue
Block a user