92 lines
2.8 KiB
Go
92 lines
2.8 KiB
Go
package showapi
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
)
|
|
|
|
type ProductInfo struct {
|
|
OriginalName string
|
|
OriginalSpec string
|
|
Name string
|
|
SpecQuality int
|
|
SpecUnit string
|
|
Unit string
|
|
Weight float32
|
|
Price int
|
|
Categories []string
|
|
ManName string // 生产商
|
|
BrandName string
|
|
}
|
|
|
|
var (
|
|
specPat *regexp.Regexp
|
|
specUnitMap = map[string]string{
|
|
"克": "g",
|
|
"千克": "kg",
|
|
"升": "L",
|
|
"毫升": "ml",
|
|
}
|
|
)
|
|
|
|
func init() {
|
|
specPat = regexp.MustCompile(`(\d+)([^\/]+)(?:\/(.+))?`)
|
|
}
|
|
|
|
func SplitProductSpec(spec string) (specQuality int, specUnit, Unit string) {
|
|
searchResult := specPat.FindStringSubmatch(spec)
|
|
// baseapi.SugarLogger.Debug(utils.Format4Output(searchResult, false))
|
|
if len(searchResult) == 4 {
|
|
return int(utils.Str2Int64WithDefault(searchResult[1], 0)), searchResult[2], searchResult[3]
|
|
}
|
|
return 0, "", ""
|
|
}
|
|
|
|
func CalculateWeight(specQuality int, specUnit string) (weight float32) {
|
|
weight = float32(specQuality)
|
|
if specUnit == "g" || specUnit == "ml" {
|
|
weight /= 1000
|
|
}
|
|
return weight
|
|
}
|
|
|
|
func (a *API) GetProductInfoByBarCode(barCode string) (product *ProductInfo, err error) {
|
|
res := ShowapiRequest("http://route.showapi.com/66-22", a.appID, a.appSecret)
|
|
res.AddTextPara("code", barCode)
|
|
result, err := res.Post()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var jsonResult map[string]interface{}
|
|
if err = utils.UnmarshalUseNumber([]byte(result), &jsonResult); err != nil {
|
|
return nil, err
|
|
}
|
|
showApiResCode := int(utils.MustInterface2Int64(jsonResult["showapi_res_code"]))
|
|
if showApiResCode != 0 {
|
|
return nil, utils.NewErrorIntCode(utils.Interface2String(jsonResult["showapi_res_error"]), showApiResCode)
|
|
}
|
|
// baseapi.SugarLogger.Debug(utils.Format4Output(jsonResult, false))
|
|
|
|
showapiResBody := jsonResult["showapi_res_body"].(map[string]interface{})
|
|
flag := showapiResBody["flag"].(bool)
|
|
if !flag {
|
|
return nil, utils.NewErrorIntCode(utils.Interface2String(showapiResBody["remark"]), int(utils.Str2Int64(utils.Interface2String(showapiResBody["ret_code"]))))
|
|
}
|
|
product = &ProductInfo{
|
|
OriginalName: showapiResBody["goodsName"].(string),
|
|
OriginalSpec: showapiResBody["spec"].(string),
|
|
Price: int(math.Round(utils.Str2Float64WithDefault(utils.Interface2String(showapiResBody["price"]), 0))),
|
|
Categories: strings.Split(showapiResBody["goodsType"].(string), ">>"),
|
|
ManName: showapiResBody["manuName"].(string),
|
|
BrandName: showapiResBody["trademark"].(string),
|
|
}
|
|
product.SpecQuality, product.SpecUnit, product.Unit = SplitProductSpec(product.OriginalSpec)
|
|
product.Name = utils.TrimBlankChar(strings.Replace(product.OriginalName, fmt.Sprintf("%d%s", product.SpecQuality, product.SpecUnit), "", -1))
|
|
product.Weight = CalculateWeight(product.SpecQuality, product.SpecUnit)
|
|
return product, err
|
|
}
|