Commit d93f32be by mushishixian

output添加跨域

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