abiao лет назад: 4
Родитель
Сommit
10c19d1daa

+ 44 - 0
go/gopath/src/fohow.com/apps/controllers/order_controller/order_controller.go

@@ -671,3 +671,47 @@ func CreateOrderDetails(product *product_model.Product, order *order_model.Order
 	}
 	new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.Price, product.RoboBalancePrice, product.Name, sizeName, colorName, cNums, depart)
 }
+
+//用户评论订单明细
+func (self *OrderController) OrderCommend() {
+	_detail_id := self.Ctx.Input.Param(":detail_id")
+	detail_id, _ := strconv.ParseInt(_detail_id, 10, 64)
+	detail := self.GetString("detail")
+	score, _ := self.GetInt64("score")
+
+	od := order_model.GetOrderDetailById(detail_id)
+	if od == nil {
+		self.ReturnError(404, apps.OrderNotExist, "", nil)
+	}
+	if od.Commend {
+		self.ReturnError(404, apps.DetailHasCommend, "", nil)
+	}
+	if len(detail) == 0 {
+		self.ReturnError(404, apps.DetailNotAllow, "", nil)
+	}
+
+	wxUId := self.GetCurrentWxUserId()
+	o := order_model.GetOrderById(od.OrderNo, false)
+	if o.WxUserId != wxUId {
+		self.ReturnError(403, apps.AccountError, "", nil)
+	}
+
+	//订单 状态已完成才允许评论
+	if o.Status == order_model.STATUS_COMPLETE {
+		//先更改明细评论状态
+		od.Commend = true
+		od.Save()
+		//创建商品评论
+		new(product_model.ProductCommend).Create(o.WxUserId, od.ProductId, score, detail)
+	} else {
+		self.ReturnError(403, apps.ParamsError, "", nil)
+	}
+
+	type apiRet struct {
+		Status bool `json:"status"`
+	}
+	self.Data["json"] = &apiRet{
+		Status: true,
+	}
+	self.ServeJSON()
+}

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

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

+ 33 - 0
go/gopath/src/fohow.com/apps/controllers/product_controller/product_controller.go

@@ -1,6 +1,8 @@
 package product_controller
 
 import (
+	"fmt"
+	"fohow.com/apps/models/user_model"
 	// "fmt"
 	"strconv"
 	"strings"
@@ -350,3 +352,34 @@ func (self *ProductController) GetProductSizesByPid() {
 	self.Data["json"] = ret
 	self.ServeJSON()
 }
+
+//获取某商品评论列表
+func (self *ProductController) GetProductsCommends() {
+	_id := self.Ctx.Input.Param(":product_id")
+	prdId, _ := strconv.ParseInt(_id, 10, 64)
+	page, _ := self.GetInt64("page")
+	perPage, _ := self.GetInt64("per_page")
+	cache, _ := self.GetBool("cache", false)
+	if page <= 0 {
+		page = 1
+	}
+	if perPage <= 0 || perPage > 100 {
+		perPage = 20
+	}
+	type Ret struct {
+		List      []*product_model.ProductCommend `json:"list"`
+		ListCount int64                           `json:"list_count"`
+	}
+	list := product_model.GetProductCommends(prdId, true, cache, page, perPage)
+	count := product_model.GetProductCommendCountByProductId(prdId, true, cache)
+	for _, cd := range list {
+		wxUser := user_model.GetWxUserById(cd.WxUserId, true)
+		if wxUser != nil {
+			cd.Head = product_model.GetCdnFullImgUrl(wxUser.Head)
+			name := string([]rune(wxUser.Nickname)[:1])
+			cd.NickName = fmt.Sprintf("%s*", name)
+		}
+	}
+	self.Data["json"] = &Ret{List: list, ListCount: count}
+	self.ServeJSON()
+}

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

@@ -85,6 +85,8 @@ var (
 	OnlyPlayInWeixin = []string{"onlyPlayInWeixin", "只能在微信客户端进行"}
 	OrderNotExist    = []string{"orderNotExist", "订单不存在"}
 	OrderExist       = []string{"orderExist", "订单已存在"}
+	DetailNotAllow   = []string{"detailNotAllow", "评论内容不能为空"}
+	DetailHasCommend = []string{"detailHasCommend", "商品已评论"}
 
 	PayFail               = []string{"payFail", "支付失败"}
 	ParamsError           = []string{"paramsError", "参数错误"}

+ 46 - 1
go/gopath/src/fohow.com/apps/models/product_model/product_commend.go

@@ -3,6 +3,8 @@ package product_model
 import (
 	// "time"
 
+	"fmt"
+	"fohow.com/cache"
 	"github.com/astaxie/beego"
 	"github.com/astaxie/beego/orm"
 	"time"
@@ -31,6 +33,23 @@ func (self *ProductCommend) TableName() string {
 	return product_commends_tablename
 }
 
+func (self *ProductCommend) Create(wxUId, pId, score int64, detail string) (item *ProductCommend) {
+	item = &ProductCommend{
+		ProductId: pId,
+		WxUserId:  wxUId,
+		Detail:    detail,
+		Score:     score,
+		IsEnable:  true,
+	}
+	id, err := orm.NewOrm().Insert(item)
+	if err != nil {
+		beego.BeeLogger.Error("Create ProductCommend err=[%s]", err)
+		return nil
+	}
+	item.Id = id
+	return item
+}
+
 func GetProductCommendById(id int64) *ProductCommend {
 	cat := &ProductCommend{Id: id}
 	if err := orm.NewOrm().Read(cat); err != nil {
@@ -41,7 +60,13 @@ func GetProductCommendById(id int64) *ProductCommend {
 }
 
 //获取商品评论列表
-func GetProductCommends(productId int64, status bool, page, perPage int) (commends []*ProductCommend) {
+func GetProductCommends(productId int64, status, useCache bool, page, perPage int64) (commends []*ProductCommend) {
+	k := fmt.Sprintf("product_model.GetProductCommends(%d).page(%d).perPage(%d)", productId, page, perPage)
+	if useCache {
+		if ret, ok := cache.Cache.Get(k).([]*ProductCommend); ok {
+			return ret
+		}
+	}
 	commend := new(ProductCommend)
 	qs := orm.NewOrm().QueryTable(commend)
 	qs = qs.Filter("product_id", productId).Filter("is_enable", status)
@@ -51,5 +76,25 @@ func GetProductCommends(productId int64, status bool, page, perPage int) (commen
 		beego.BeeLogger.Debug("get productCommeds[%d] commend list err=[%s]", productId, err)
 		return nil
 	}
+	cache.Cache.Put(k, commends, 10*time.Minute)
+
 	return commends
 }
+
+//获取商品评论列表总条数
+func GetProductCommendCountByProductId(productId int64, status, useCache bool) int64 {
+	k := fmt.Sprintf("product_model.GetProductCommendCountByProductId(%d)", productId)
+	if useCache {
+		if ret, ok := cache.Cache.Get(k).(int64); ok {
+			return ret
+		}
+	}
+	item := new(Product)
+	o := orm.NewOrm()
+	qs := o.QueryTable(item)
+
+	count, _ := qs.Filter("is_enable", status).Filter("product_id", productId).Count()
+
+	cache.Cache.Put(k, count, 10*time.Minute)
+	return count
+}

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

@@ -86,6 +86,9 @@ func init() {
 	beego.Router("/v1/product/cats", &product_controller.ProductController{}, "get:Categories")
 	//某个类别下的商品列表
 	beego.Router("/v1/cat/:cat_id([0-9]+)/products", &product_controller.ProductController{}, "get:GetProductsByCat")
+	//获取某商品下评论列表
+	beego.Router("/v1/product/:product_id([0-9]+)/commends", &product_controller.ProductController{}, "get:GetProductsCommends")
+
 	//商品详情
 	beego.Router("/v1/product/:id([0-9]+)", &product_controller.ProductController{}, "get:Get")
 	//商品规格明细
@@ -104,6 +107,8 @@ func init() {
 	beego.Router("/v1/order/:order_id/express_no", &order_controller.OrderController{}, "get:GetExpressNo")
 	//更改订单状态:确认收货、取消订单
 	beego.Router("/v1/order/:order_id/:operate", &order_controller.OrderController{}, "put:Operate")
+	//订单明细评论
+	beego.Router("/v1/order_detail/:detail_id/commend", &order_controller.OrderController{}, "put:OrderCommend")
 	//----------- 支付相关 -----------
 	beego.Router("/v1/pay", &pay_controller.PayController{}, "post,get:Pay")
 	beego.Router("/v1/pay/:target:string/async/:payway:string", &pay_controller.PayController{}, "post,get:PayAsync")