提交 b3599bd1 authored 作者: 屈传平's avatar 屈传平

hcw

上级 d4d5ed7f
...@@ -5,11 +5,6 @@ import ( ...@@ -5,11 +5,6 @@ import (
"github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/encoding/gjson"
) )
var Base = new(base)
type base struct {
}
type AddressReq struct { type AddressReq struct {
Source string `json:"source"` //渠道(aux,midea,suning) Source string `json:"source"` //渠道(aux,midea,suning)
Code string `json:"code"` //地址编码/上级地址编码 Code string `json:"code"` //地址编码/上级地址编码
...@@ -24,9 +19,9 @@ type AddressRes struct { ...@@ -24,9 +19,9 @@ type AddressRes struct {
SnId string `json:"snId"` SnId string `json:"snId"`
} }
func (s *refund) Address(ctx context.Context, req *AddressReq) (res []*AddressRes, err error) { func (s *hcwClient) Address(ctx context.Context, req *AddressReq) (res []*AddressRes, err error) {
var method = "/address" var method = "/address"
result, err := get(ctx, method, req) result, err := s.get(ctx, method, req)
if nil != err { if nil != err {
return return
} }
......
...@@ -23,8 +23,6 @@ import ( ...@@ -23,8 +23,6 @@ import (
"time" "time"
) )
var server *Config
const pkgName = "hcw" const pkgName = "hcw"
type Config struct { type Config struct {
...@@ -32,9 +30,14 @@ type Config struct { ...@@ -32,9 +30,14 @@ type Config struct {
ApiUrl string ApiUrl string
} }
func New(req *Config) { type hcwClient struct {
server = req Config *Config
return }
func New(req *Config) *hcwClient {
HcwClient := new(hcwClient)
HcwClient.Config = req
return HcwClient
} }
//const ( //const (
...@@ -47,7 +50,7 @@ type EncryptedResponse struct { ...@@ -47,7 +50,7 @@ type EncryptedResponse struct {
Signature string `json:"signature"` Signature string `json:"signature"`
} }
func EncryptWithSignature(data interface{}) (string, error) { func (s *hcwClient) EncryptWithSignature(data interface{}) (string, error) {
// 生成时间戳 // 生成时间戳
timestamp := time.Now().Unix() timestamp := time.Now().Unix()
...@@ -61,7 +64,7 @@ func EncryptWithSignature(data interface{}) (string, error) { ...@@ -61,7 +64,7 @@ func EncryptWithSignature(data interface{}) (string, error) {
signatureString := string(jsonData) + strconv.FormatInt(timestamp, 10) signatureString := string(jsonData) + strconv.FormatInt(timestamp, 10)
// 计算HMAC-SHA256签名 // 计算HMAC-SHA256签名
signature := calculateHMAC([]byte(server.SecretKey), signatureString) signature := calculateHMAC([]byte(s.Config.SecretKey), signatureString)
// 构建加密数据结构 // 构建加密数据结构
encryptedData := &EncryptedResponse{ encryptedData := &EncryptedResponse{
...@@ -77,7 +80,7 @@ func EncryptWithSignature(data interface{}) (string, error) { ...@@ -77,7 +80,7 @@ func EncryptWithSignature(data interface{}) (string, error) {
} }
// AES加密 // AES加密
block, err := aes.NewCipher([]byte(server.SecretKey)) block, err := aes.NewCipher([]byte(s.Config.SecretKey))
if err != nil { if err != nil {
return "", fmt.Errorf("创建加密器失败: %v", err) return "", fmt.Errorf("创建加密器失败: %v", err)
} }
...@@ -149,18 +152,18 @@ func mapToSortedQuery(params map[string]string) string { ...@@ -149,18 +152,18 @@ func mapToSortedQuery(params map[string]string) string {
return builder.String() return builder.String()
} }
func post(ctx context.Context, method string, req interface{}) (res string, err error) { func (s *hcwClient) post(ctx context.Context, method string, req interface{}) (res string, err error) {
Start := gtime.TimestampMilli() Start := gtime.TimestampMilli()
reqMap := gconv.Map(req) reqMap := gconv.Map(req)
param := gjson.New(reqMap) param := gjson.New(reqMap)
signature, err := EncryptWithSignature(reqMap) signature, err := s.EncryptWithSignature(reqMap)
if nil != err { if nil != err {
return return
} }
Url := server.ApiUrl + method Url := s.Config.ApiUrl + method
Request := g.Client() Request := g.Client()
Request.SetHeader("Content-Type", "text/xml; charset=utf-8") Request.SetHeader("Content-Type", "text/xml; charset=utf-8")
Request.SetHeader("Format", "json") Request.SetHeader("Format", "json")
...@@ -181,17 +184,17 @@ func post(ctx context.Context, method string, req interface{}) (res string, err ...@@ -181,17 +184,17 @@ func post(ctx context.Context, method string, req interface{}) (res string, err
return return
} }
func get(ctx context.Context, method string, req interface{}) (res string, err error) { func (s *hcwClient) get(ctx context.Context, method string, req interface{}) (res string, err error) {
Start := gtime.TimestampMilli() Start := gtime.TimestampMilli()
reqMap := gconv.Map(req) reqMap := gconv.Map(req)
param := gjson.New(reqMap) param := gjson.New(reqMap)
signature, err := EncryptWithSignature(reqMap) signature, err := s.EncryptWithSignature(reqMap)
if nil != err { if nil != err {
return return
} }
queryParams := getQueryParams(reqMap) queryParams := getQueryParams(reqMap)
query := mapToSortedQuery(queryParams) query := mapToSortedQuery(queryParams)
Url := server.ApiUrl + method + "?" + query Url := s.Config.ApiUrl + method + "?" + query
Request := g.Client() Request := g.Client()
Request.SetHeader("Content-Type", "text/xml; charset=utf-8") Request.SetHeader("Content-Type", "text/xml; charset=utf-8")
Request.SetHeader("Format", "json") Request.SetHeader("Format", "json")
......
...@@ -5,11 +5,6 @@ import ( ...@@ -5,11 +5,6 @@ import (
"github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/encoding/gjson"
) )
var Goods = new(goods)
type goods struct {
}
type GoodsListReq struct { type GoodsListReq struct {
Page string `json:"page"` //页数 Page string `json:"page"` //页数
PageSize string `json:"page_size"` //条数 PageSize string `json:"page_size"` //条数
...@@ -43,9 +38,9 @@ type GoodsListRes struct { ...@@ -43,9 +38,9 @@ type GoodsListRes struct {
} }
// 商品列表 // 商品列表
func (s *goods) List(ctx context.Context, req *GoodsListReq) (res *GoodsListRes, err error) { func (s *hcwClient) GoodsList(ctx context.Context, req *GoodsListReq) (res *GoodsListRes, err error) {
var method = "/api/v1/product" var method = "/api/v1/product"
result, err := get(ctx, method, req) result, err := s.get(ctx, method, req)
if nil != err { if nil != err {
return return
} }
...@@ -80,11 +75,11 @@ type GoodsDetailRes struct { ...@@ -80,11 +75,11 @@ type GoodsDetailRes struct {
} }
// 商品详情 // 商品详情
func (s *goods) Detail(ctx context.Context, productSn, source string) (res *GoodsDetailRes, err error) { func (s *hcwClient) GoodsDetail(ctx context.Context, productSn, source string) (res *GoodsDetailRes, err error) {
var method = "/api/v1/product/" + productSn var method = "/api/v1/product/" + productSn
req := &GoodsDetailReq{} req := &GoodsDetailReq{}
req.Source = source req.Source = source
result, err := get(ctx, method, req) result, err := s.get(ctx, method, req)
if nil != err { if nil != err {
return return
} }
......
...@@ -6,11 +6,6 @@ import ( ...@@ -6,11 +6,6 @@ import (
"github.com/gogf/gf/frame/g" "github.com/gogf/gf/frame/g"
) )
var Order = new(order)
type order struct {
}
type OrderCreateReq struct { type OrderCreateReq struct {
Order struct { Order struct {
Id int `json:"id"` Id int `json:"id"`
...@@ -60,9 +55,9 @@ type OrderCreateRes struct { ...@@ -60,9 +55,9 @@ type OrderCreateRes struct {
} `json:"data"` } `json:"data"`
} }
func (s *order) Create(ctx context.Context, req *OrderCreateReq) (res *OrderCreateRes, err error) { func (s *hcwClient) OrderCreate(ctx context.Context, req *OrderCreateReq) (res *OrderCreateRes, err error) {
var method = "/api/v1/order" var method = "/api/v1/order"
result, err := post(ctx, method, req) result, err := s.post(ctx, method, req)
if nil != err { if nil != err {
return return
} }
...@@ -104,9 +99,9 @@ type OrderListRes struct { ...@@ -104,9 +99,9 @@ type OrderListRes struct {
} `json:"data"` } `json:"data"`
} }
func (s *order) List(ctx context.Context, req *OrderCreateReq) (res *OrderListRes, err error) { func (s *hcwClient) OrderList(ctx context.Context, req *OrderCreateReq) (res *OrderListRes, err error) {
var method = "/api/v1/order" var method = "/api/v1/order"
result, err := get(ctx, method, req) result, err := s.get(ctx, method, req)
if nil != err { if nil != err {
return return
} }
...@@ -178,9 +173,9 @@ type OrderDetailRes struct { ...@@ -178,9 +173,9 @@ type OrderDetailRes struct {
} `json:"data"` } `json:"data"`
} }
func (s *order) Detail(ctx context.Context, orderSn string) (res *OrderDetailRes, err error) { func (s *hcwClient) OrderDetail(ctx context.Context, orderSn string) (res *OrderDetailRes, err error) {
var method = "/api/v1/order/" + orderSn var method = "/api/v1/order/" + orderSn
result, err := get(ctx, method, g.Map{}) result, err := s.get(ctx, method, g.Map{})
if nil != err { if nil != err {
return return
} }
...@@ -202,9 +197,9 @@ type OrderCheckRes struct { ...@@ -202,9 +197,9 @@ type OrderCheckRes struct {
} `json:"data"` } `json:"data"`
} }
func (s *order) Check(ctx context.Context, req *OrderCheckReq) (res *OrderCheckRes, err error) { func (s *hcwClient) OrderCheck(ctx context.Context, req *OrderCheckReq) (res *OrderCheckRes, err error) {
var method = "/api/v1/order/check" var method = "/api/v1/order/check"
result, err := post(ctx, method, req) result, err := s.post(ctx, method, req)
if nil != err { if nil != err {
return return
} }
......
...@@ -5,11 +5,6 @@ import ( ...@@ -5,11 +5,6 @@ import (
"github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/encoding/gjson"
) )
var Refund = new(refund)
type refund struct {
}
type RefundCreateReq struct { type RefundCreateReq struct {
RefundOrderNo string `json:"refund_order_no"` RefundOrderNo string `json:"refund_order_no"`
OrderSn string `json:"order_sn"` OrderSn string `json:"order_sn"`
...@@ -29,9 +24,9 @@ type RefundCreateReq struct { ...@@ -29,9 +24,9 @@ type RefundCreateReq struct {
type RefundCreateRes struct { type RefundCreateRes struct {
} }
func (s *refund) Create(ctx context.Context, req *RefundCreateReq) (res *RefundCreateRes, err error) { func (s *hcwClient) OrderRefundCreate(ctx context.Context, req *RefundCreateReq) (res *RefundCreateRes, err error) {
var method = "/api/v1/orderRefund" var method = "/api/v1/orderRefund"
result, err := post(ctx, method, req) result, err := s.post(ctx, method, req)
if nil != err { if nil != err {
return return
} }
...@@ -47,9 +42,9 @@ type RefundDetailReq struct { ...@@ -47,9 +42,9 @@ type RefundDetailReq struct {
type RefundDetailRes struct { type RefundDetailRes struct {
} }
func (s *refund) Detail(ctx context.Context, req *RefundDetailReq) (res *RefundDetailRes, err error) { func (s *hcwClient) OrderRefundDetail(ctx context.Context, req *RefundDetailReq) (res *RefundDetailRes, err error) {
var method = "/api/v1/orderRefund" var method = "/api/v1/orderRefund"
result, err := get(ctx, method, req) result, err := s.get(ctx, method, req)
if nil != err { if nil != err {
return return
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论