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

hcw

上级 79e83209
package main
func main() {
}
package hcw
import (
"context"
"github.com/gogf/gf/encoding/gjson"
)
var Base = new(base)
type base struct {
}
type AddressReq struct {
Source string `json:"source"` //渠道(aux,midea,suning)
Code string `json:"code"` //地址编码/上级地址编码
RegionType string `json:"region_type"` //地址类型source为aux时必传(1-一级地址、2-二级地址、3-三级地址、4-四级地址)
}
type AddressRes struct {
Id string `json:"id"`
Level string `json:"level"`
Name string `json:"name"`
PId string `json:"pId"`
SnId string `json:"snId"`
}
func (s *refund) Address(ctx context.Context, req *AddressReq) (res []*AddressRes, err error) {
var method = "/address"
result, err := get(ctx, method, req)
if nil != err {
return
}
_ = gjson.New(result).Scan(&res)
return
}
......@@ -16,7 +16,10 @@ import (
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
"io"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
......@@ -25,8 +28,7 @@ var server *Config
const pkgName = "hcw"
type Config struct {
AppId string
AppSecret string
SecretKey string
ApiUrl string
}
......@@ -35,9 +37,9 @@ func New(req *Config) {
return
}
const (
secretKey = "b828a6aa972bcbe5e1e52ce4f24c467f" // 必须32字节
)
//const (
// secretKey = "b828a6aa972bcbe5e1e52ce4f24c467f" // 必须32字节
//)
type EncryptedResponse struct {
Data []byte `json:"data"`
......@@ -59,7 +61,7 @@ func EncryptWithSignature(data interface{}) (string, error) {
signatureString := string(jsonData) + strconv.FormatInt(timestamp, 10)
// 计算HMAC-SHA256签名
signature := calculateHMAC([]byte(secretKey), signatureString)
signature := calculateHMAC([]byte(server.SecretKey), signatureString)
// 构建加密数据结构
encryptedData := &EncryptedResponse{
......@@ -75,7 +77,7 @@ func EncryptWithSignature(data interface{}) (string, error) {
}
// AES加密
block, err := aes.NewCipher([]byte(secretKey))
block, err := aes.NewCipher([]byte(server.SecretKey))
if err != nil {
return "", fmt.Errorf("创建加密器失败: %v", err)
}
......@@ -113,6 +115,40 @@ func pkcs7Pad(data []byte, blockSize int) []byte {
return append(data, padtext...)
}
func getQueryParams(queryParamsTemp map[string]interface{}) map[string]string {
queryParams := map[string]string{}
for k, v := range queryParamsTemp {
queryParams[k] = gconv.String(v)
}
return queryParams
}
// 处理 interface{} 类型的值
func mapToSortedQuery(params map[string]string) string {
if len(params) == 0 {
return ""
}
keys := make([]string, 0, len(params))
for k := range params {
keys = append(keys, k)
}
sort.Strings(keys)
var builder strings.Builder
for i, k := range keys {
if i > 0 {
builder.WriteByte('&')
}
builder.WriteString(url.QueryEscape(k))
builder.WriteByte('=')
builder.WriteString(url.QueryEscape(params[k]))
}
return builder.String()
}
func post(ctx context.Context, method string, req interface{}) (res string, err error) {
Start := gtime.TimestampMilli()
......@@ -124,7 +160,7 @@ func post(ctx context.Context, method string, req interface{}) (res string, err
return
}
Url := server.ApiUrl
Url := server.ApiUrl + method
Request := g.Client()
Request.SetHeader("Content-Type", "text/xml; charset=utf-8")
Request.SetHeader("Format", "json")
......@@ -147,25 +183,24 @@ func post(ctx context.Context, method string, req interface{}) (res string, err
func get(ctx context.Context, method string, req interface{}) (res string, err error) {
Start := gtime.TimestampMilli()
reqMap := gconv.Map(req)
param := gjson.New(reqMap)
signature, err := EncryptWithSignature(reqMap)
if nil != err {
return
}
Url := server.ApiUrl
queryParams := getQueryParams(reqMap)
query := mapToSortedQuery(queryParams)
Url := server.ApiUrl + method + "?" + query
Request := g.Client()
Request.SetHeader("Content-Type", "text/xml; charset=utf-8")
Request.SetHeader("Format", "json")
Request.SetHeader("signature", signature)
resp, err := Request.Timeout(time.Second*10).Get(Url, param.MustToJsonString())
resp, err := Request.Timeout(time.Second * 10).Get(Url)
defer func() {
_ = resp.Close()
ctx = context.WithValue(ctx, "Method", "POST")
ctx = context.WithValue(ctx, "Method", "GET")
ctx = context.WithValue(ctx, "URI", Url)
if err != nil {
g.Log().Ctx(ctx).Cat(pkgName).Cat("error").Infof("参数【%v】错误【%v】响应时间【%v ms】", param.MustToJsonString(), err.Error(), gtime.TimestampMilli()-Start)
......
package hcw
import (
"context"
"github.com/gogf/gf/encoding/gjson"
)
var Goods = new(goods)
type goods struct {
}
type GoodsListReq struct {
Page string `json:"page"` //页数
PageSize string `json:"page_size"` //条数
Source string `json:"source"` //商品来源
}
type GoodsListRes struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
List []struct {
Id int `json:"id"`
ProductNo string `json:"product_no"`
Source string `json:"source"`
PlatNo interface{} `json:"plat_no"`
Name string `json:"name"`
BrandNo int `json:"brand_no"`
CategoryNo int `json:"category_no"`
Price string `json:"price"`
Stock int `json:"stock"`
Image string `json:"image"`
Images []string `json:"images"`
Content string `json:"content"`
Status int `json:"status"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
DeletedAt interface{} `json:"deleted_at"`
} `json:"list"`
Total int `json:"total"`
} `json:"data"`
}
// 商品列表
func (s *goods) List(ctx context.Context, req *GoodsListReq) (res *GoodsListRes, err error) {
var method = "/api/v1/product"
result, err := get(ctx, method, req)
if nil != err {
return
}
_ = gjson.New(result).Scan(&res)
return
}
type GoodsDetailReq struct {
Source string `json:"source"` //商品来源
}
type GoodsDetailRes struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Id int `json:"id"`
ProductNo string `json:"product_no"`
Source string `json:"source"`
PlatNo interface{} `json:"plat_no"`
Name string `json:"name"`
BrandNo int `json:"brand_no"`
CategoryNo int `json:"category_no"`
Price string `json:"price"`
Stock int `json:"stock"`
Image string `json:"image"`
Images []string `json:"images"`
Content string `json:"content"`
Status int `json:"status"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
} `json:"data"`
}
// 商品详情
func (s *goods) Detail(ctx context.Context, productSn, source string) (res *GoodsDetailRes, err error) {
var method = "/api/v1/product/" + productSn
req := &GoodsDetailReq{}
req.Source = source
result, err := get(ctx, method, req)
if nil != err {
return
}
_ = gjson.New(result).Scan(&res)
return
}
package hcw
import (
"context"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/frame/g"
)
var Order = new(order)
type order struct {
}
type OrderCreateReq struct {
Order struct {
Id int `json:"id"`
ParentOrderSn string `json:"parent_order_sn"`
OrderSn string `json:"order_sn"`
ThirdOrderSn string `json:"third_order_sn"`
CreatedTime string `json:"created_time"`
OrderPayTime string `json:"order_pay_time"`
DispatchPrice int `json:"dispatch_price"`
OrderTotalPrice int `json:"order_total_price"`
Remark string `json:"remark"`
RealName string `json:"real_name"`
Mobile string `json:"mobile"`
Provice string `json:"provice"`
City string `json:"city"`
District string `json:"district"`
Street string `json:"street"`
Address string `json:"address"`
ProvinceId int `json:"province_id"`
CityId int `json:"city_id"`
DistrictId int `json:"district_id"`
StreetId int `json:"street_id"`
SupplyType string `json:"supply_type"`
} `json:"order"`
OrderGoodsDetail []*OrderGoodsDetailList `json:"order_goods_detail"`
}
type OrderGoodsDetailList struct {
Id int `json:"id"`
GoodsOrderSn string `json:"goods_order_sn"`
GoodsId int `json:"goods_id"`
Total int `json:"total"`
CreatedTime int `json:"created_time"`
Thumb string `json:"thumb"`
Title string `json:"title"`
GoodsPrice int `json:"goods_price"`
GoodsOptionId int `json:"goods_option_id"`
GoodsOptionTitle string `json:"goods_option_title"`
GoodsCode string `json:"goods_code"`
}
type OrderCreateRes struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
OrderId int `json:"order_id"`
} `json:"data"`
}
func (s *order) Create(ctx context.Context, req *OrderCreateReq) (res *OrderCreateRes, err error) {
var method = "/api/v1/order"
result, err := post(ctx, method, req)
if nil != err {
return
}
_ = gjson.New(result).Scan(&res)
return
}
type OrderListReq struct {
Page string `json:"page"` //页数
PageSize string `json:"page_size"` //条数
}
type OrderListRes struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
List []struct {
Id int `json:"id"`
OrderSn string `json:"order_sn"`
ThirdOrderSn string `json:"third_order_sn"`
ThreeOrderSn string `json:"three_order_sn"`
OrderPrice string `json:"order_price"`
OrderDiscountPrice string `json:"order_discount_price"`
OrderFreightPrice string `json:"order_freight_price"`
OrderPayPrice string `json:"order_pay_price"`
OrderPayTime string `json:"order_pay_time"`
OrderFinishTime interface{} `json:"order_finish_time"`
OrderCancelTime interface{} `json:"order_cancel_time"`
OrderSendTime interface{} `json:"order_send_time"`
OrderType int `json:"order_type"`
OrderStatus int `json:"order_status"`
OrderPayStatus int `json:"order_pay_status"`
SupplyType string `json:"supply_type"`
OrderRemark string `json:"order_remark"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
} `json:"list"`
Total int `json:"total"`
} `json:"data"`
}
func (s *order) List(ctx context.Context, req *OrderCreateReq) (res *OrderListRes, err error) {
var method = "/api/v1/order"
result, err := get(ctx, method, req)
if nil != err {
return
}
_ = gjson.New(result).Scan(&res)
return
}
type OrderDetailRes struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Id int `json:"id"`
OrderSn string `json:"order_sn"`
ThirdOrderSn string `json:"third_order_sn"`
ThreeOrderSn string `json:"three_order_sn"`
OrderPrice string `json:"order_price"`
OrderDiscountPrice string `json:"order_discount_price"`
OrderFreightPrice string `json:"order_freight_price"`
OrderPayPrice string `json:"order_pay_price"`
OrderPayTime string `json:"order_pay_time"`
OrderFinishTime interface{} `json:"order_finish_time"`
OrderCancelTime interface{} `json:"order_cancel_time"`
OrderSendTime interface{} `json:"order_send_time"`
OrderType int `json:"order_type"`
OrderStatus int `json:"order_status"`
OrderPayStatus int `json:"order_pay_status"`
SupplyType string `json:"supply_type"`
OrderRemark string `json:"order_remark"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
DeletedAt interface{} `json:"deleted_at"`
OrderGoods struct {
OrderGoodsId int `json:"order_goods_id"`
OrderSn string `json:"order_sn"`
PlatOrderSn interface{} `json:"plat_order_sn"`
SupplyType string `json:"supply_type"`
GoodsCode string `json:"goods_code"`
GoodsName string `json:"goods_name"`
GoodsNum int `json:"goods_num"`
GoodsPrice string `json:"goods_price"`
GoodsBuyPrice string `json:"goods_buy_price"`
GoodsPayPrice string `json:"goods_pay_price"`
GoodsSkuName string `json:"goods_sku_name"`
GoodsSkuNo string `json:"goods_sku_no"`
GoodsImage string `json:"goods_image"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
DeletedAt interface{} `json:"deleted_at"`
} `json:"order_goods"`
OrderAddress struct {
OrderAddressId int `json:"order_address_id"`
OrderSn string `json:"order_sn"`
ReceiveName string `json:"receive_name"`
ReceiveMobile string `json:"receive_mobile"`
ReceiveAddress string `json:"receive_address"`
ReceiveProvinceId string `json:"receive_province_id"`
ReceiveCityId string `json:"receive_city_id"`
ReceiveAreaId string `json:"receive_area_id"`
ReceiverStreetId string `json:"receiver_street_id"`
ReceiveProvinceName string `json:"receive_province_name"`
ReceiveCityName string `json:"receive_city_name"`
ReceiveAreaName string `json:"receive_area_name"`
ReceiveStreetName string `json:"receive_street_name"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
DeletedAt interface{} `json:"deleted_at"`
} `json:"order_address"`
OrderLogistics interface{} `json:"order_logistics"`
} `json:"data"`
}
func (s *order) Detail(ctx context.Context, orderSn string) (res *OrderDetailRes, err error) {
var method = "/api/v1/order/" + orderSn
result, err := get(ctx, method, g.Map{})
if nil != err {
return
}
_ = gjson.New(result).Scan(&res)
return
}
type OrderCheckReq struct {
GoodsId int `json:"goods_id"`
BuyNum int `json:"buy_num"`
SupplyType string `json:"supply_type"`
}
type OrderCheckRes struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
OrderId int `json:"order_id"`
} `json:"data"`
}
func (s *order) Check(ctx context.Context, req *OrderCheckReq) (res *OrderCheckRes, err error) {
var method = "/api/v1/order/check"
result, err := post(ctx, method, req)
if nil != err {
return
}
_ = gjson.New(result).Scan(&res)
return
}
package hcw
import (
"context"
"github.com/gogf/gf/encoding/gjson"
)
var Refund = new(refund)
type refund struct {
}
type RefundCreateReq struct {
RefundOrderNo string `json:"refund_order_no"`
OrderSn string `json:"order_sn"`
RefundType int `json:"refund_type"`
RefundReason string `json:"refund_reason"`
RefundReasonType int `json:"refund_reason_type"`
RefundRemark string `json:"refund_remark"`
RefundAddressMerchant string `json:"refund_address_merchant"`
RefundVoucher []string `json:"refund_voucher"`
RefundPrice int `json:"refund_price"`
RefundGoods struct {
OrderGoodsCode string `json:"order_goods_code"`
RefundGoodsNum int `json:"refund_goods_num"`
} `json:"refund_goods"`
}
type RefundCreateRes struct {
}
func (s *refund) Create(ctx context.Context, req *RefundCreateReq) (res *RefundCreateRes, err error) {
var method = "/api/v1/orderRefund"
result, err := post(ctx, method, req)
if nil != err {
return
}
_ = gjson.New(result).Scan(&res)
return
}
type RefundDetailReq struct {
OrderSn string `json:"order_sn"`
RefundOrderNo string `json:"refund_order_no"`
}
type RefundDetailRes struct {
}
func (s *refund) Detail(ctx context.Context, req *RefundDetailReq) (res *RefundDetailRes, err error) {
var method = "/api/v1/orderRefund"
result, err := get(ctx, method, req)
if nil != err {
return
}
_ = gjson.New(result).Scan(&res)
return
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论