Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
search_server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
884b600c
authored
Jul 14, 2020
by
mushishixian
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
快手getdata方法
parent
02ea1cbe
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
170 additions
and
5 deletions
controller/quote_controller.go
model/quote.go
requests/quote_request.go
service/quote_service.go
service/transformer/quote_transformer.go
test/test.go
controller/quote_controller.go
View file @
884b600c
...
...
@@ -23,6 +23,7 @@ func QuoteIndex(c *gin.Context) {
if
err
:=
c
.
ShouldBind
(
&
r
);
err
!=
nil
{
fmt
.
Println
(
err
)
}
quoteService
:=
service
.
QuoteService
{}
//检查参数,有错误就返回错误
errCode
,
errMsg
:=
requests
.
CheckQuoteRequest
(
r
)
if
errCode
!=
0
{
...
...
@@ -32,7 +33,7 @@ func QuoteIndex(c *gin.Context) {
flag
:=
1
var
res
map
[
string
]
interface
{}
if
r
.
FollowHit
!=
0
&&
r
.
FollowHit
!=
1
{
res
,
_
=
s
ervice
.
GetGoodsBySupplier
(
r
)
res
,
_
=
quoteS
ervice
.
GetGoodsBySupplier
(
r
)
if
len
(
res
)
>
0
{
flag
=
0
}
...
...
@@ -50,7 +51,7 @@ func QuoteIndex(c *gin.Context) {
//mouser同步请求搜索
res
,
_
=
mouserOrg
(
r
.
Keyword
,
r
.
SupplierId
)
}
else
{
res
,
_
=
s
ervice
.
GetGoodsBySupplierName
(
r
)
res
,
_
=
quoteS
ervice
.
GetGoodsBySupplierName
(
r
)
}
if
len
(
res
)
>
0
{
flag
=
0
...
...
@@ -138,3 +139,10 @@ func GetDataPur(c *gin.Context) {
c
.
String
(
200
,
"ok"
)
return
}
//提供外链搜索接口
func
GetData
(
keyword
,
supplierName
string
)
(
res
interface
{})
{
quoteService
:=
service
.
QuoteService
{}
res
,
_
=
quoteService
.
GetData
(
keyword
,
supplierName
)
return
}
model/quote.go
0 → 100644
View file @
884b600c
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"`
}
requests/quote_request.go
View file @
884b600c
...
...
@@ -27,6 +27,11 @@ type QuoteIndexRequest struct {
K2
int
`form:"k2"`
}
type
QuoteGetDataRequest
struct
{
Keyword
string
`form:"keyword"`
SupplierName
string
`form:"supplier_name"`
}
func
CheckQuoteRequest
(
r
QuoteIndexRequest
)
(
errCode
int
,
errMsg
string
)
{
//解码参数
var
err
error
...
...
@@ -46,3 +51,4 @@ func CheckQuoteRequest(r QuoteIndexRequest) (errCode int, errMsg string) {
}
return
}
service/quote_service.go
View file @
884b600c
package
service
import
(
"encoding/json"
"fmt"
"github.com/imroc/req"
"github.com/syyongx/php2go"
.
"github.com/tidwall/gjson"
"search_server/model"
"search_server/pkg/common"
"search_server/pkg/es"
"search_server/requests"
"search_server/service/query"
"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
)
//获取查询条件
...
...
@@ -32,7 +41,7 @@ func GetGoodsBySupplier(r requests.QuoteIndexRequest) (result map[string]interfa
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
queryString
,
err
:=
query
.
GetGoodsBySupplierNameQuery
(
r
)
fmt
.
Println
(
supplierIndex
)
...
...
@@ -49,3 +58,62 @@ func GetGoodsBySupplierName(r requests.QuoteIndexRequest) (result map[string]int
result
[
"took"
]
=
Get
(
esResult
,
"took"
)
.
Int
()
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
}
service/transformer/quote_transformer.go
0 → 100644
View file @
884b600c
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
}
test/test.go
View file @
884b600c
...
...
@@ -5,7 +5,7 @@ import (
"fmt"
"github.com/syyongx/php2go"
"search_server/boot"
"search_server/
service
"
"search_server/
controller
"
)
func
StrRandom
(
lenNum
int
)
string
{
...
...
@@ -29,5 +29,6 @@ func main() {
if
err
:=
boot
.
Boot
(
path
);
err
!=
nil
{
fmt
.
Println
(
err
)
}
fmt
.
Println
(
controller
.
GetData
(
"lm358"
,
"arrow"
))
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment