Commit 884b600c by mushishixian

快手getdata方法

parent 02ea1cbe
...@@ -23,6 +23,7 @@ func QuoteIndex(c *gin.Context) { ...@@ -23,6 +23,7 @@ func QuoteIndex(c *gin.Context) {
if err := c.ShouldBind(&r); err != nil { if err := c.ShouldBind(&r); err != nil {
fmt.Println(err) fmt.Println(err)
} }
quoteService := service.QuoteService{}
//检查参数,有错误就返回错误 //检查参数,有错误就返回错误
errCode, errMsg := requests.CheckQuoteRequest(r) errCode, errMsg := requests.CheckQuoteRequest(r)
if errCode != 0 { if errCode != 0 {
...@@ -32,7 +33,7 @@ func QuoteIndex(c *gin.Context) { ...@@ -32,7 +33,7 @@ func QuoteIndex(c *gin.Context) {
flag := 1 flag := 1
var res map[string]interface{} var res map[string]interface{}
if r.FollowHit != 0 && r.FollowHit != 1 { if r.FollowHit != 0 && r.FollowHit != 1 {
res, _ = service.GetGoodsBySupplier(r) res, _ = quoteService.GetGoodsBySupplier(r)
if len(res) > 0 { if len(res) > 0 {
flag = 0 flag = 0
} }
...@@ -50,7 +51,7 @@ func QuoteIndex(c *gin.Context) { ...@@ -50,7 +51,7 @@ func QuoteIndex(c *gin.Context) {
//mouser同步请求搜索 //mouser同步请求搜索
res, _ = mouserOrg(r.Keyword, r.SupplierId) res, _ = mouserOrg(r.Keyword, r.SupplierId)
} else { } else {
res, _ = service.GetGoodsBySupplierName(r) res, _ = quoteService.GetGoodsBySupplierName(r)
} }
if len(res) > 0 { if len(res) > 0 {
flag = 0 flag = 0
...@@ -138,3 +139,10 @@ func GetDataPur(c *gin.Context) { ...@@ -138,3 +139,10 @@ func GetDataPur(c *gin.Context) {
c.String(200, "ok") c.String(200, "ok")
return return
} }
//提供外链搜索接口
func GetData(keyword, supplierName string) (res interface{}) {
quoteService := service.QuoteService{}
res, _ = quoteService.GetData(keyword, supplierName)
return
}
package model
//采集返回的商品数据结构
type CollectData struct {
HasNext bool `json:"hasNext"`
Data []struct {
GoodsSn string `json:"goods_sn"`
GoodsName string `json:"goods_name"`
ProviderName string `json:"provider_name"`
ProviderURL string `json:"provider_url"`
PdfURL string `json:"pdf_url"`
GoodsBrief string `json:"goods_brief"`
GoodsDesc string `json:"goods_desc"`
GoodsThumb string `json:"goods_thumb"`
GoodsImg string `json:"goods_img"`
URL string `json:"url"`
Attributes []string`json:"attributes"`
Stock int `json:"stock"`
Prices []struct {
Purchases int `json:"purchases"`
Price float64 `json:"price"`
} `json:"prices"`
MOQ int `json:"MOQ"`
Increment int `json:"increment"`
GoodsID string `json:"goods_id"`
From string `json:"from"`
} `json:"data"`
PageToken string `json:"pageToken"`
Total int `json:"total"`
DataType string `json:"dataType"`
AppCode string `json:"appCode"`
}
...@@ -27,6 +27,11 @@ type QuoteIndexRequest struct { ...@@ -27,6 +27,11 @@ type QuoteIndexRequest struct {
K2 int `form:"k2"` K2 int `form:"k2"`
} }
type QuoteGetDataRequest struct {
Keyword string `form:"keyword"`
SupplierName string `form:"supplier_name"`
}
func CheckQuoteRequest(r QuoteIndexRequest) (errCode int, errMsg string) { func CheckQuoteRequest(r QuoteIndexRequest) (errCode int, errMsg string) {
//解码参数 //解码参数
var err error var err error
...@@ -46,3 +51,4 @@ func CheckQuoteRequest(r QuoteIndexRequest) (errCode int, errMsg string) { ...@@ -46,3 +51,4 @@ func CheckQuoteRequest(r QuoteIndexRequest) (errCode int, errMsg string) {
} }
return return
} }
package service package service
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/imroc/req"
"github.com/syyongx/php2go"
. "github.com/tidwall/gjson" . "github.com/tidwall/gjson"
"search_server/model"
"search_server/pkg/common"
"search_server/pkg/es" "search_server/pkg/es"
"search_server/requests" "search_server/requests"
"search_server/service/query" "search_server/service/query"
"search_server/service/transformer" "search_server/service/transformer"
"strconv"
) )
type QuoteService struct {
}
//根据供应商获取商品信息 //根据供应商获取商品信息
func GetGoodsBySupplier(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) { func (qs *QuoteService) GetGoodsBySupplier(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) {
//索引字典 //索引字典
supplierIndex := query.GetSupplierIndex(r.SupplierId) supplierIndex := query.GetSupplierIndex(r.SupplierId)
//获取查询条件 //获取查询条件
...@@ -32,7 +41,7 @@ func GetGoodsBySupplier(r requests.QuoteIndexRequest) (result map[string]interfa ...@@ -32,7 +41,7 @@ func GetGoodsBySupplier(r requests.QuoteIndexRequest) (result map[string]interfa
return return
} }
func GetGoodsBySupplierName(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) { func (qs *QuoteService) GetGoodsBySupplierName(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) {
supplierIndex := r.SupplierName supplierIndex := r.SupplierName
queryString, err := query.GetGoodsBySupplierNameQuery(r) queryString, err := query.GetGoodsBySupplierNameQuery(r)
fmt.Println(supplierIndex) fmt.Println(supplierIndex)
...@@ -49,3 +58,62 @@ func GetGoodsBySupplierName(r requests.QuoteIndexRequest) (result map[string]int ...@@ -49,3 +58,62 @@ func GetGoodsBySupplierName(r requests.QuoteIndexRequest) (result map[string]int
result["took"] = Get(esResult, "took").Int() result["took"] = Get(esResult, "took").Int()
return return
} }
//获取采集那边的数据
func (qs *QuoteService) GetData(keyword, supplier string) (res interface{}, err error) {
collectData, err := qs.GetCollectData(keyword, supplier)
if err != nil {
return
}
quoteTransformer := transformer.QuoteTransformer{}
res = quoteTransformer.TransformCollectData(keyword, supplier, collectData)
return
}
//去采集那边的接口获取数据
func (qs *QuoteService) GetCollectData(keyword, supplierName string) (collectData model.CollectData, err error) {
portSet01 := []int{8084, 8084, 8084, 8084, 8085, 8086, 8086, 8086, 8086, 8087}
portSet02 := []int{8084, 8084, 8084, 8084, 8086, 8086, 8086, 8086}
portSet03 := []int{8086}
portSet04 := []int{8087}
supplierNamePortSetMap := map[string][]int{
"vnet": portSet03,
"arrow": portSet03,
"verical": portSet03,
"mouser": portSet02,
"rs": portSet02,
"rochester": portSet02,
"master": portSet04,
"aipco": portSet01,
"alliedelec": portSet01,
"buerklin": portSet01,
"digikey": portSet01,
"chip1stop": portSet01,
"element14": portSet01,
"tme": portSet01,
}
ip := "http://39.108.51.147"
key, exist := supplierNamePortSetMap[supplierName]
if !exist {
return
}
var index int
if len(key) == 1 {
index = 0
} else {
index = common.Rand(0, len(key)-1)
}
port := key[index]
portStr := strconv.Itoa(port)
keyword = php2go.URLEncode(keyword)
url := ip + ":" + portStr + "/elec/" + supplierName + "?kw=" + keyword
resp, err := req.Get(url)
if err != nil {
return
}
err = json.Unmarshal(resp.Bytes(), &collectData)
if err != nil {
return
}
return
}
package transformer
import (
"search_server/model"
"search_server/pkg/common"
)
type QuoteTransformer struct {
}
//针对GetUrl返回的数据进行数据转换
func (qt *QuoteTransformer) TransformCollectData(keyword, supplierName string, collectData model.CollectData) (transformedData map[string]interface{}) {
transformedData = make(map[string]interface{})
if len(collectData.Data) > 0 {
for _, data := range collectData.Data {
item := make(map[string]interface{})
if data.GoodsName == "" || data.GoodsSn == "" || data.ProviderName == "" {
continue
}
item["goods_name"] = data.GoodsName
item["goods_sn"] = data.GoodsSn
item["url"] = data.URL
goodsNameTemp := common.CaseInsensitiveReplace(data.GoodsName, keyword, "<b class='f-red'>"+keyword+"</b>")
item["goods_name_temp"] = goodsNameTemp
item["desc"] = data.GoodsDesc
item["displayName"] = supplierName
item["brand_name"] = data.ProviderName
item["docurl"] = data.PdfURL
item["goods_img"] = data.PdfURL
item["increment"] = data.Increment
item["single_price"] = 0
var tiered []model.LadderPrice
if len(data.Prices) > 0 {
for _, price := range data.Prices {
//获取正式格式的阶梯价
var ladderPrice model.LadderPrice
ladderPrice.Purchases = price.Purchases
ladderPrice.PriceUs = price.Price
tiered = append(tiered, ladderPrice)
item["single_price"] = price.Price
}
}
item["tiered"] = tiered
item["price_temp"] = data.Prices
item["stock"] = []int{data.MOQ, data.Stock}
transformedData[data.GoodsSn] = item
}
}
return
}
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"github.com/syyongx/php2go" "github.com/syyongx/php2go"
"search_server/boot" "search_server/boot"
"search_server/service" "search_server/controller"
) )
func StrRandom(lenNum int) string { func StrRandom(lenNum int) string {
...@@ -29,5 +29,6 @@ func main() { ...@@ -29,5 +29,6 @@ func main() {
if err := boot.Boot(path); err != nil { if err := boot.Boot(path); err != nil {
fmt.Println(err) fmt.Println(err)
} }
fmt.Println(controller.GetData("lm358","arrow"))
} }
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