Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
LC_soapi
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
fbf2e8b4
authored
Jan 09, 2020
by
杨树贤
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
临时推送
parent
902c9278
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
215 additions
and
28 deletions
conf/app.ini
go.mod
go.sum
main.go
pkg/app/response.go
pkg/common/function.go
pkg/es/elastic.go
pkg/setting/data.go
pkg/setting/setting.go
server/hrpc/self.go
server/http/self.go
service/self.go
conf/app.ini
View file @
fbf2e8b4
...
...
@@ -29,5 +29,10 @@ IdleTimeout = 200
DBNum
=
0
[log]
LogPath
=
'logs/'
LogChanSize
=
1000
\ No newline at end of file
LogPath
=
logs/
LogChanSize
=
1000
[es]
#猎芯自营索引键,sz是 liexin_zixing_test 线上是 liexin_zixing
IndexName
=
lc_ziying
Host
=
http://127.0.0.1:9200/
go.mod
View file @
fbf2e8b4
...
...
@@ -18,5 +18,6 @@ require (
github.com/syyongx/php2go v0.9.4
golang.org/x/sys v0.0.0-20200102141924-c96a22e43c9c // indirect
gopkg.in/go-playground/validator.v9 v9.31.0 // indirect
gopkg.in/olivere/elastic.v5 v5.0.84
gopkg.in/yaml.v2 v2.2.7 // indirect
)
go.sum
View file @
fbf2e8b4
...
...
@@ -17,6 +17,7 @@ github.com/denisenkom/go-mssqldb v0.0.0-20190920000552-128d9f4ae1cd/go.mod h1:xb
github.com/dgrijalva/jwt-go v3.1.0+incompatible h1:FFziAwDQQ2dz1XClWMkwvukur3evtZx7x/wMHKM1i20=
github.com/dgrijalva/jwt-go v3.1.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
...
...
@@ -81,6 +82,8 @@ github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdA
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mailru/easyjson v0.0.0-20180730094502-03f2033d19d5/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 h1:2gxZ0XQIU/5z3Z3bUBu+FXuk2pFbkN6tcwi/pjyaDic=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
...
...
@@ -100,11 +103,13 @@ github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwd
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM=
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s=
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
...
...
@@ -169,6 +174,8 @@ gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWd
gopkg.in/go-playground/validator.v9 v9.31.0 h1:bmXmP2RSNtFES+bn4uYuHT7iJFJv7Vj+an+ZQdDaD1M=
gopkg.in/go-playground/validator.v9 v9.31.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/ini.v1 v1.47.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/olivere/elastic.v5 v5.0.84 h1:acF/tRSg5geZpE3rqLglkS79CQMIMzOpWZE7hRXIkjs=
gopkg.in/olivere/elastic.v5 v5.0.84/go.mod h1:LXF6q9XNBxpMqrcgax95C6xyARXWbbCXUrtTxrNrxJI=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
...
...
main.go
View file @
fbf2e8b4
package
main
import
(
"LC_soapi/pkg/es"
"LC_soapi/pkg/gredis"
"LC_soapi/pkg/logging"
"LC_soapi/pkg/setting"
...
...
@@ -13,8 +14,9 @@ import (
func
init
()
{
setting
.
Setup
()
gredis
.
Setup
()
logging
.
Setup
()
gredis
.
Setup
()
es
.
Setup
()
}
func
main
()
{
...
...
pkg/app/response.go
View file @
fbf2e8b4
...
...
@@ -16,15 +16,10 @@ type Response struct {
}
//返回数据
func
(
g
*
Gin
)
Response
(
code
int
,
msg
string
,
data
interface
{})
{
if
code
>
100
{
logging
.
ReportErrorLog
(
code
,
msg
,
g
.
C
.
ClientIP
())
}
func
(
g
*
Gin
)
ResponseData
(
data
interface
{})
{
g
.
C
.
JSON
(
200
,
Response
{
Code
:
code
,
Msg
:
msg
,
Code
:
0
,
Msg
:
"ok"
,
Data
:
data
,
})
return
...
...
@@ -39,3 +34,15 @@ func (g *Gin) ResponseOk() {
g
.
C
.
JSON
(
200
,
response
)
return
}
//返回错误
func
(
g
*
Gin
)
ResponseErr
(
code
int
,
msg
string
)
{
response
:=
map
[
string
]
interface
{}{
"err_code"
:
code
,
"err_msg"
:
msg
,
}
//记录到请求日志
logging
.
ReportErrorLog
(
code
,
msg
,
g
.
C
.
ClientIP
())
g
.
C
.
JSON
(
200
,
response
)
return
}
pkg/common/function.go
0 → 100644
View file @
fbf2e8b4
package
common
import
(
"fmt"
"regexp"
)
func
PregMatchAll
(
str
string
,
expr
string
){
r
,
_
:=
regexp
.
Compile
(
expr
)
fmt
.
Println
(
r
.
FindStringSubmatch
(
str
))
for
index
,
match
:=
range
r
.
FindStringSubmatch
(
str
)
{
fmt
.
Printf
(
"[%d] %s
\n
"
,
index
,
match
)
}
}
pkg/es/elastic.go
0 → 100644
View file @
fbf2e8b4
package
es
import
(
"LC_soapi/pkg/setting"
"context"
"github.com/ichunt2019/logger"
"gopkg.in/olivere/elastic.v5"
"log"
"os"
)
var
(
ESConn
*
elastic
.
Client
)
func
Setup
()
{
errorLog
:=
log
.
New
(
os
.
Stdout
,
"APP"
,
log
.
LstdFlags
)
esSetting
:=
setting
.
ESSetting
ESConn
,
err
:=
elastic
.
NewClient
(
elastic
.
SetErrorLog
(
errorLog
),
elastic
.
SetURL
(
esSetting
.
Host
))
if
err
!=
nil
{
logger
.
Error
(
"ES connect fail.Reason is %s"
,
err
)
}
info
,
code
,
err
:=
ESConn
.
Ping
(
esSetting
.
Host
)
.
Do
(
context
.
Background
())
if
err
!=
nil
{
panic
(
err
)
}
logger
.
Info
(
"Elasticsearch returned with code %d and version %s
\n
"
,
code
,
info
.
Version
.
Number
)
}
pkg/setting/data.go
View file @
fbf2e8b4
...
...
@@ -25,3 +25,18 @@ var SelfBrandReplace = map[string]string{
"国星"
:
"Nationstar(国星光电)"
,
"贝岭"
:
"BL(上海贝岭)"
,
}
//搜索映射关系
var
SearchKeywordMapping
=
map
[
string
]
string
{
`^(.* )?([\d\.]+)(欧|欧姆|R|r)( .*)?$`
:
"$1$2Ω$4"
,
`^(.* )?([\d\.]+)(U|u|μ)(.?)( .*)?$`
:
"$1$2Μ$4$5"
,
`COG`
:
"C0G"
,
`NPO`
:
"NP0"
,
`华科|华新科技|华新科`
:
"华新"
,
}
//精确匹配的型号列表
var
MathExactGoodsName
=
[]
string
{
"M7"
,
"M1"
,
"M4"
,
"M2"
,
"A7"
,
"F7"
,
"X2"
,
"G4"
,
"X4"
,
"m7"
,
"m1"
,
"m4"
,
"m2"
,
"a7"
,
"f7"
,
"x2"
,
"g4"
,
"x4"
,
}
pkg/setting/setting.go
View file @
fbf2e8b4
...
...
@@ -69,6 +69,13 @@ type Log struct {
var
LogSetting
=
&
Log
{}
type
ES
struct
{
Host
string
IndexName
string
}
var
ESSetting
=
&
ES
{}
var
cfg
*
ini
.
File
func
Setup
()
{
...
...
@@ -83,6 +90,7 @@ func Setup() {
mapTo
(
"database"
,
DatabaseSetting
)
mapTo
(
"redis"
,
RedisSetting
)
mapTo
(
"log"
,
LogSetting
)
mapTo
(
"es"
,
ESSetting
)
AppSetting
.
ImageMaxSize
=
AppSetting
.
ImageMaxSize
*
1024
*
1024
ServerSetting
.
ReadTimeout
=
ServerSetting
.
ReadTimeout
*
time
.
Second
...
...
server/hrpc/self.go
View file @
fbf2e8b4
...
...
@@ -3,6 +3,6 @@ package hrpc
type
SelfServer
struct
{
}
func
(
s
*
SelfServer
)
Search
()
{
func
(
s
*
SelfServer
)
Search
()
string
{
return
"Test Ok"
}
server/http/self.go
View file @
fbf2e8b4
...
...
@@ -6,12 +6,15 @@ import (
"github.com/gin-gonic/gin"
)
//搜索接口
func
Search
(
c
*
gin
.
Context
)
{
appG
:=
app
.
Gin
{
C
:
c
}
//先去处理请求参数
params
:=
service
.
Search
(
map
[
string
]
string
{})
//fmt.Println(params)
var
params
service
.
SearchParams
if
err
:=
c
.
ShouldBindQuery
(
&
params
);
err
!=
nil
{
appG
.
ResponseErr
(
500
,
"参数有误"
)
}
appG
.
Response
(
500
,
"错误测试"
,
params
)
//appG.ResponseOk(
)
result
:=
service
.
Search
(
params
)
appG
.
ResponseData
(
result
)
}
service/self.go
View file @
fbf2e8b4
package
service
import
(
"LC_soapi/pkg/es"
"LC_soapi/pkg/setting"
"fmt"
"github.com/syyongx/php2go"
"gopkg.in/olivere/elastic.v5"
"regexp"
"strings"
)
var
esClient
=
es
.
ESConn
type
Params
struct
{
//搜索的请求参数
type
SearchParams
struct
{
GoodsName
string
`form:"goods_name/condition"`
BrandName
string
`form:"brand_name/condition"`
Page
int
`form:"p"`
Flag
int
`form:"flag"`
Status
int
`form:"status/condition"`
}
func
Search
(
data
map
[
string
]
string
)
interface
{}{
params
:=
getSearchParams
(
data
)
fmt
.
Println
(
params
)
//ES的搜索参数
type
ESParams
struct
{
IndexName
string
Type
string
Source
[]
string
}
func
Search
(
params
SearchParams
)
interface
{}
{
//整合并且获取ES的查询条件数据
esp
:=
getESParams
(
params
)
return
esp
}
//获取需要用到的ES参数
func
getESParams
(
params
SearchParams
)
(
esParams
ESParams
)
{
var
(
upperField
,
intField
[]
string
goodsTempStatus
,
TestFlagNum
int
esQuery
interface
{}
)
upperField
=
[]
string
{
"goods_name"
,
"encap"
,
"packing"
,
"brand_name"
}
intField
=
[]
string
{
"status"
,
"class_id2"
,
"class_id1"
,
"single_price"
,
"stock"
,
"goods_id"
,
"goods_type"
,
"sort"
,
"brand_id"
}
//判断是否测试的flag值
TestFlagNum
=
100
//先对搜索参数设置一些默认值
params
=
setParamsDefaultValue
(
params
)
esParams
=
setESParamsDefaultValue
(
esParams
)
//flag用于测试
if
params
.
Flag
!=
TestFlagNum
{
esParams
.
Source
=
[]
string
{
"goods_id"
,
"goods_name"
}
}
//判断status
if
params
.
Status
!=
0
{
goodsTempStatus
=
1
}
//判断商品名称
if
params
.
GoodsName
!=
""
{
params
.
GoodsName
=
transformGoodsName
(
params
.
GoodsName
)
goodsNameUpper
:=
strings
.
ToUpper
(
params
.
GoodsName
)
return
nil
//精确匹配的型号列表
if
php2go
.
InArray
(
goodsNameUpper
,
setting
.
MathExactGoodsName
)
{
termQuery
:=
elastic
.
NewTermQuery
(
"auto_goods_name.raw"
,
goodsNameUpper
)
esQuery
=
elastic
.
NewBoolQuery
()
.
Must
(
termQuery
)
}
}
fmt
.
Println
(
upperField
,
intField
,
goodsTempStatus
,
esQuery
)
return
esParams
}
//转换商品名称
func
transformGoodsName
(
goodsName
string
)
string
{
//正则处理
for
key
,
value
:=
range
setting
.
SearchKeywordMapping
{
reg
,
_
:=
regexp
.
Compile
(
key
)
goodsName
=
reg
.
ReplaceAllString
(
goodsName
,
value
)
}
goodsName
=
strings
.
TrimSpace
(
goodsName
)
return
goodsName
}
func
getSearchParams
(
data
map
[
string
]
string
)
(
params
Params
)
{
return
params
func
getGoodsNameSlice
(
goodsName
string
)
(
goodsNameSlice
[]
string
)
{
//判断是否存在于品牌中,如果存在,则转换成品牌数据
if
goodsName
!=
""
{
brandName
:=
setting
.
SelfBrandReplace
[
strings
.
ToUpper
(
goodsName
)]
if
brandName
!=
""
{
goodsNameSlice
=
append
(
goodsNameSlice
,
brandName
)
return
goodsNameSlice
}
}
return
goodsNameSlice
}
func
getGoodsName
(
goodsName
string
){
//判断是否存在于品牌中
if
(
goodsName
!=
""
)
&&
(
php2go
.
Strtoupper
(
goodsName
)
!=
""
)
{
//设置搜索相关数据的默认值
func
setParamsDefaultValue
(
params
SearchParams
)
(
result
SearchParams
)
{
//默认页码
if
params
.
Page
==
0
{
params
.
Page
=
1
}
result
=
params
return
result
}
//获取ES的相关数据默认值
func
setESParamsDefaultValue
(
params
ESParams
)
(
result
ESParams
)
{
//ES的索引键名
if
setting
.
ESSetting
.
IndexName
==
""
{
params
.
IndexName
=
"liexin_ziying"
}
else
{
params
.
IndexName
=
setting
.
ESSetting
.
IndexName
}
params
.
Type
=
"goods"
result
=
params
return
result
}
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