You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.1 KiB

package jingtong
import (
"bytes"
"encoding/xml"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/url"
"recook/configs"
"strconv"
"strings"
)
const (
baseUrl = configs.Config_Jingtong_BaseUrl
token = configs.Config_Jingtong_Token
)
var (
Jingtong = &jingtong{}
client = &http.Client{}
)
type jingtong struct {
}
type GoodsPrice struct {
Code string `xml:"result"`
Price float32 `xml:"info>data_info>price"`
MarketPrice float32 `xml:"info>data_info>market_price"`
Store uint `xml:"info>data_info>store"`
IsShipping int `xml:"info>data_info>is_shipping"`
Products string `xml:"info>data_info>products"`
MemberPrice string `xml:"info>data_info>member_price"`
}
// @Style 基础工商信息
func (j *jingtong) Price(goodsId uint) (result GoodsPrice, err error) {
err = j.exec(url.Values{
"act": []string{"search_goods_price"},
"api_version": []string{"1.0"},
"token": []string{token},
"goods_id": []string{strconv.FormatUint(uint64(goodsId), 10)},
}, &result)
return
}
type GoodsInfo struct {
Code string `xml:"result"`
GoodsId uint `xml:"info>data_info>goods_id"`
LastModify int64 `xml:"info>data_info>last_modify"`
CatId uint `xml:"info>data_info>cat_id"`
CategoryName string `xml:"info>data_info>category_name"`
BrandId uint `xml:"info>data_info>brand_id"`
BrandName string `xml:"info>data_info>brand_name"`
Price float32 `xml:"info>data_info>price"`
Bn string `xml:"info>data_info>bn"`
Name string `xml:"info>data_info>name"`
Marketable int `xml:"info>data_info>marketable"`
Weight float32 `xml:"info>data_info>weight"`
Store uint `xml:"info>data_info>store"`
Score int `xml:"info>data_info>score"`
Uptime int64 `xml:"info>data_info>uptime"`
ImageDefault string `xml:"info>data_info>image_default"`
Intro string `xml:"info>data_info>intro"`
Type string `xml:"info>data_info>type"`
IsShipping int `xml:"info>data_info>is_shipping"`
DutyFree int `xml:"info>data_info>duty_free"`
Brief string `xml:"info>data_info>brief"`
LimitNum string `xml:"info>data_info>limit_num"`
WarehouseId uint `xml:"info>data_info>warehouse_id"`
CountryId uint `xml:"info>data_info>country_id"`
GoodsLink string `xml:"info>data_info>goods_link"`
Unit string `xml:"info>data_info>unit"`
CountryAbbreviation string `xml:"info>data_info>country_abbreviation"`
Warehouse string `xml:"info>data_info>warehouse"`
ImgUrl []string `xml:"info>data_info>img_url>item>img"`
ThumbUrl []string `xml:"info>data_info>thumb_url>item>img"`
ImgOriginal []string `xml:"info>data_info>img_original>item>img"`
Products string `xml:"info>data_info>products"`
}
// @Style 基础工商信息
func (j *jingtong) Info(goodsId uint) (result GoodsInfo, err error) {
err = j.exec(url.Values{
"act": []string{"search_goods_detail"},
"api_version": []string{"1.0"},
"token": []string{token},
"goods_id": []string{strconv.FormatUint(uint64(goodsId), 10)},
}, &result)
return
}
// @Style 执行请求
func (j *jingtong) exec(data url.Values, result interface{}) error {
res, err := RequestFormData(baseUrl, data, map[string]string{
"Content-Type": "multipart/form-data",
})
if err != nil {
return err
}
log.Println(string(res))
if err := xml.Unmarshal(res, result); err != nil {
return err
}
return nil
}
// @Style 请求
func request(method, url, data string, headers ...map[string]string) ([]byte, error) {
reqest, err := http.NewRequest(method, url, strings.NewReader(data))
if err != nil {
return nil, err
}
if len(headers) > 0 {
for key, value := range headers[0] {
reqest.Header.Add(key, value)
}
}
response, err := client.Do(reqest)
if err != nil {
return nil, err
}
defer response.Body.Close()
result, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return result, nil
}
// @Style formdata
func RequestFormData(url string, data url.Values, headers ...map[string]string) ([]byte, error) {
read := &bytes.Buffer{}
write := multipart.NewWriter(read)
for key, value := range data {
for _, val := range value {
write.WriteField(key, val)
}
}
write.Close()
reqest, err := http.NewRequest("POST", url, read)
if err != nil {
return nil, err
}
if len(headers) > 0 {
if _, ok := headers[0]["Content-Type"]; ok {
headers[0]["Content-Type"] = write.FormDataContentType()
} else {
reqest.Header.Add("Content-Type", write.FormDataContentType())
}
for key, value := range headers[0] {
reqest.Header.Add(key, value)
}
} else {
reqest.Header.Add("Content-Type", write.FormDataContentType())
}
response, err := client.Do(reqest)
if err != nil {
return nil, err
}
defer response.Body.Close()
result, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return result, nil
}