Commit 81f6d2b5 by huangchengyi

增加mouser联营

parent a20a53f6
protoc --proto_path=protos --micro_out=protopb/mouser --go_out=protopb/mouser mouser.proto
\ No newline at end of file
package controller
import (
"github.com/gin-gonic/gin"
"search_server/framework/gin_"
"search_server/protopb/mouser"
"search_server/service"
)
func init() {
MouserService := service.NewMouserServiceImpl()
gin_.NewBuilder().WithService(MouserService).
WithEndpoint(MouserGetData(MouserService)).
WithRequest(MouserRequest()).
WithResponse(MouserResponse()).Build("/search/mouser/test", "POST")
}
//处理请求
func MouserGetData(c *service.MouserServiceImpl) gin_.Endpoint {
return func(context *gin.Context, request interface{}) (response interface{}, err error) {
rsp := &mouser.MouserResponse{}
err = c.MouserGetData(context, request.(*mouser.MouserRequest), rsp)
return rsp, err
}
}
//接收请求
func MouserRequest() gin_.EncodeRequestFunc {
return func(context *gin.Context) (i interface{}, e error) {
bReq := &mouser.MouserRequest{}
err := context.BindQuery(bReq) //使用的是query 参数
bReq.GoodsName, _ = context.GetPostForm("goods_name")
if err != nil {
return nil, err
}
return bReq, nil
}
}
//响应请求
func MouserResponse() gin_.DecodeResponseFunc {
return func(context *gin.Context, res interface{}) error {
context.JSON(200, res)
return nil
}
}
......@@ -18,8 +18,10 @@ require (
github.com/jinzhu/gorm v1.9.12
github.com/joncalhoun/qson v0.0.0-20170526102502-8a9cab3a62b1 // indirect
github.com/mholt/certmagic v0.9.3 // indirect
github.com/micro/go-micro v1.16.0
github.com/micro/go-micro/v2 v2.9.0
github.com/micro/micro/v2 v2.9.1 // indirect
github.com/micro/protoc-gen-micro v1.0.0 // indirect
github.com/prometheus/client_golang v1.5.1 // indirect
github.com/prometheus/common v0.9.1
github.com/prometheus/procfs v0.0.11 // indirect
......@@ -33,6 +35,7 @@ require (
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
google.golang.org/genproto v0.0.0-20200527145253-8367513e4ece // indirect
google.golang.org/grpc v1.29.1 // indirect
google.golang.org/protobuf v1.24.0
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
gopkg.in/olivere/elastic.v5 v5.0.85
......
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: mouser.proto
package mouser
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
import (
context "context"
client "github.com/micro/go-micro/client"
server "github.com/micro/go-micro/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ client.Option
var _ server.Option
// Client API for MouserService service
type MouserService interface {
MouserGetData(ctx context.Context, in *MouserRequest, opts ...client.CallOption) (*MouserResponse, error)
}
type mouserService struct {
c client.Client
name string
}
func NewMouserService(name string, c client.Client) MouserService {
if c == nil {
c = client.NewClient()
}
if len(name) == 0 {
name = "mouser"
}
return &mouserService{
c: c,
name: name,
}
}
func (c *mouserService) MouserGetData(ctx context.Context, in *MouserRequest, opts ...client.CallOption) (*MouserResponse, error) {
req := c.c.NewRequest(c.name, "MouserService.MouserGetData", in)
out := new(MouserResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for MouserService service
type MouserServiceHandler interface {
MouserGetData(context.Context, *MouserRequest, *MouserResponse) error
}
func RegisterMouserServiceHandler(s server.Server, hdlr MouserServiceHandler, opts ...server.HandlerOption) error {
type mouserService interface {
MouserGetData(ctx context.Context, in *MouserRequest, out *MouserResponse) error
}
type MouserService struct {
mouserService
}
h := &mouserServiceHandler{hdlr}
return s.Handle(s.NewHandler(&MouserService{h}, opts...))
}
type mouserServiceHandler struct {
MouserServiceHandler
}
func (h *mouserServiceHandler) MouserGetData(ctx context.Context, in *MouserRequest, out *MouserResponse) error {
return h.MouserServiceHandler.MouserGetData(ctx, in, out)
}
syntax = "proto3";
package mouser;
service MouserService {
rpc MouserGetData(MouserRequest) returns (MouserResponse);
}
message MouserRequest {
// @inject_tag: uri:"goods_name"
string goods_name = 1;
}
message ResponseData{
// @inject_tag: json:"total"
int64 total = 1;
// @inject_tag: json:"flag"
int32 flag = 2;
// @inject_tag: json:"data"
repeated GoodsModel data = 3;
}
message MouserResponse{
// @inject_tag: json:"error_code"
int32 error_code = 1;
// @inject_tag: json:"error_message"
string error_message = 2;
// @inject_tag: json:"data"
ResponseData data = 3;
}
message OtherAttrs {
string gross_wegiht = 1;
}
message LadderPrice {
int64 purchases = 1;
float price_cn = 2;
float price_us = 3;
float price_ac = 4;
}
message GoodsModel {
// @inject_tag: json:"other_attrs"
OtherAttrs other_attrs = 1;
// @inject_tag: json:"pick_type"
int64 pick_type = 2;
// @inject_tag: json:"barcode"
string barcode = 3;
// @inject_tag: json:"goods_id"
string goods_id = 4;
// @inject_tag: json:"goods_type
int64 goods_type = 5;
// @inject_tag: json:"supplier_id"
int64 supplier_id = 6;
// @inject_tag: json:"brand_id"
int64 brand_id = 7;
// @inject_tag: json:"class_id1"
int64 class_id1 = 8;
// @inject_tag: json:"class_id2"
int64 class_id2 = 9;
// @inject_tag: json:"goods_name"
string goods_name = 10;
// @inject_tag: json:"status"
int64 status = 11;
// @inject_tag: json:"encoded"
string encoded = 12;
// @inject_tag: json:"encap"
string encap = 13;
// @inject_tag: json:"packing"
int64 packing = 14;
// @inject_tag: json:"goods_unit"
int64 goods_unit = 15;
// @inject_tag: json:"goods_images"
bool goods_images = 16;
// @inject_tag: json:"pdf"
string pdf = 17;
// @inject_tag: json:"goods_brief"
string goods_brief = 18;
// @inject_tag: json:"moq"
int64 moq = 19;
// @inject_tag: json:"mpq"
int64 mpq = 20;
// @inject_tag: json:"ladder_price"
repeated LadderPrice ladder_price = 21;
// @inject_tag: json:"update_time"
int64 update_time = 22;
// @inject_tag: json:"sku_name"
string sku_name = 23;
// @inject_tag: json:"mpl"
int64 mpl = 24;
// @inject_tag: json:"stock"
int64 stock = 25;
// @inject_tag: json:"attrs"
string attrs = 26;
// @inject_tag: json:"cost"
string cost = 27;
// @inject_tag: json:"new_cost"
string new_cost = 28;
// @inject_tag: json:"supplier_stock"
int64 supplier_stock = 29;
// @inject_tag: json:"self_supplier_type"
int64 self_supplier_type = 30;
// @inject_tag: json:"cn_delivery_time"
string cn_delivery_time = 31;
// @inject_tag: json:"brand_name"
string brand_name = 32;
// @inject_tag: json:"supplier_name"
string supplier_name = 33;
string goods_unit_name = 34;
string packing_name = 35;
string mpq_unit_name = 36;
// @inject_tag: json:"ac_type"
int64 ac_type = 37;
int64 allow_coupon = 38;
string class_id1_name = 39;
string class_id2_name = 40;
// @inject_tag: json:"otheis_buyr_attrs"
int64 is_buy = 41;
// @inject_tag: json:"hk_delivery_type"
string hk_delivery_type = 42;
}
\ No newline at end of file
package service
import (
"context"
"github.com/prometheus/common/log"
"github.com/tidwall/gjson"
"gopkg.in/olivere/elastic.v5"
"os"
"regexp"
"search_server/pkg/es"
"search_server/protopb/mouser"
"strings"
)
//单例
type MouserServiceImpl struct{}
func NewMouserServiceImpl() *MouserServiceImpl {
return &MouserServiceImpl{}
}
//mouser外链请求
func (bs *MouserServiceImpl) MouserGetData(ctx context.Context, req *mouser.MouserRequest, rsp *mouser.MouserResponse) error {
goodsNameSlice := OutLink(req.GoodsName)
if len(goodsNameSlice) == 0 {
rsp.ErrorCode = 1
}
//rsp.Data = goodsNameSlice
return nil
}
//mouser请求外链
func OutLink(goodsName string) []string {
print("nini")
os.Exit(1)
replace, _ := regexp.Compile("[^A-Za-z0-9]+")
goodsName = replace.ReplaceAllString(goodsName, "")
goodsName = strings.ToUpper(goodsName)
query := getTermQuery(goodsName)
source := elastic.NewSearchSource().Query(query)
source = source.From(0).Size(10)
searchRequestStr, _ := elastic.NewSearchRequest().Source(source).Body()
result, err := es.CurlES("lie_spu", searchRequestStr)
//只要其中的goods_name
if err != nil {
log.Error(err)
}
goodsNameList := make([]string, 0)
goodsList := gjson.Get(result, "hits.hits.#._source").Array()
for _, goods := range goodsList {
goodsName := gjson.Get(goods.String(), "auto_goods_name").String()
goodsNameList = append(goodsNameList, goodsName)
}
return goodsNameList
}
\ No newline at end of file
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