-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_c.go
211 lines (184 loc) · 5.11 KB
/
order_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Copyright 2014 @ z3q.net.
* name :
* author : jarryliu
* date : 2013-12-12 16:55
* description :
* history :
*/
package partner
import (
"encoding/json"
"github.com/jsix/gof"
"go2o/src/app/cache"
"go2o/src/core/domain/interface/enum"
"go2o/src/core/domain/interface/member"
"go2o/src/core/service/dps"
"go2o/src/x/echox"
"html/template"
"net/http"
"strings"
)
type orderC struct {
}
func (this *orderC) List(ctx *echox.Context) error {
partnerId := getPartnerId(ctx)
shopsJson := cache.GetShopsJson(partnerId)
d := ctx.NewData()
d.Map["shops"] = template.JS(shopsJson)
return ctx.Render(http.StatusOK, "order.list.html", d)
}
func (this *orderC) WaitPaymentList(ctx *echox.Context) error {
partnerId := getPartnerId(ctx)
shopsJson := cache.GetShopsJson(partnerId)
d := ctx.NewData()
d.Map["shops"] = template.JS(shopsJson)
return ctx.Render(http.StatusOK, "order.waitpay_list.html", d)
}
func (this *orderC) Cancel(ctx *echox.Context) error {
if ctx.Request().Method == "POST" {
return this.cancel_post(ctx)
}
d := ctx.NewData()
return ctx.Render(http.StatusOK, "order.cancel.html", d)
}
func (this *orderC) cancel_post(ctx *echox.Context) error {
result := gof.Message{}
partnerId := getPartnerId(ctx)
r := ctx.HttpRequest()
r.ParseForm()
reason := r.FormValue("reason")
err := dps.ShoppingService.CancelOrder(partnerId,
r.FormValue("order_no"), reason)
if err == nil {
result.Result = true
} else {
result.Message = err.Error()
}
return ctx.JSON(http.StatusOK, result)
}
func (this *orderC) View(ctx *echox.Context) error {
partnerId := getPartnerId(ctx)
r := ctx.HttpRequest()
r.ParseForm()
e := dps.ShoppingService.GetOrderByNo(partnerId, r.FormValue("order_no"))
if e == nil {
return ctx.String(http.StatusOK, "无效订单")
}
member := dps.MemberService.GetMember(e.MemberId)
if member == nil {
return ctx.StringOK("无效订单")
}
e.ItemsInfo = strings.Replace(e.ItemsInfo, "\n", "<br />", -1)
if len(e.Note) == 0 {
e.Note = "无备注"
}
js, _ := json.Marshal(e)
var shopName string
var payment string
var orderStateText string
if e.ShopId == 0 {
shopName = "未指定"
} else {
shopName = dps.PartnerService.GetShopValueById(partnerId, e.ShopId).Name
}
payment = enum.GetPaymentName(e.PaymentOpt)
orderStateText = enum.OrderState(e.Status).String()
d := ctx.NewData()
d.Map = map[string]interface{}{
"entity": template.JS(js),
"member": member,
"shopName": shopName,
"payment": payment,
"state": orderStateText,
}
return ctx.Render(http.StatusOK, "order.view.html", d)
}
func (this *orderC) Setup(ctx *echox.Context) error {
partnerId := getPartnerId(ctx)
r := ctx.HttpRequest()
e := dps.ShoppingService.GetOrderByNo(partnerId, r.FormValue("order_no"))
if e == nil {
return ctx.String(http.StatusOK, "无效订单")
}
if e.ShopId == 0 {
return this.setShop(ctx, partnerId, e)
}
return this.setState(ctx, partnerId, e)
}
// 锁定,防止重复下单,返回false,表示正在处理订单
func (this *orderC) lockOrder(ctx *echox.Context) bool {
s := ctx.Session
v := s.Get("pt_order_lock")
if v != nil {
return false
}
s.Set("pt_order_lock", "1")
s.Save()
return true
}
func (this *orderC) releaseOrder(ctx *echox.Context) {
ctx.Session.Remove("pt_order_lock")
ctx.Session.Save()
}
// 订单流程(POST)
func (this *orderC) OrderSetup(ctx *echox.Context) error {
if !this.lockOrder(ctx) {
return ctx.String(http.StatusOK, "请勿频繁操作")
}
var msg gof.Message
partnerId := getPartnerId(ctx)
r := ctx.HttpRequest()
if r.Method == "POST" {
r.ParseForm()
err := dps.ShoppingService.HandleOrder(partnerId, r.FormValue("order_no"))
this.releaseOrder(ctx)
if err != nil {
msg.Message = err.Error()
} else {
msg.Result = true
}
return ctx.JSON(http.StatusOK, msg)
}
return nil
}
func (this *orderC) Payment(ctx *echox.Context) error {
if ctx.Request().Method == "POST" {
return this.payment_post(ctx)
}
partnerId := getPartnerId(ctx)
r := ctx.HttpRequest()
r.ParseForm()
e := dps.ShoppingService.GetOrderByNo(partnerId, r.FormValue("order_no"))
if e == nil {
return ctx.String(http.StatusOK, "无效订单")
} else if e.IsPaid == 1 {
return ctx.String(http.StatusOK, "订单已付款")
}
var shopName string
if e.ShopId == 0 {
shopName = "未指定"
} else {
shopName = dps.PartnerService.GetShopValueById(partnerId, e.ShopId).Name
}
d := ctx.NewData()
d.Map["shopName"] = shopName
d.Map["order"] = *e
return ctx.Render(http.StatusOK, "order.payment.html", d)
}
func (this *orderC) payment_post(ctx *echox.Context) error {
partnerId := getPartnerId(ctx)
r := ctx.HttpRequest()
r.ParseForm()
orderNo := r.FormValue("orderNo")
order := dps.ShoppingService.GetOrderByNo(partnerId, orderNo)
err := dps.MemberService.Charge(partnerId, order.MemberId, member.TypeBalanceSystemCharge, "系统充值(订单付款)", "", order.PayFee)
if err == nil {
err = dps.ShoppingService.PayForOrderByManager(partnerId, orderNo)
}
if err != nil {
return ctx.String(http.StatusOK, "{result:false,message:'"+err.Error()+"'}")
} else {
return ctx.String(http.StatusOK, "{result:true,message:'付款成功'}")
}
}