Bläddra i källkod

增加搜索关键字推荐

abiao 4 år sedan
förälder
incheckning
b3eb8c6984

+ 2 - 2
go/gopath/src/fohow.com/apps/controllers/product_controller/init.go

@@ -16,8 +16,8 @@ import (
 
 var (
 	//需要校验用户登录的Action
-	exceptCheckUserLoginAction   = []string{"GetProductsCommends", "GetPdDetail", "GetProductSizesByPid", "Latest", "Get", "Categories", "GetProductsByCat", "GetNeedShare"}
-	exceptCheckWxUserLoginAction = []string{"GetProductsCommends", "GetPdDetail", "GetProductSizesByPid", "Latest", "Get", "Categories", "GetProductsByCat"}
+	exceptCheckUserLoginAction   = []string{"GetProductsCommends", "GetPdDetail", "GetProductSizesByPid", "Latest", "Get", "Categories", "GetProductsByCat", "GetNeedShare", "GetCommendWords"}
+	exceptCheckWxUserLoginAction = []string{"GetProductsCommends", "GetPdDetail", "GetProductSizesByPid", "Latest", "Get", "Categories", "GetProductsByCat", "GetCommendWords"}
 )
 
 type ProductController struct {

+ 20 - 2
go/gopath/src/fohow.com/apps/controllers/product_controller/product_controller.go

@@ -306,6 +306,7 @@ func (self *ProductController) GetProductsByCat() {
 	catId, _ := strconv.ParseInt(_id, 10, 64)
 	page, _ := self.GetInt64("page")
 	perPage, _ := self.GetInt64("per_page")
+	words := self.GetString("words")
 	cache, _ := self.GetBool("cache", false)
 	if page <= 0 {
 		page = 1
@@ -317,8 +318,8 @@ func (self *ProductController) GetProductsByCat() {
 		List      []*product_model.Product `json:"list"`
 		ListCount int64                    `json:"list_count"`
 	}
-	list := product_model.GetProductsByCatId(catId, page, perPage, cache)
-	count := product_model.GetProductCountByCatId(catId, cache)
+	list := product_model.GetProductsByCatId(catId, page, perPage, words, cache)
+	count := product_model.GetProductCountByCatId(catId, words, cache)
 	for _, pd := range list {
 		pd.SoldCount = pd.SaleNums
 		if pd.Count > pd.SoldCount {
@@ -504,3 +505,20 @@ func (self *ProductController) GetProductsCommends() {
 	self.Data["json"] = &Ret{List: list, ListCount: count}
 	self.ServeJSON()
 }
+
+func (self *ProductController) GetCommendWords() {
+
+	perPage, _ := self.GetInt64("per_page")
+	cache, _ := self.GetBool("cache", true)
+
+	if perPage <= 0 {
+		perPage = 10
+	}
+	type Ret struct {
+		List []*product_model.CommendWord `json:"list"`
+	}
+	list := product_model.GetCommendWordsList(perPage, cache)
+
+	self.Data["json"] = &Ret{List: list}
+	self.ServeJSON()
+}

+ 44 - 0
go/gopath/src/fohow.com/apps/models/product_model/commend_words.go

@@ -0,0 +1,44 @@
+package product_model
+
+import (
+	"fmt"
+	"fohow.com/cache"
+	"github.com/astaxie/beego"
+	"github.com/astaxie/beego/orm"
+	"time"
+)
+
+const (
+	commend_words_tablename = "commend_words"
+)
+
+type CommendWord struct {
+	Id    int64  `orm:"column(id);pk"                json:"id"`    // int(11)
+	Sort  int64  `orm:"column(sort)"           json:"sort"`        // int(11)
+	Title string `orm:"column(title);null"           json:"title"` // varchar(64)
+	State bool   `orm:"column(state);null"         json:"-"`       // tinyint(1)
+
+}
+
+func (self *CommendWord) TableName() string {
+	return commend_words_tablename
+}
+
+func GetCommendWordsList(perSize int64, useCache bool) (items []*CommendWord) {
+	k := fmt.Sprintf("product_model.GetCommendWordsList(%d)", perSize)
+
+	if useCache {
+		if ret, ok := cache.Cache.Get(k).([]*CommendWord); ok {
+			return ret
+		}
+	}
+
+	o := orm.NewOrm()
+	_, err := o.QueryTable(new(CommendWord)).Limit(perSize).OrderBy("-sort").All(&items)
+	if err != nil {
+		beego.BeeLogger.Debug("GetCommendWordsList err=%s", err)
+	}
+
+	cache.Cache.Put(k, items, 10*time.Minute)
+	return items
+}

+ 1 - 0
go/gopath/src/fohow.com/apps/models/product_model/init.go

@@ -17,6 +17,7 @@ func init() {
 		new(ProductCat),
 		new(ProductCommend),
 		new(ProductItem),
+		new(CommendWord),
 	)
 }
 

+ 10 - 7
go/gopath/src/fohow.com/apps/models/product_model/product.go

@@ -111,6 +111,7 @@ type Product struct {
 	HaveSize         bool            `orm:"-"                                     json:"have_size"`  // bool
 	SizeName         string          `orm:"-"                                     json:"size_name"`  // varchar(255)
 	ColorName        string          `orm:"-"                                     json:"color_name"` // varchar(255)
+	KeyWords         string          `orm:"column(key_words)"                     json:"key_words"`  // varchar(255)
 	SinglePurchLimit int64           `orm:"column(single_purch_limit)"            json:"-"`          // varchar(255)
 	Pv               int64           `orm:"column(pv)"                  json:"-"`                    // varchar(255)
 	OutNums          int64           `orm:"column(out_nums)"            json:"-"`                    // varchar(255)
@@ -182,8 +183,8 @@ func GetProductById(id int64, useCache bool) *Product {
 	return item
 }
 
-func GetProductsByCatId(cId, page, perPage int64, useCache bool) (products []*Product) {
-	k := fmt.Sprintf("product_model.GetProductsByCatId(%d).page(%d).perPage(%d)", cId, page, perPage)
+func GetProductsByCatId(cId, page, perPage int64, words string, useCache bool) (products []*Product) {
+	k := fmt.Sprintf("product_model.GetProductsByCatId(%d).page(%d).perPage(%d).words(%d)", cId, page, perPage, words)
 	if useCache {
 		if ret, ok := cache.Cache.Get(k).([]*Product); ok {
 			return ret
@@ -193,8 +194,9 @@ func GetProductsByCatId(cId, page, perPage int64, useCache bool) (products []*Pr
 	qs := o.QueryTable(new(Product)).Filter("status", 1).Filter("show_flag", true).Filter("ptype", TYPE_DIRECT_SALE)
 	if cId != 0 {
 		qs = qs.Filter("category_id", cId)
-	} else {
-		qs = qs.Exclude("category_id", 3) //3为审核类别,暂写死
+	}
+	if len(words) > 0 {
+		qs = qs.Filter("key_words__icontains", words)
 	}
 	_, err := qs.
 		OrderBy("-recommend", "-created_at").
@@ -212,7 +214,7 @@ func GetProductsByCatId(cId, page, perPage int64, useCache bool) (products []*Pr
 	return products
 }
 
-func GetProductCountByCatId(cId int64, useCache bool) int64 {
+func GetProductCountByCatId(cId int64, words string, useCache bool) int64 {
 	k := fmt.Sprintf("product_model.GetProductCountByCatId(%d)", cId)
 	if useCache {
 		if ret, ok := cache.Cache.Get(k).(int64); ok {
@@ -224,8 +226,9 @@ func GetProductCountByCatId(cId int64, useCache bool) int64 {
 	qs := o.QueryTable(item)
 	if cId != 0 {
 		qs = qs.Filter("category_id", cId)
-	} else {
-		qs = qs.Exclude("category_id", 3) //3为审核类别,暂写死
+	}
+	if len(words) > 0 {
+		qs = qs.Filter("key_words__icontains", words)
 	}
 	count, _ := qs.Filter("status", 1).Filter("show_flag", true).Filter("ptype", TYPE_DIRECT_SALE).Count()
 

BIN
go/gopath/src/fohow.com/fohowmall.com


+ 4 - 0
go/gopath/src/fohow.com/routers/routes.go

@@ -95,6 +95,10 @@ func init() {
 
 	//商品详情
 	beego.Router("/v1/product/:id([0-9]+)", &product_controller.ProductController{}, "get:Get")
+
+	//关键字推荐
+	beego.Router("/v1/product/commend_words", &product_controller.ProductController{}, "get:GetCommendWords")
+
 	//商品规格明细
 	beego.Router("/v1/product_size/:id([0-9]+)", &product_controller.ProductController{}, "get:GetProductSizesByPid")
 	//商品详情 by size + color