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

wandian

上级 d50d8663
......@@ -23,6 +23,7 @@ const (
Tmv3 = 20 //天猫优选
Suning = 21 //苏宁有货
Yonghui = 22 //永辉
Wandian = 24 //万店通
)
var (
......@@ -116,6 +117,10 @@ func GetUpstreamList() (res interface{}, err error) {
"key": Yonghui,
"name": GetUpstreamName(Yonghui),
},
g.Map{
"key": Wandian,
"name": GetUpstreamName(Wandian),
},
}
return
}
......@@ -158,6 +163,8 @@ func GetUpstreamName(source int) string {
return "苏宁易购"
case Yonghui:
return "永辉"
case Wandian:
return "万店通"
default:
return "未知来源"
}
......
package wandian
import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
"net/url"
"sort"
"strings"
"time"
"unicode/utf8"
)
var server *Config
//万店
const pkgName = "wandian"
type Config struct {
Sid string
Appkey string
AppSecret string
ApiUrl string
}
func New(req *Config) {
server = req
return
}
func packData(req map[string]interface{}) string {
// 1. 提取并排序键名
keys := make([]string, 0, len(req))
for k := range req {
if k == "sign" {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
// 2. 使用 strings.Builder 高效拼接字符串
var builder strings.Builder
// 3. 遍历排序后的键
for i, key := range keys {
val := req[key]
// 非首个元素时添加分隔符
if i > 0 {
builder.WriteString(";")
}
// 4. 处理键部分
keyLen := utf8.RuneCountInString(key)
builder.WriteString(fmt.Sprintf("%02d-", keyLen))
builder.WriteString(key)
builder.WriteString(":")
// 5. 处理值部分
valLen := utf8.RuneCountInString(gconv.String(val))
builder.WriteString(fmt.Sprintf("%04d-", valLen))
builder.WriteString(gconv.String(val))
}
return builder.String()
}
// 带错误处理的增强版本
func makeSignSecure(req map[string]interface{}, appSecret string) map[string]interface{} {
data := packData(req)
if data == "" {
return req
}
combined := data + appSecret
hash := md5.Sum([]byte(combined))
req["sign"] = hex.EncodeToString(hash[:])
return req
}
//除空数组
func deleteZeroValues(req map[string]interface{}) map[string]interface{} {
param := g.Map{}
for k, v := range req {
if gconv.String(v) != "" {
param[k] = v
}
}
return param
}
func post(ctx context.Context, method string, req interface{}) (res string, err error) {
Start := gtime.TimestampMilli()
reqMap := gconv.Map(req)
reqMap["sid"] = server.Sid
reqMap["appkey"] = server.Appkey
reqMap["timestamp"] = time.Now().Unix()
reqMap = deleteZeroValues(reqMap)
reqMap = makeSignSecure(reqMap, server.AppSecret)
param := gjson.New(reqMap)
Url := server.ApiUrl + method
Request := g.Client()
Request.SetHeader("Content-Type", "application/x-www-form-urlencoded")
Request.SetHeader("Content-length", fmt.Sprintf("%d", len(param.MustToJsonString())))
postData := url.Values{}
for k, v := range param.Map() {
postData.Add(k, gconv.String(v))
}
encoded := postData.Encode()
resp, err := Request.Timeout(time.Second*10).Post(Url, encoded)
defer func() {
_ = resp.Close()
ctx = context.WithValue(ctx, "Method", "POST")
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)
} else {
g.Log().Ctx(ctx).Cat(pkgName).Infof("参数【%v】响应【%v】响应时间【%v ms】", param.MustToJsonString(), res, gtime.TimestampMilli()-Start)
}
}()
res = resp.ReadAllString()
return
}
package wandian
import (
"context"
"github.com/gogf/gf/encoding/gjson"
)
var Goods = new(goods)
type goods struct {
}
type GoodsListReq struct {
SpecNo string `json:"spec_no"`
GoodsNo string `json:"goods_no"`
BrandNo string `json:"brand_no"`
ClassName string `json:"class_name"`
Barcode string `json:"barcode"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
PageSize string `json:"page_size"`
PageNo string `json:"page_no"`
}
type GoodsListRes struct {
Code int `json:"code"`
Message string `json:"message"`
TotalCount string `json:"total_count"`
GoodsList []struct {
GoodsId string `json:"goods_id"`
GoodsNo string `json:"goods_no"`
GoodsName string `json:"goods_name"`
ShortName string `json:"short_name"`
Alias string `json:"alias"`
GoodsType string `json:"goods_type"`
SpecCount string `json:"spec_count"`
Pinyin string `json:"pinyin"`
BrandNo string `json:"brand_no"`
BrandName string `json:"brand_name"`
Remark string `json:"remark"`
Prop1 string `json:"prop1"`
Prop2 string `json:"prop2"`
Prop3 string `json:"prop3"`
Prop4 string `json:"prop4"`
Prop5 string `json:"prop5"`
Prop6 string `json:"prop6"`
Origin string `json:"origin"`
ClassName string `json:"class_name"`
ClassId string `json:"class_id"`
UnitName interface{} `json:"unit_name"`
AuxUnitName interface{} `json:"aux_unit_name"`
FlagName interface{} `json:"flag_name"`
GoodsCreated string `json:"goods_created"`
GoodsModified string `json:"goods_modified"`
SpecList []struct {
SpecId string `json:"spec_id"`
GoodsId string `json:"goods_id"`
SpecNo string `json:"spec_no"`
SpecCode string `json:"spec_code"`
Barcode string `json:"barcode"`
SpecName string `json:"spec_name"`
LowestPrice string `json:"lowest_price"`
RetailPrice string `json:"retail_price"`
WholesalePrice string `json:"wholesale_price"`
MemberPrice string `json:"member_price"`
MarketPrice string `json:"market_price"`
CustomPrice1 string `json:"custom_price1"`
CustomPrice2 string `json:"custom_price2"`
SaleScore string `json:"sale_score"`
PackScore string `json:"pack_score"`
PickScore string `json:"pick_score"`
ValidityDays string `json:"validity_days"`
SalesDays string `json:"sales_days"`
ReceiveDays string `json:"receive_days"`
Weight string `json:"weight"`
Length string `json:"length"`
Width string `json:"width"`
Height string `json:"height"`
IsSnEnable string `json:"is_sn_enable"`
IsAllowNegStock string `json:"is_allow_neg_stock"`
IsNotNeedExamine string `json:"is_not_need_examine"`
IsZeroCost string `json:"is_zero_cost"`
IsLowerCost string `json:"is_lower_cost"`
IsNotUseAir string `json:"is_not_use_air"`
TaxRate string `json:"tax_rate"`
LargeType string `json:"large_type"`
Remark string `json:"remark"`
SpecCreated string `json:"spec_created"`
SpecModified string `json:"spec_modified"`
Prop1 string `json:"prop1"`
Prop2 string `json:"prop2"`
Prop3 string `json:"prop3"`
Prop4 string `json:"prop4"`
Prop5 string `json:"prop5"`
Prop6 string `json:"prop6"`
ImgUrl string `json:"img_url"`
SpecAuxUnitName interface{} `json:"spec_aux_unit_name"`
SpecUnitName interface{} `json:"spec_unit_name"`
} `json:"spec_list"`
} `json:"goods_list"`
}
/**
商品列表
*/
func (s *goods) List(ctx context.Context, req *GoodsListReq) (res *GoodsListRes, err error) {
result, err := post(ctx, "/openapi2/goods_query.php", req)
if nil != err {
return
}
err = gjson.New(result).Scan(&res)
return
}
差异被折叠。
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论