You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.6 KiB
110 lines
2.6 KiB
package cart
|
|
|
|
import (
|
|
"github.com/towgo/towgo/dao/basedboperat"
|
|
"github.com/towgo/towgo/dao/ormDriver/xormDriver"
|
|
"github.com/towgo/towgo/towgo"
|
|
"src/module/usercenter"
|
|
)
|
|
|
|
func init() {
|
|
xormDriver.Sync2(new(Cart))
|
|
towgo.NewCRUDJsonrpcAPI("/cart", Cart{}, []Cart{}).RegAPI()
|
|
towgo.SetFunc("/cart/mycart", GetMyCart)
|
|
towgo.SetFunc("/cart/add", CartAdd)
|
|
towgo.SetFunc("/cart/edit", CartEdit)
|
|
}
|
|
|
|
func GetMyCart(rpc towgo.JsonRpcConnection) {
|
|
user, err := usercenter.LoginByToken(rpc.GetRpcRequest().Session)
|
|
//user, err := usercenter.LoginByToken("15c59252b34734f5f106be41a8108cf3")
|
|
user.Token = rpc.GetRpcRequest().Session
|
|
if err != nil {
|
|
rpc.GetRpcResponse().Error.Set(401, err.Error())
|
|
rpc.Write()
|
|
return
|
|
}
|
|
var c Cart
|
|
var cs []Cart
|
|
var listCart basedboperat.List
|
|
listCart.Limit = -1
|
|
listCart.Where = append(listCart.Where, basedboperat.Condition{
|
|
Field: "user_id",
|
|
Operator: "=",
|
|
Value: user.ID,
|
|
})
|
|
basedboperat.ListScan(&listCart, &c, &cs)
|
|
|
|
rpc.WriteResult(cs)
|
|
}
|
|
|
|
type AddParams struct {
|
|
GoodsList []struct {
|
|
GoodsId int64 `json:"goods_id"`
|
|
GoodsNum int64 `json:"goods_num"`
|
|
SkuPriceId int64 `json:"sku_price_id"`
|
|
GoodsPrice string `json:"goods_price"`
|
|
} `json:"goods_list"`
|
|
Form string `json:"form"`
|
|
}
|
|
|
|
func CartAdd(rpc towgo.JsonRpcConnection) {
|
|
var params AddParams
|
|
rpc.ReadParams(¶ms)
|
|
user, err := usercenter.LoginByToken(rpc.GetRpcRequest().Session)
|
|
//user, err := usercenter.LoginByToken("15c59252b34734f5f106be41a8108cf3")
|
|
user.Token = rpc.GetRpcRequest().Session
|
|
if err != nil {
|
|
rpc.GetRpcResponse().Error.Set(401, err.Error())
|
|
rpc.Write()
|
|
return
|
|
}
|
|
var c Cart
|
|
for _, v := range params.GoodsList {
|
|
c = Cart{
|
|
UserID: user.ID,
|
|
GoodsId: v.GoodsId,
|
|
GoodsNum: v.GoodsNum,
|
|
SkuPriceId: v.SkuPriceId,
|
|
}
|
|
basedboperat.Create(&c)
|
|
}
|
|
type res struct {
|
|
Id int64 `json:"id"`
|
|
UserId int64 `json:"user_id"`
|
|
GoodsId int64 `json:"goods_id"`
|
|
GoodsNum int64 `json:"goods_num"`
|
|
SkuPriceId int64 `json:"sku_price_id"`
|
|
}
|
|
rpc.WriteResult(res{
|
|
Id: c.ID,
|
|
UserId: user.UserID,
|
|
GoodsId: c.GoodsId,
|
|
GoodsNum: c.GoodsNum,
|
|
SkuPriceId: c.SkuPriceId,
|
|
})
|
|
|
|
}
|
|
|
|
type EditParams struct {
|
|
CartList []int64 `json:"cart_list"`
|
|
Value string `json:"value"`
|
|
Act string `json:"act"`
|
|
}
|
|
|
|
func CartEdit(rpc towgo.JsonRpcConnection) {
|
|
var params EditParams
|
|
rpc.ReadParams(¶ms)
|
|
if len(params.CartList) == 0 {
|
|
rpc.WriteError(200, "请选择商品")
|
|
return
|
|
}
|
|
var c Cart
|
|
if params.Act == "delete" {
|
|
for _, v := range params.CartList {
|
|
basedboperat.Delete(&c, v, nil, nil)
|
|
}
|
|
}
|
|
rpc.WriteResult(true)
|
|
}
|