Commit a71b2f30 by huangchengyi

1.0

parent 3b8428c7
package controller
import (
"github.com/gin-gonic/gin"
"search_server/middleware"
"search_server/model"
"search_server/pkg/common"
"search_server/service/ly"
)
//搜索型号
func GetDigikeyData(c *gin.Context) {
goodsName := middleware.REQUEST["k"] //关键词
common.PrintDebugHeader() //debug
if goodsName == "" {
common.Output(1001, "查询关键词不得为空", "")
return
}
//调外链拿数据
apiData := ly.OutLinkDigikey(&goodsName)
//供应商详情
lycon := new(ly.CommonLyService)
err := 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
...@@ -16,7 +16,7 @@ func GetMouserData(c *gin.Context) { ...@@ -16,7 +16,7 @@ func GetMouserData(c *gin.Context) {
common.PrintDebugHeader() //debug common.PrintDebugHeader() //debug
if goodsName == "" { if goodsName == "" {
c.JSON(200,"goods_name不得为空") common.Output(1001, "查询关键词不得为空", "")
return return
} }
...@@ -35,9 +35,5 @@ func GetMouserData(c *gin.Context) { ...@@ -35,9 +35,5 @@ func GetMouserData(c *gin.Context) {
} }
common.PrintDebugHtml("-----执行结束----") //debug common.PrintDebugHtml("-----执行结束----") //debug
c.JSON(200, common.BomResponse{ common.Output(errCode, "查询成功", "")
ErrCode: errCode,
ErrMsg: "查询成功",
Data: "",
})
} }
...@@ -21,6 +21,8 @@ func InitRouter() *gin.Engine { ...@@ -21,6 +21,8 @@ func InitRouter() *gin.Engine {
r.POST("search/ZiYing/index", controller.Index) r.POST("search/ZiYing/index", controller.Index)
r.POST("search/mouser/a", controller.GetMouserData) r.POST("search/mouser/a", controller.GetMouserData)
r.GET("search/mouser/a", controller.GetMouserData) r.GET("search/mouser/a", controller.GetMouserData)
r.POST("search/digikey/a", controller.GetDigikeyData)
r.GET("search/digikey/a", controller.GetDigikeyData)
//bom相关 //bom相关
r.POST("/search/bom/autospu", controller.AutoSpu) r.POST("/search/bom/autospu", controller.AutoSpu)
......
package ly
import (
"github.com/imroc/req"
"github.com/tidwall/gjson"
"search_server/middleware"
"search_server/model"
"search_server/pkg/common"
"strconv"
"strings"
"time"
)
//mouser外链网址
const digikey_api_url string = "https://api.mouser.com/api/v1/search/partnumber?apiKey=0a11fa6f-ddcb-4ddf-9947-e42b2f3b4723"
//mouser请求外链
func OutLinkDigikey(goodsName *string) map[string]*model.LyClearGoodsList {
if *goodsName == "" {
return nil
}
var result string;
if middleware.REQUEST["flags"] == "-1" { //原始数据调试
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\"}}"))
if err != nil {
print(Mouser_api_url)
print(err)
}
result = resp.String(); //请求外链拿到结果
}
common.PrintDebugHtml("原始数据:"+result)
productList := make(map[string]*model.LyClearGoodsList,0)
apiGoodsList := gjson.Get(result, "SearchResults.Parts").Array()
for _, goods := range apiGoodsList {
goodsSn := goods.Get("MouserPartNumber").String()
ladderPrice := make([]*model.TierItem, 0)
priceTemp := make([]interface{}, 0)
//拼接价格梯度
apiPriceTi := goods.Get("PriceBreaks").Array()
var apiLowerPrice float64 = 0; //计算最低价格
for _,priceItem := range apiPriceTi{
priceItemStr := priceItem.String();
//价格转换
onePrice := gjson.Get(priceItemStr, "Price").String()
onePrice = strings.ReplaceAll(strings.Trim(onePrice,"$"),",","")
skuPrice,_ := strconv.ParseFloat(onePrice,64) //转成float64
//数量转换
quantity := gjson.Get(priceItemStr, "Quantity").Uint()
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,
})
}
//计算库存
tempStock := goods.Get("Availability").String()
stock := strings.Trim(strings.ReplaceAll(tempStock," 有庫存","")," ")
var Mystock int64 = 0 ;
if stock != "" {
Mystock = common.MyInt64(stock);
}
//拼接联营数据
LyClearGoodsList := model.LyClearGoodsList{
GoodsName: goods.Get("ManufacturerPartNumber").String(),
BrandName: goods.Get("Manufacturer").String(),
Desc: goods.Get("Description").String(),
GoodsSn: goodsSn,
Docurl: goods.Get("DataSheetUrl").String(),
Url: goods.Get("ProductDetailUrl").String(),
GoodsImg: goods.Get("ImagePath").String(),
Cat: goods.Get("Category").String(),
Increment: goods.Get("Mult").Int(),
RestrictionMessage: goods.Get("RestrictionMessage").String(),
Stock:Mystock, //库存
Moq:goods.Get("Min").Int(), //最低起订量
SinglePrice: apiLowerPrice,
Tiered:ladderPrice,
PriceTemp: priceTemp,
}
productList[goodsSn] = &LyClearGoodsList
}
//fmt.Println(productList)
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