Commit 5348da38 by huangchengyi

1.0

parent 14e3d0a3
......@@ -25,16 +25,10 @@ func GetDigikeyData(c *gin.Context) {
//供应商详情
lycon := new(ly.CommonLyService)
err := lycon.GetSkuByGoodsSn(apiData, &model.SUPPLIER_REDIS_INFO{
lycon.GetSkuByGoodsSn(apiData, &model.SUPPLIER_REDIS_INFO{
SupplierId: 14,
SupplierNickname: "mouser",
})
errCode := 0
if !err {
errCode = 1
}
common.PrintDebugHtml("-----执行结束----") //debug
common.Output(errCode, "查询成功", "")
}
\ No newline at end of file
package controller
import (
"github.com/gin-gonic/gin"
"search_server/middleware"
"search_server/model"
"search_server/pkg/common"
"search_server/service/ly"
)
//搜索型号
func GetTmeData(c *gin.Context) {
goodsName := middleware.REQUEST["k"] //关键词
common.PrintDebugHeader() //debug
if goodsName == "" {
common.Output(1001, "查询关键词不得为空", "")
return
}
//调外链拿数据
apiData := ly.OutLinkTme(&goodsName)
//供应商详情
lycon := new(ly.CommonLyService)
lycon.GetSkuByGoodsSn(apiData, &model.SUPPLIER_REDIS_INFO{
SupplierId: 4,
SupplierNickname: "tme",
})
common.PrintDebugHtml("-----执行结束----") //debug
}
\ No newline at end of file
package common
import (
"crypto/hmac"
"crypto/md5"
crand "crypto/rand"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
......@@ -21,6 +24,20 @@ import (
)
/*
sha1加密(tme目前在用)
@param string query 查询字符串
@param string pri_key 加密字符串
@return 返回加密后的字符串
*/
func Sha1(query string, pri_key string) string {
key := []byte(pri_key)
mac := hmac.New(sha1.New, key)
mac.Write([]byte(query))
query = base64.StdEncoding.EncodeToString(mac.Sum(nil))
return query
}
/*
输出header
*/
func PrintDebugHeader() {
......
......@@ -27,6 +27,8 @@ func InitRouter() *gin.Engine {
r.GET("search/mouser/a", controller.GetMouserData)
r.POST("search/digikey/a", controller.GetDigikeyData)
r.GET("search/digikey/a", controller.GetDigikeyData)
r.POST("search/tme/a", controller.GetTmeData)
r.GET("search/tme/a", controller.GetTmeData)
//bom相关
r.POST("/search/bom/autospu", controller.AutoSpu)
......
......@@ -11,7 +11,7 @@ import (
"time"
)
//mouser外链网址
const Mouser_api_url string = "https://api.mouser.com/api/v1/search/partnumber?apiKey=0a11fa6f-ddcb-4ddf-9947-e42b2f3b4723"
const mouser_api_url string = "https://api.mouser.com/api/v1/search/partnumber?apiKey=0a11fa6f-ddcb-4ddf-9947-e42b2f3b4723"
//mouser请求外链
func OutLinkMouser(goodsName *string) map[string]*model.LyClearGoodsList {
......@@ -24,9 +24,9 @@ func OutLinkMouser(goodsName *string) map[string]*model.LyClearGoodsList {
result = `{"Errors":[],"SearchResults":{"NumberOfResult":80,"Parts":[{"Availability":"15242 有庫存","DataSheetUrl":"","Description":"運算放大器 - 運放器 1.2 MHz industry standard dual-channel amplifier with -40C to 85C operation 8-SOIC -40 to 85","FactoryStock":"0","ImagePath":"https://www.mouser.com/images/texasinstruments/images/ITP_TI_SOIC-8_D_t.jpg","Category":"運算放大器 - 運放器","LeadTime":"42 日數","LifecycleStatus":"New Product","Manufacturer":"Texas Instruments","ManufacturerPartNumber":"LM358BIDR","Min":"1","Mult":"1","MouserPartNumber":"595-LM358BIDR","ProductAttributes":[{"AttributeName":"封裝","AttributeValue":"Cut Tape"},{"AttributeName":"封裝","AttributeValue":"MouseReel"},{"AttributeName":"封裝","AttributeValue":"Reel"},{"AttributeName":"標準包裝數量","AttributeValue":"2500"}],"PriceBreaks":[{"Quantity":1,"Price":"$0.44","Currency":"USD"},{"Quantity":10,"Price":"$0.287","Currency":"USD"},{"Quantity":100,"Price":"$0.154","Currency":"USD"}],"AlternatePackagings":null,"ProductDetailUrl":"https://www.mouser.hk/ProductDetail/Texas-Instruments/LM358BIDR?qs=byeeYqUIh0MUqg09TCRNgA%3D%3D","Reeling":true,"ROHSStatus":"RoHS Compliant","SuggestedReplacement":"","MultiSimBlue":0,"ProductCompliance":[{"ComplianceName":"CNHTS","ComplianceValue":"8542339000"},{"ComplianceName":"USHTS","ComplianceValue":"8542330001"},{"ComplianceName":"TARIC","ComplianceValue":"8542330000"},{"ComplianceName":"ECCN","ComplianceValue":"EAR99"}]}]}}`;
}else{
req.SetTimeout(10 * time.Second)
resp, err := req.Post(Mouser_api_url,req.BodyJSON("{\"SearchByPartRequest\":{\"mouserPartNumber\":\""+*goodsName+"\",\"partSearchOptions\":\"string\"}}"))
resp, err := req.Post(mouser_api_url,req.BodyJSON("{\"SearchByPartRequest\":{\"mouserPartNumber\":\""+*goodsName+"\",\"partSearchOptions\":\"string\"}}"))
if err != nil {
print(Mouser_api_url)
print(mouser_api_url)
print(err)
}
result = resp.String(); //请求外链拿到结果
......
package ly
import (
"github.com/imroc/req"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"search_server/middleware"
"search_server/model"
"search_server/pkg/common"
"strconv"
"time"
)
//tme 类
type tme struct {
}
//tme外链网址
const tme_api_url_product string = "https://api.tme.eu/Products/Search.json" //'Products/Search' 查询商品 或者 'Products/GetPricesAndStocks' 查询价格
const tme_api_url_stock string = "https://api.tme.eu/Products/GetPricesAndStocks.json" //'Products/Search' 查询商品 或者 'Products/GetPricesAndStocks' 查询价格
const tme_token string = "3381c9d5d9663be68eb69ea3ce00af3a2b5789094c7cd488a4"//密钥
const tme_app_secret string = "a1d8647fa38710140628" //账号
//tme请求外链
func OutLinkTme(goodsName *string) map[string]*model.LyClearGoodsList {
if *goodsName == "" {
return nil
}
var (
result string //请求外链原始结果
SymbolList string //外链原始数据的goods_sn
);
if middleware.REQUEST["flags"] == "-1" { //原始数据调试
result = `{"Status":"OK","Data":{"ProductList":[{"Symbol":"0402WGF1000TCE","CustomerSymbol":"","OriginalSymbol":"0402WGF1000TCE","Producer":"ROYAL OHM","Description":"Resistor: thick film; SMD; 0402; 100\u03a9; 63mW; \u00b11%; -55\u00f7155\u00b0C","CategoryId":"100578","Category":"0402 SMD resistors","Photo":"\/\/ce8dc832c.cloudimg.io\/fit\/640x480\/n@4760e4647bc7d903de9205a51f321b69cdbed098\/_cdn_\/D9\/C2\/B0\/00\/0\/732317_1.jpg?mark_url=_tme-wrk_%2Ftme_new_render3d.png&mark_pos=center&mark_size=100pp","Thumbnail":"\/\/ce8dc832c.cloudimg.io\/fit\/100x75\/webp@3ec3fbcb8427a46b791b093cd6e191b483328d8a\/_cdn_\/D9\/C2\/B0\/00\/0\/732317_1.jpg","Weight":0.01,"WeightUnit":"g","SuppliedAmount":0,"MinAmount":10000,"Multiples":10000,"ProductStatusList":[],"Unit":"pcs","ProductInformationPage":"\/\/www.tme.eu\/en\/details\/0402wgf1000tce\/0402-smd-resistors\/royal-ohm\/","Guarantee":null,"OfferId":null},{"Symbol":"SMD0402-100R-1%","CustomerSymbol":"","OriginalSymbol":"0402WGF1000TCE","Producer":"ROYAL OHM","Description":"Resistor: thick film; SMD; 0402; 100\u03a9; 63mW; \u00b11%; -55\u00f7155\u00b0C","CategoryId":"100578","Category":"0402 SMD resistors","Photo":"\/\/ce8dc832c.cloudimg.io\/fit\/640x480\/n@4760e4647bc7d903de9205a51f321b69cdbed098\/_cdn_\/D9\/C2\/B0\/00\/0\/732317_1.jpg?mark_url=_tme-wrk_%2Ftme_new_render3d.png&mark_pos=center&mark_size=100pp","Thumbnail":"\/\/ce8dc832c.cloudimg.io\/fit\/100x75\/webp@3ec3fbcb8427a46b791b093cd6e191b483328d8a\/_cdn_\/D9\/C2\/B0\/00\/0\/732317_1.jpg","Weight":0.006,"WeightUnit":"g","SuppliedAmount":0,"MinAmount":100,"Multiples":100,"ProductStatusList":[],"Unit":"pcs","ProductInformationPage":"\/\/www.tme.eu\/en\/details\/smd0402-100r-1%\/0402-smd-resistors\/royal-ohm\/0402wgf1000tce\/","Guarantee":null,"OfferId":null}],"Amount":2,"PageNumber":1,"CategoryList":{"111000":2,"100299":2,"100300":2,"100578":2,"112309":2}}}`;
}else{
params1 := "Country=US&Currency=USD&Language=EN&SearchPlain="+*goodsName+"&Token="+tme_token
param := "POST"+"&"+php2go.Rawurlencode(tme_api_url_product)+"&"+php2go.Rawurlencode(params1)
common.PrintDebugHtml(param)
sign := common.Sha1(param,tme_app_secret) //生成签名
req.SetTimeout(10 * time.Second)
req.Debug = true
postParam := req.Param{
"SearchPlain":*goodsName,
"Country":"US",
"Currency":"USD",
"Language":"EN",
"Token":tme_token,
"ApiSignature":sign,
}
resp, err := req.Post(tme_api_url_product,postParam)
if err != nil {
print(tme_api_url_product)
print(err)
}
result = resp.String(); //请求外链拿到结果
}
common.PrintDebugHtml("原始数据:"+result)
productList := make(map[string]*model.LyClearGoodsList,0)
if gjson.Get(result,"Status").String() != "OK" {
common.PrintDebugHtml("请求出错-没有数据")
return nil
}
apiGoodsList := gjson.Get(result, "Data.ProductList").Array()
i := 0;
SymbolArr := make([]string,0)
for _, goods := range apiGoodsList {
goodsSn := goods.Get("Symbol").String()
SymbolList = SymbolList+"&"+php2go.HTTPBuildQuery(map[string][]string{
"SymbolList["+strconv.Itoa(i)+"]":[]string{goodsSn},
});
SymbolArr = append(SymbolArr,goodsSn)
i ++;
var increment int64 = 1;
if goods.Get("Multiples").Int() > 0 {
increment = goods.Get("Multiples").Int()
}
var moq int64 = 1;
if goods.Get("MinAmount").Int() > 0 {
moq = goods.Get("MinAmount").Int()
}
//拼接联营数据
LyClearGoodsList := model.LyClearGoodsList{
GoodsName: goods.Get("OriginalSymbol").String(),
BrandName: goods.Get("Producer").String(),
Desc: goods.Get("Description").String(),
GoodsSn: goodsSn,
Docurl: goods.Get("DataSheetUrl").String(),
Url: goods.Get("ProductInformationPage").String(),
GoodsImg: goods.Get("Photo").String(),
Increment: increment,
Moq:moq, //最低起订量
}
productList[goodsSn] = &LyClearGoodsList
}
//查询价格库存
params_s2 := "Country=US&Currency=USD&Language=EN"+SymbolList+"&Token="+tme_token
param_s3 := "POST"+"&"+php2go.Rawurlencode(tme_api_url_stock)+"&"+php2go.Rawurlencode(params_s2)
common.PrintDebugHtml(param_s3)
req.SetTimeout(10 * time.Second)
req.Debug = true
postParam2 := req.Param{
"Country":"US",
"Currency":"USD",
"Language":"EN",
"Token":tme_token,
//"SymbolList":[]string{"0402WGF1000TCE","SMD0402-100R-1%"},
"SymbolList":SymbolArr,
"ApiSignature":common.Sha1(param_s3,tme_app_secret),
}
common.PrintDebugHtml(postParam2)
resp2, err := req.Post(tme_api_url_stock,req.BodyJSON(postParam2))
if err != nil {
print(tme_api_url_stock)
print(err)
}
result_s2 := resp2.String()
if gjson.Get(result_s2,"Status").String() != "OK" {
common.PrintDebugHtml("请求出错-价格库存:"+result_s2)
return nil
}
priceData := gjson.Get(result, "Data.ProductList").Array()
for _, info := range priceData {
ladderPrice := make([]*model.TierItem, 0)
priceTemp := make([]interface{}, 0)
//拼接价格梯度
apiPriceTi := info.Get("PriceList").Array()
var apiLowerPrice float64 = 0; //计算最低价格
for _,priceItem := range apiPriceTi{
priceItemStr := priceItem.String();
skuPrice := gjson.Get(priceItemStr, "PriceValue").Float()
quantity := gjson.Get(priceItemStr, "Amount").Int()
if apiLowerPrice == 0 {
apiLowerPrice = skuPrice
}else if apiLowerPrice > skuPrice {
apiLowerPrice = skuPrice
}
ladder := model.TierItem{
Purchases: quantity,
PriceUs: skuPrice,
PriceCn: 0,
}
ladderPrice = append(ladderPrice, &ladder)
//梯度缓存数据
priceTemp = append(priceTemp,[]interface{}{
quantity,
skuPrice,
})
}
goodsSn := info.Get("Symbol").String()
productList[goodsSn].Stock = info.Get("Amount").Int() //库存
productList[goodsSn].Tiered = ladderPrice //梯度价格
productList[goodsSn].PriceTemp = priceTemp //梯度价格2
productList[goodsSn].SinglePrice = apiLowerPrice //最低价格
}
return productList
}
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
_ "encoding/base64"
_ "github.com/syyongx/php2go"
)
func Sha1(query string, pri_key string) string {
key := []byte(pri_key)
mac := hmac.New(sha1.New, key)
mac.Write([]byte(query))
query = base64.StdEncoding.EncodeToString(mac.Sum(nil))
return query
}
func main() {
//println(php2go.Sha1("123456"+"123456"))
ss := Sha1("POST&https%3A%2F%2Fapi.tme.eu%2FProducts%2FSearch.json&Country%3DUS%26Currency%3DUSD%26Language%3DEN%26SearchPlain%3DRMCF0402FT12R0%26Token%3D3381c9d5d9663be68eb69ea3ce00af3a2b5789094c7cd488a4","a1d8647fa38710140628")
println(ss)
}
package main
import (
"fmt"
"sort"
)
// 按map的key排序
func main() {
params := make(map[string]interface{})
params["name"] = "xxx"
params["age"] = 23
params["sex"] = 0
params["level"] = 1
// 1.取出所有的key
var keys []string
for k := range params{
keys = append(keys, k)
}
// 对字符串切片排序
sort.Strings(keys)
fmt.Println(keys)
// 打印key, val
for _, k := range keys{
fmt.Printf("key: %v val:%v \n", k, params[k])
}
}
package test
import (
"fmt"
"github.com/imroc/req"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"search_server/middleware"
"search_server/model"
"search_server/pkg/common"
"strconv"
"time"
)
//tme 类
type tme struct {
}
//tme外链网址
const tme_api_url_product string = "https://api.tme.eu/Products/Search.json" //'Products/Search' 查询商品 或者 'Products/GetPricesAndStocks' 查询价格
const tme_api_url_stock string = "https://api.tme.eu/Products/GetPricesAndStocks.json" //'Products/Search' 查询商品 或者 'Products/GetPricesAndStocks' 查询价格
const tme_token string = "3381c9d5d9663be68eb69ea3ce00af3a2b5789094c7cd488a4"//密钥
const tme_app_secret string = "a1d8647fa38710140628" //账号
//tme请求外链
func OutLinkTme(goodsName *string) map[string]*model.LyClearGoodsList {
if *goodsName == "" {
return nil
}
var (
result string //请求外链原始结果
SymbolList string //外链原始数据的goods_sn
);
if middleware.REQUEST["flags"] == "-1" { //原始数据调试
result = `{"Status":"OK","Data":{"ProductList":[{"Symbol":"0402WGF1000TCE","CustomerSymbol":"","OriginalSymbol":"0402WGF1000TCE","Producer":"ROYAL OHM","Description":"Resistor: thick film; SMD; 0402; 100\u03a9; 63mW; \u00b11%; -55\u00f7155\u00b0C","CategoryId":"100578","Category":"0402 SMD resistors","Photo":"\/\/ce8dc832c.cloudimg.io\/fit\/640x480\/n@4760e4647bc7d903de9205a51f321b69cdbed098\/_cdn_\/D9\/C2\/B0\/00\/0\/732317_1.jpg?mark_url=_tme-wrk_%2Ftme_new_render3d.png&mark_pos=center&mark_size=100pp","Thumbnail":"\/\/ce8dc832c.cloudimg.io\/fit\/100x75\/webp@3ec3fbcb8427a46b791b093cd6e191b483328d8a\/_cdn_\/D9\/C2\/B0\/00\/0\/732317_1.jpg","Weight":0.01,"WeightUnit":"g","SuppliedAmount":0,"MinAmount":10000,"Multiples":10000,"ProductStatusList":[],"Unit":"pcs","ProductInformationPage":"\/\/www.tme.eu\/en\/details\/0402wgf1000tce\/0402-smd-resistors\/royal-ohm\/","Guarantee":null,"OfferId":null},{"Symbol":"SMD0402-100R-1%","CustomerSymbol":"","OriginalSymbol":"0402WGF1000TCE","Producer":"ROYAL OHM","Description":"Resistor: thick film; SMD; 0402; 100\u03a9; 63mW; \u00b11%; -55\u00f7155\u00b0C","CategoryId":"100578","Category":"0402 SMD resistors","Photo":"\/\/ce8dc832c.cloudimg.io\/fit\/640x480\/n@4760e4647bc7d903de9205a51f321b69cdbed098\/_cdn_\/D9\/C2\/B0\/00\/0\/732317_1.jpg?mark_url=_tme-wrk_%2Ftme_new_render3d.png&mark_pos=center&mark_size=100pp","Thumbnail":"\/\/ce8dc832c.cloudimg.io\/fit\/100x75\/webp@3ec3fbcb8427a46b791b093cd6e191b483328d8a\/_cdn_\/D9\/C2\/B0\/00\/0\/732317_1.jpg","Weight":0.006,"WeightUnit":"g","SuppliedAmount":0,"MinAmount":100,"Multiples":100,"ProductStatusList":[],"Unit":"pcs","ProductInformationPage":"\/\/www.tme.eu\/en\/details\/smd0402-100r-1%\/0402-smd-resistors\/royal-ohm\/0402wgf1000tce\/","Guarantee":null,"OfferId":null}],"Amount":2,"PageNumber":1,"CategoryList":{"111000":2,"100299":2,"100300":2,"100578":2,"112309":2}}}`;
}else{
params1 := "Country=US&Currency=USD&Language=EN&SearchPlain="+*goodsName+"&Token="+tme_token
param := "POST"+"&"+php2go.Rawurlencode(tme_api_url_product)+"&"+php2go.Rawurlencode(params1)
common.PrintDebugHtml(param)
sign := common.Sha1(param,tme_app_secret) //生成签名
postParam := params1+"&ApiSignature="+php2go.URLEncode(sign);
common.PrintDebugHtml(postParam)
req.SetTimeout(30 * time.Second)
//resp, err := req.Post(params2,postParam)
req.Debug = true
ss := req.Param{
"SearchPlain":*goodsName,
"Country":"US",
"Currency":"USD",
"Language":"EN",
"Token":"3381c9d5d9663be68eb69ea3ce00af3a2b5789094c7cd488a4",
"ApiSignature":sign,
}
resp, err := req.Post(tme_api_url_product,ss)
if err != nil {
print(tme_api_url_product)
print(err)
}
result = resp.String(); //请求外链拿到结果
}
common.PrintDebugHtml("原始数据:"+result)
productList := make(map[string]*model.LyClearGoodsList,0)
if gjson.Get(result,"Status").String() != "OK" {
common.PrintDebugHtml("请求出错-没有数据")
return nil
}
apiGoodsList := gjson.Get(result, "Data.ProductList").Array()
i := 0;
for _, goods := range apiGoodsList {
goodsSn := goods.Get("Symbol").String()
if i == 0 {
SymbolList = SymbolList+"&SymbolList[0]="+goodsSn
}else{
SymbolList = SymbolList+"&SymbolList["+strconv.Itoa(i)+"]"+goodsSn
}
i ++;
var increment int64 = 1;
if goods.Get("Multiples").Int() > 0 {
increment = goods.Get("Multiples").Int()
}
var moq int64 = 1;
if goods.Get("MinAmount").Int() > 0 {
moq = goods.Get("MinAmount").Int()
}
//拼接联营数据
LyClearGoodsList := model.LyClearGoodsList{
GoodsName: goods.Get("OriginalSymbol").String(),
BrandName: goods.Get("Producer").String(),
Desc: goods.Get("Description").String(),
GoodsSn: goodsSn,
Docurl: goods.Get("DataSheetUrl").String(),
Url: goods.Get("ProductInformationPage").String(),
GoodsImg: goods.Get("Photo").String(),
Increment: increment,
Moq:moq, //最低起订量
}
productList[goodsSn] = &LyClearGoodsList
}
//查询价格库存
params1 := "Country=US&Currency=USD&Language=EN&SymbolList="+SymbolList+"&Token="+tme_token
param := "POST"+"&"+php2go.Rawurlencode(tme_api_url_stock)+php2go.Rawurlencode(params1)
sign := common.Sha1(param,tme_app_secret) //生成签名
req.SetTimeout(10 * time.Second)
resp, err := req.Post(tme_api_url_stock,req.BodyJSON(params1+"&ApiSignature"+sign))
if err != nil {
print(tme_api_url_stock)
print(err)
}
result = resp.String(); //请求外链拿到结果
fmt.Println(result,SymbolList)
if gjson.Get(result,"Status").String() != "OK" {
common.PrintDebugHtml("请求出错-价格库存")
return nil
}
priceData := gjson.Get(result, "Data.ProductList").Array()
for _, info := range priceData {
ladderPrice := make([]*model.TierItem, 0)
priceTemp := make([]interface{}, 0)
//拼接价格梯度
apiPriceTi := info.Get("PriceList").Array()
var apiLowerPrice float64 = 0; //计算最低价格
for _,priceItem := range apiPriceTi{
priceItemStr := priceItem.String();
skuPrice := gjson.Get(priceItemStr, "PriceValue").Float()
quantity := gjson.Get(priceItemStr, "Amount").Int()
if apiLowerPrice == 0 {
apiLowerPrice = skuPrice
}else if apiLowerPrice > skuPrice {
apiLowerPrice = skuPrice
}
ladder := model.TierItem{
Purchases: quantity,
PriceUs: skuPrice,
PriceCn: 0,
}
ladderPrice = append(ladderPrice, &ladder)
//梯度缓存数据
priceTemp = append(priceTemp,[]interface{}{
quantity,
skuPrice,
})
}
goodsSn := info.Get("Symbol").String()
productList[goodsSn].Stock = info.Get("Amount").Int() //库存
productList[goodsSn].Tiered = ladderPrice //梯度价格
productList[goodsSn].PriceTemp = priceTemp //梯度价格2
productList[goodsSn].SinglePrice = apiLowerPrice //最低价格
}
return productList
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment