Commit d93f32be by mushishixian

output添加跨域

parent 9444bc4b
......@@ -31,14 +31,10 @@ func Recommend(c *gin.Context) {
fmt.Println(err)
}
if req.Flag == 0 {
res := common.BomResponse{
ErrCode: 101,
ErrMsg: "参数不全",
Data: []string{},
}
c.JSON(200, res)
common.Output(101, "参数不全", nil)
return
}
data := service.Recommend(req)
c.JSON(200, data)
common.Output(req.Flag, "", data)
return
}
......@@ -132,8 +132,8 @@ func GetDataPur(c *gin.Context) {
defer redisCon.Close()
for _, supplier := range supplierData {
if value, exist := temp2[supplier.ChannelUid]; exist {
res, err := redis.Bool(redisCon.Do("HSET", "search_supplier_canaltopurchase_test", supplier.SupplierCode, value))
if err != nil || !res {
_, err := redis.Bool(redisCon.Do("HSET", "search_supplier_canaltopurchase_test", supplier.SupplierCode, value))
if err != nil {
c.String(200, "失败")
return
}
......
package common
import (
"regexp"
"runtime"
"search_server/middleware"
"search_server/pkg/log_report"
"strconv"
"strings"
)
type Response struct {
......@@ -53,13 +53,8 @@ func Output(errCode int, errMsg string, data interface{}) {
ctx.JSONP(200, response)
} else {
referer := ctx.Request.Referer()
r, _ := regexp.Compile(`/^(\w+:\/\/)?([^\/]+)/i`)
var match string
matches := r.FindStringSubmatch(referer)
if len(matches) > 0 {
match = matches[0]
}
ctx.Header("Access-Control-Allow-Origin", match)
referer = strings.TrimRight(referer,"/")
ctx.Header("Access-Control-Allow-Origin", referer)
ctx.Header("Access-Control-Allow-Credentials", "true")
//允许跨站访问的站点域名
//跨域请求头设置
......
......@@ -6,6 +6,7 @@ import (
"github.com/tidwall/gjson"
"gopkg.in/olivere/elastic.v5"
"regexp"
"search_server/model"
"search_server/pkg/common"
"search_server/pkg/config"
"search_server/pkg/es"
......@@ -64,7 +65,7 @@ func getTermQuery(goodsName string) (query *elastic.BoolQuery) {
}
//推荐商品搜索
func Recommend(req *common.RecommendRequest) (rsp *common.BomResponse) {
func Recommend(req *common.RecommendRequest) (goodsList []model.ApiGoods) {
var err error
//先去请求参数,看是否是参数,如果是参数的话,需要修改请求的商品名称
var goodsName string
......@@ -101,10 +102,8 @@ func Recommend(req *common.RecommendRequest) (rsp *common.BomResponse) {
goodsIdList = append(goodsIdList, goodsId)
}
goodsIdListStr = strings.Join(goodsIdList, ",")
goodsList, err := GetGoodsInfoByApi(goodsIdListStr)
response.Data = goodsList
response.Flag = req.Flag
return &response
goodsList, err = GetGoodsInfoByApi(goodsIdListStr)
return goodsList
}
//处理recommend接口的请求参数
......
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