forked from ixre/go2o
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart_c.go
120 lines (106 loc) · 2.77 KB
/
cart_c.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Copyright 2015 @ z3q.net.
* name : cart_c.go
* author : jarryliu
* date : -- :
* description :
* history :
*/
package ols
import (
"encoding/json"
"github.com/jsix/gof"
"github.com/jsix/gof/web"
"go2o/src/core/domain/interface/partner"
"go2o/src/core/infrastructure/format"
"go2o/src/core/service/dps"
"net/http"
"strconv"
"strings"
"time"
)
type CartC struct {
*BaseC
}
// 购物车
func (this *CartC) CartApiHandle(ctx *web.Context) {
if !this.BaseC.Requesting(ctx) {
ctx.Response.Write([]byte("invalid request"))
return
}
r, _ := ctx.Request, ctx.Response
p := this.BaseC.GetPartner(ctx)
m := this.BaseC.GetMember(ctx)
r.ParseForm()
var action = strings.ToLower(r.FormValue("action"))
var cartKey = r.FormValue("cart.key")
var memberId int
if m != nil {
memberId = m.Id
}
switch action {
case "get":
this.cart_GetCart(ctx, p, memberId, cartKey)
case "add":
this.cart_AddItem(ctx, p, memberId, cartKey)
case "remove":
this.cart_RemoveItem(ctx, p, memberId, cartKey)
}
}
func (this *CartC) cart_GetCart(ctx *web.Context, p *partner.ValuePartner,
memberId int, cartKey string) {
cart := dps.ShoppingService.GetShoppingCart(p.Id, memberId, cartKey)
if cart.Items != nil {
for _, v := range cart.Items {
v.GoodsImage = format.GetGoodsImageUrl(v.GoodsImage)
}
}
// 持续保存cookie
ck, err := ctx.Request.Cookie("_cart")
if err != nil {
ck = &http.Cookie{
Name: "_cart",
Path: "/",
}
}
ck.Value = cart.CartKey
ck.Expires = time.Now().Add(time.Hour * 48)
http.SetCookie(ctx.Response, ck)
d, _ := json.Marshal(cart)
ctx.Response.Write(d)
}
func (this *CartC) cart_AddItem(ctx *web.Context,
p *partner.ValuePartner, memberId int, cartKey string) {
r := ctx.Request
goodsId, _ := strconv.Atoi(r.FormValue("id"))
num, _ := strconv.Atoi(r.FormValue("num"))
item := dps.ShoppingService.AddCartItem(p.Id, memberId, cartKey, goodsId, num)
var d map[string]interface{} = make(map[string]interface{})
if item == nil {
d["error"] = "商品不存在"
} else {
item.GoodsImage = format.GetGoodsImageUrl(item.GoodsImage)
d["item"] = item
}
this.BaseC.JsonOutput(ctx, d)
}
func (this *CartC) cart_RemoveItem(ctx *web.Context,
p *partner.ValuePartner, memberId int, cartKey string) {
var msg gof.Message
r := ctx.Request
goodsId, _ := strconv.Atoi(r.FormValue("id"))
num, _ := strconv.Atoi(r.FormValue("num"))
err := dps.ShoppingService.SubCartItem(p.Id, memberId, cartKey, goodsId, num)
if err != nil {
msg.Message = err.Error()
} else {
msg.Result = true
}
this.BaseC.ResultOutput(ctx, msg)
}
func (this *CartC) Index(ctx *web.Context) {
this.BaseC.ExecuteTemplate(ctx, gof.TemplateDataMap{},
"views/shop/ols/{device}/cart.html",
"views/shop/ols/{device}/inc/header.html",
"views/shop/ols/{device}/inc/footer.html")
}