iedc-go/example/app/vip_order.go

161 lines
4.3 KiB
Go
Raw Normal View History

2022-05-03 14:40:02 +00:00
package app
import (
. "code.hoteas.com/golang/hotime"
2022-05-04 16:29:30 +00:00
. "code.hoteas.com/golang/hotime/common"
"code.hoteas.com/golang/hotime/dri/wechat"
"fmt"
"time"
2022-05-03 14:40:02 +00:00
)
var VipOrderCtr = Ctr{
//创建V订单
"create": func(that *Context) {
if that.Session("user_id").Data == nil {
that.Display(2, "没有登录")
return
}
phone := that.Req.FormValue("phone")
companyName := that.Req.FormValue("company_name")
2022-05-06 03:35:40 +00:00
userName := that.Req.FormValue("user_name")
if len(phone) != 11 || len(companyName) < 4 || len(userName) < 2 {
2022-05-03 14:40:02 +00:00
that.Display(3, "请求参数异常")
return
}
2022-05-04 16:29:30 +00:00
that.Db.Delete("vip_order", Map{"AND": Map{"user_id": that.Session("user_id").Data, "status": 0}})
user := that.Db.Get("user", "*", Map{"id": that.Session("user_id").Data})
if user == nil {
that.Display(2, "找不到此用户")
return
}
wc := that.Db.Get("wechat", "openid", Map{"AND": Map{"appid": that.Config.GetString("wechatAppID"), "user_id": that.Session("user_id").Data}})
if wc == nil {
that.Display(2, "没有获取微信个人信息")
return
}
2022-05-06 03:35:40 +00:00
err := auth(that, phone, companyName, userName)
2022-05-03 14:40:02 +00:00
if err != nil {
2022-05-04 16:29:30 +00:00
that.Display(3, err.Error())
return
}
data := Map{
"sn": "SN" + time.Now().Format("20060102150405") + getSn(),
//"name":"1年VIP会员",
2022-05-05 08:07:01 +00:00
"amount": 36000, //720元
2022-05-04 16:29:30 +00:00
"user_id": user.GetCeilInt64("id"),
"company_id": user.GetCeilInt("company_id"),
"expiration_time": time.Now().Add(365 * 24 * time.Hour).Format("2006-01-02 15:04:05"),
"create_time[#]": "now()",
"modify_time[#]": "now()",
"del_flag": 0,
"status": 0,
}
tp := "购买"
if user.GetString("expiration_time") != "" {
data["old_expiration_time"] = user.GetString("expiration_time")
t, e := time.Parse("2006-01-02 15:04:05", user.GetString("expiration_time"))
fmt.Println(e, "时间创建失败")
if t.Unix() >= time.Now().Unix() {
tp = "续订"
data["expiration_time"] = t.Add(365 * 24 * time.Hour).Format("2006-01-02 15:04:05")
}
}
if user.GetCeilInt("provider_id") != 0 {
data["provider_id"] = user.GetCeilInt("provider_id")
data["amount"] = 36000
//tp=tp
}
2022-05-05 08:07:01 +00:00
//data["amount"] = 1
2022-05-04 16:29:30 +00:00
if user.GetCeilInt("salesman_id") != 0 {
data["salesman_id"] = user.GetCeilInt("salesman_id")
}
2022-05-11 06:55:28 +00:00
data["name"] = companyName + tp + "1年VIP会员"
2022-05-04 16:29:30 +00:00
jsParams, e := wechat.WxPay.GetJsOrder(data.GetCeilInt64("amount"), that.Config.GetString("wechatAppID"), wc.GetString("openid"), data.GetString("name"), data.GetString("sn"), that.Config.GetString("wechatAppNotifyUrl"))
if e != nil {
fmt.Println(e)
that.Display(4, e)
return
}
re := that.Db.Insert("vip_order", data)
fmt.Println(re)
that.Display(0, jsParams)
},
"callback": func(that *Context) {
data, e := wechat.WxPay.CallbackJsOrder(that.Req)
if e != nil {
fmt.Println(e)
//that.Display(4,e)
fmt.Println("返回数据错误", e)
return
}
sn := data.OutTradeNo
amount := int64(data.Amount.Total)
state := data.TradeState
//state:="SUCCESS"
//data := Map{"ces": "das"}
//sn := that.Req.FormValue("sn")
//amount := ObjToCeilInt64(that.Req.FormValue("amount"))
if state != "SUCCESS" {
fmt.Println("购买返回失败", data)
return
}
vipOrder := that.Db.Get("vip_order", "*", Map{"sn": sn})
if vipOrder == nil {
fmt.Println("找不到订单", vipOrder, data)
return
}
user := that.Db.Get("user", "*", Map{"id": vipOrder.GetCeilInt("user_id")})
if user == nil {
fmt.Println("找不到用户", vipOrder, data)
return
}
if vipOrder.GetCeilInt64("amount") != amount {
fmt.Println("金额不符", user, vipOrder, amount, data)
return
}
that.Db.Update("vip_order", Map{"status": 1}, Map{"id": vipOrder.GetCeilInt("id")})
idata := Map{"expiration_time": time.Now().Add(365 * 24 * time.Hour).Format("2006-01-02 15:04:05")}
if user.GetString("expiration_time") != "" {
t, e := time.Parse("2006-01-02 15:04:05", user.GetString("expiration_time"))
fmt.Println(e, "时间创建失败")
if t.Unix() >= time.Now().Unix() {
idata["expiration_time"] = t.Add(365 * 24 * time.Hour).Format("2006-01-02 15:04:05")
}
}
re := that.Db.Update("user", idata, Map{"id": user.GetCeilInt("id")})
if re == 0 {
fmt.Println("购买失败", user, vipOrder, re, data)
2022-05-03 14:40:02 +00:00
return
}
2022-05-04 16:29:30 +00:00
fmt.Println("成功购买", user, vipOrder, re, data)
return
2022-05-03 14:40:02 +00:00
},
}