Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
bbe5e26e95 | ||
![]() |
158bb2542d | ||
![]() |
26bc6b3bcc | ||
![]() |
755c39089b | ||
![]() |
ace2872c2d | ||
![]() |
d2a1182c66 | ||
![]() |
f26c317144 | ||
![]() |
2835ad200b | ||
![]() |
8b245587bb | ||
![]() |
f5e8a4b990 |
4
go.mod
4
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
|
||||
|
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@@ -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)
|
||||
}
|
||||
|
@@ -160,19 +160,19 @@ type GoodsItem struct {
|
||||
|
||||
// 商品SKU信息
|
||||
type GoodsSkuItem struct {
|
||||
CFatherGoodsID string `json:"c_father_goods_id"` // 父级商品ID
|
||||
CGoodsID string `json:"c_goods_id"` // 商品ID
|
||||
CGoodsName string `json:"c_goods_name"` // 商品规格名称
|
||||
COriginalPrice string `json:"c_original_price"` // 商品市场价
|
||||
CInPrice string `json:"c_in_price"` // 商品进价
|
||||
CSalePrice string `json:"c_sale_price"` // 商品建议售价
|
||||
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"` // 最大购买数量
|
||||
CFatherGoodsID string `json:"c_father_goods_id"` // 父级商品ID
|
||||
CGoodsID string `json:"c_goods_id"` // 商品ID
|
||||
CGoodsName string `json:"c_goods_name"` // 商品规格名称
|
||||
COriginalPrice string `json:"c_original_price"` // 商品市场价
|
||||
CInPrice string `json:"c_in_price"` // 商品进价
|
||||
CSalePrice string `json:"c_sale_price"` // 商品建议售价
|
||||
CGoodsColor string `json:"c_goods_color"` // 商品颜色
|
||||
CGoodsSize string `json:"c_goods_size"` // 商品尺寸
|
||||
CGoodsImage string `json:"c_goods_image"` // 商品列表图片
|
||||
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"` // 商品市场价(吊牌价)
|
||||
}
|
||||
|
@@ -22,13 +22,15 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
GetGoodBrandURL = "mcang/Mcang/goodBrand" //查询商品的品牌
|
||||
GetGoodsClassifyURL = "mcang/Mcang/getGoodsClassify" //查询商品分类
|
||||
GetGoodsListURL = "mcang/Mcang/getGoodsList" //获取全部商品列表接口
|
||||
GetGoodsdeptURL = "mcang/Mcang/getGoodsdept" //(推荐使用)获取后台已选商品列表接口
|
||||
GetDetailsGoodsUrl = "mcang/Mcang/getDetailsGoods" //获取批量商品详情接口
|
||||
GetGoodsDetailsURL = "mcang/Mcang/getGoodsDetails" //获取单个商品详情接口
|
||||
GetGoodsStockURL = "mcang/Mcang/getGoodsStock" //获取单个商品详情接口
|
||||
GetGoodBrandURL = "mcang/Mcang/goodBrand" //查询商品的品牌
|
||||
GetGoodsClassifyURL = "mcang/Mcang/getGoodsClassify" //查询商品分类
|
||||
GetGoodsListURL = "mcang/Mcang/getGoodsList" //获取全部商品列表接口
|
||||
GetGoodsdeptURL = "mcang/Mcang/getGoodsdept" //(推荐使用)获取后台已选商品列表接口
|
||||
GetDetailsGoodsUrl = "mcang/Mcang/getDetailsGoods" //获取批量商品详情接口
|
||||
GetGoodsDetailsURL = "mcang/Mcang/getGoodsDetails" //获取单个商品详情接口
|
||||
GetGoodsStockURL = "mcang/Mcang/getGoodsStock" //获取单个商品详情接口
|
||||
GetSaleVenueURL = "mcang/Mcang/getSaleVenue" //获取会场列表接口
|
||||
GetSaleVenueGoodsURL = "mcang/Mcang/getSaleVenueGoods" //获取会场商品列表接口
|
||||
)
|
||||
|
||||
// Config 客户端配置
|
||||
|
@@ -25,11 +25,11 @@ type CreateOrderRes struct {
|
||||
Data []CreateOrderData `json:"data"` // 返回数据 data array
|
||||
}
|
||||
type CreateOrderData struct {
|
||||
ThirdOrderNo string `json:"thirdOrderNo"` // 第三方订单号 本地订单号
|
||||
OrderNo string `json:"orderNo"` // 主订单号
|
||||
McOrderNo string `json:"mcOrderNo"` //蜜仓子订单号
|
||||
OrderAmount float64 `json:"orderAmount"` // 子订单总金额
|
||||
Sku []SkuData `json:"sku"` // 订单商品信息
|
||||
ThirdOrderNo string `json:"thirdOrderNo"` // 第三方订单号 本地订单号
|
||||
OrderNo string `json:"orderNo"` // 主订单号
|
||||
McOrderNo string `json:"mcOrderNo"` //蜜仓子订单号
|
||||
OrderAmount interface{} `json:"orderAmount"` // 子订单总金额
|
||||
Sku []SkuData `json:"sku"` // 订单商品信息
|
||||
}
|
||||
|
||||
type SkuData struct {
|
||||
@@ -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 {
|
||||
|
Reference in New Issue
Block a user