From 0236148838c80065b06202887a7b469d523c49d0 Mon Sep 17 00:00:00 2001 From: tommy <3405129587@qq.com> Date: Mon, 20 Apr 2020 16:57:20 +0800 Subject: [PATCH] re order --- controllers/client/good.go | 116 ++++++++++++++++++++++++++++++++++++++++++++ controllers/client/live.go | 1 - controllers/client/order.go | 106 ---------------------------------------- libs/im/im.go | 16 +++--- models/customer_order.go | 1 + services/im/im.go | 2 +- services/pay/order.go | 37 +++++++++++++- 7 files changed, 162 insertions(+), 117 deletions(-) create mode 100644 controllers/client/good.go delete mode 100644 controllers/client/order.go diff --git a/controllers/client/good.go b/controllers/client/good.go new file mode 100644 index 0000000..5b75547 --- /dev/null +++ b/controllers/client/good.go @@ -0,0 +1,116 @@ +package client + +import ( + "hudongzhuanjia/controllers" + "hudongzhuanjia/models" + pay_service "hudongzhuanjia/services/pay" + "hudongzhuanjia/utils/code" + "time" +) + +type GoodCtl struct { + controllers.AuthorCtl +} + +// 商品列表 +func (t *GoodCtl) ListGood() { + activityId := t.MustGetInt64("activity_id") + areaId := t.MustGetInt64("area_id") + + goods, err := models.GetGoodsByActivityId(activityId, areaId) + t.CheckErr(err) + + t.JSON(goods) +} + +func (t *GoodCtl) ListOrder() { + activityId := t.MustGetInt64("activity_id") + status := t.MustGetInt("status") + areaId := t.MustGetInt64("area_id") + + activity := new(models.Activity) + exist, err := models.GetById(activity, activityId) + t.CheckErr(err) + t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") + + orders, err := models.GetCustomerOrdersByActivityId(activity.Id, activity.RehearsalId, areaId, status) + t.CheckErr(err) + + t.JSON(orders) +} + +// 下订单 +func (t *GoodCtl) Order() { + userId := t.MustGetUID() // + areaId := t.MustGetInt64("area_id") // 地区id + name := t.MustGet("name") // 收货人名字 + phone := t.MustGet("phone") // 收货电话 + address := t.MustGet("address") // 收货地址 + goodId := t.MustGetInt64("good_id") // 商品id + num := t.MustGetInt("num") // 商品数量 + _type := t.MustGetInt("delivery") // 配送: 1 快递免邮 2 快递 xxx 3 自提 4 到付 + + fee, exist := t.GetDouble("fee") // 快递费用 + if _type == 2 && !exist || fee == 0 { + t.ERROR("fee参数错误", code.MSG_ERR_Param) + return + } + + good := new(models.CustomerGoods) + exist, err := models.GetById(good, goodId) + t.CheckErr(err) + t.Assert(exist, code.MSG_DATA_NOT_EXIST, "商品不存在") + + activity := new(models.Activity) + exist, err = models.GetById(activity, good.ActivityId) + t.CheckErr(err) + t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") + + area := new(models.AreaStore) + exist, err = models.GetById(area, areaId) + t.CheckErr(err) + t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") + + user := models.User{} + exist, err = models.GetById(user, userId) + t.CheckErr(err) + t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") + + res, err := pay_service.UnifiedOrder("欧轩互动-直播商品", user.Openid, int64(int(good.Price*100)*num), 4, userId, activity.Id) + t.CheckErr(err) + + order := models.CustomerOrder{} + order.AreaId = areaId + order.AreaName = area.Name + order.RehearsalId = activity.RehearsalId + order.BuyerId = userId + order.GoodsId = goodId + order.GoodsName = good.Name + order.ActivityId = activity.Id + order.TotalAmount = float64(num) * good.Price + order.PayAmount = order.TotalAmount + order.OutTradeNo = res["out_trade_no"].(string) + order.GoodsNum = num + order.Address = address + order.Receiver = name + order.Phone = phone + order.Postage = fee + order.Status = 0 + order.IsDelete = false + order.CreatedAt = time.Now() + order.UpdatedAt = time.Now() + _, err = order.Add() + t.CheckErr(err) + + res["order_id"] = order.Id + t.JSON(res) +} + +// 为支付的重新支付 +func (t *GoodCtl) Reorder() { + outTradeNo := t.MustGet("out_trade_no") + + res, err := pay_service.ReOrder(outTradeNo) + t.CheckErr(err) + t.JSON(res) +} diff --git a/controllers/client/live.go b/controllers/client/live.go index 66d58ff..40969a6 100644 --- a/controllers/client/live.go +++ b/controllers/client/live.go @@ -21,7 +21,6 @@ type NoticeRedPackEvent struct { type LiveCtl struct { controllers.AuthorCtl - //controllers.BaseCtl } // 详情 diff --git a/controllers/client/order.go b/controllers/client/order.go deleted file mode 100644 index 25d702b..0000000 --- a/controllers/client/order.go +++ /dev/null @@ -1,106 +0,0 @@ -package client - -import ( - "hudongzhuanjia/controllers" - "hudongzhuanjia/models" - pay_service "hudongzhuanjia/services/pay" - "hudongzhuanjia/utils/code" - "time" -) - -type GoodCtl struct { - controllers.AuthorCtl -} - -// 商品列表 -func (t *GoodCtl) ListGood() { - activityId := t.MustGetInt64("activity_id") - areaId := t.MustGetInt64("area_id") - - goods, err := models.GetGoodsByActivityId(activityId, areaId) - t.CheckErr(err) - - t.JSON(goods) -} - -func (t *GoodCtl) ListOrder() { - activityId := t.MustGetInt64("activity_id") - status := t.MustGetInt("status") - areaId := t.MustGetInt64("area_id") - - activity := new(models.Activity) - exist, err := models.GetById(activity, activityId) - t.CheckErr(err) - t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") - - orders, err := models.GetCustomerOrdersByActivityId(activity.Id, activity.RehearsalId, areaId, status) - t.CheckErr(err) - - t.JSON(orders) -} - -// 下订单 -func (t *GoodCtl) Order() { - userId := t.MustGetUID() // - areaId := t.MustGetInt64("area_id") // 地区id - name := t.MustGet("name") // 收货人名字 - phone := t.MustGet("phone") // 收货电话 - address := t.MustGet("address") // 收货地址 - goodId := t.MustGetInt64("good_id") // 商品id - num := t.MustGetInt("num") // 商品数量 - _type := t.MustGetInt("delivery") // 配送: 1 快递免邮 2 快递 xxx 3 自提 4 到付 - - fee, exist := t.GetDouble("fee") // 快递费用 - if _type == 2 && !exist || fee == 0 { - t.ERROR("fee参数错误", code.MSG_ERR_Param) - return - } - - good := new(models.CustomerGoods) - exist, err := models.GetById(good, goodId) - t.CheckErr(err) - t.Assert(exist, code.MSG_DATA_NOT_EXIST, "商品不存在") - - activity := new(models.Activity) - exist, err = models.GetById(activity, good.ActivityId) - t.CheckErr(err) - t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") - - area := new(models.AreaStore) - exist, err = models.GetById(area, areaId) - t.CheckErr(err) - t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") - - user := models.User{} - exist, err = models.GetById(user, userId) - t.CheckErr(err) - t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") - - res, err := pay_service.UnifiedOrder("欧轩互动-直播商品", user.Openid, int64(int(good.Price*100)*num), 4, userId, activity.Id) - t.CheckErr(err) - - order := models.CustomerOrder{} - order.AreaId = areaId - order.AreaName = area.Name - order.RehearsalId = activity.RehearsalId - order.BuyerId = userId - order.GoodsId = goodId - order.GoodsName = good.Name - order.ActivityId = activity.Id - order.TotalAmount = float64(num) * good.Price - order.OutTradeNo = res["out_trade_no"].(string) - order.GoodsNum = num - order.Address = address - order.Receiver = name - order.Phone = phone - order.Postage = fee - order.Status = 0 - order.IsDelete = false - order.CreatedAt = time.Now() - order.UpdatedAt = time.Now() - _, err = order.Add() - t.CheckErr(err) - - res["order_id"] = order.Id - t.JSON(res) -} diff --git a/libs/im/im.go b/libs/im/im.go index 8fc3385..fe99c30 100644 --- a/libs/im/im.go +++ b/libs/im/im.go @@ -18,13 +18,13 @@ var ( TxImHostV4 = "https://console.tim.qq.com/v4" ) -type NoticeType int +type NoticeStatus int -const NoticeLiveRedPackStart NoticeType = 256 // 通知直播用户红包开始了 -const NoticeLiveRedPackEnd NoticeType = 257 // 通知直播用户红包结束了 -const NoticeLiveRedPackGet NoticeType = 258 // 某人摇中红包 -const NoticeShakeRedPackStart NoticeType = 259 // 通知摇红包开始了 -const NoticeShakeRedPackEnd NoticeType = 260 // 通知摇红包结束了 +const NoticeLiveRedPackStart NoticeStatus = 256 // 通知直播用户红包开始了 +const NoticeLiveRedPackEnd NoticeStatus = 257 // 通知直播用户红包结束了 +const NoticeLiveRedPackGet NoticeStatus = 258 // 某人摇中红包 +const NoticeShakeRedPackStart NoticeStatus = 259 // 通知摇红包开始了 +const NoticeShakeRedPackEnd NoticeStatus = 260 // 通知摇红包结束了 type CommonResult struct { ActionStatus string `json:"ActionStatus"` @@ -80,7 +80,7 @@ type SendGroupSystemNotificationParam struct { ToMembersAccount []string `json:"ToMembers_Account,omitempty"` } -func SendGroupSystemNotification(groupId string, noticeType NoticeType, data map[string]interface{}, members ...string) error { +func SendGroupSystemNotification(groupId string, status NoticeStatus, data map[string]interface{}, members ...string) error { sig, err := GenSig("admin") if err != nil { return err @@ -90,7 +90,7 @@ func SendGroupSystemNotification(groupId string, noticeType NoticeType, data map TxImHostV4, SdkAppid, sig, random) var m = make(map[string]interface{}) - m["type"] = noticeType + m["status"] = status m["data"] = data content, err := json.Marshal(&m) if err != nil { diff --git a/models/customer_order.go b/models/customer_order.go index 637aee3..840d8fb 100644 --- a/models/customer_order.go +++ b/models/customer_order.go @@ -26,6 +26,7 @@ type CustomerOrder struct { GoodsName string `json:"goods_name" xorm:"not null default '' comment('商品名字') VARCHAR(255)"` GoodsNum int `json:"goods_num" xorm:"not null default 0 comment('商品数量') INT(11)"` TotalAmount float64 `json:"total_amount" xorm:"not null default 0.00 comment('订单总额') DECIMAL(18)"` + PayAmount float64 `json:"pay_amount" xorm:"not null default 0.00 comment('支付金额') DECIMAL(18)"` Postage float64 `json:"postage" xorm:"not null default 0.00 comment('邮费[0免邮]') DECIMAL(18)"` Status int `json:"status" xorm:"not null default 0 comment('订单状态[0未支付1已支付2待发货3已发货4确认收货5申请退款6已退款]')"` // 快递信息 diff --git a/services/im/im.go b/services/im/im.go index f0f2ede..3f84947 100644 --- a/services/im/im.go +++ b/services/im/im.go @@ -6,7 +6,7 @@ import ( "hudongzhuanjia/models" ) -func SendNoticeByActivityId(activityId int64, _type im.NoticeType, data map[string]interface{}, members ...string) error { +func SendNoticeByActivityId(activityId int64, _type im.NoticeStatus, data map[string]interface{}, members ...string) error { live := new(models.LiveConfig) exist, err := live.GetByActivityId(activityId) if err != nil { diff --git a/services/pay/order.go b/services/pay/order.go index ba426ad..53a3dbb 100644 --- a/services/pay/order.go +++ b/services/pay/order.go @@ -38,6 +38,10 @@ func PutOrderDelayQueue(body, outTradeNo, openId string, amount, status int, exp expires = time.Now().Add(2 * time.Hour).Unix() // 2 个小时 } + if delay == 0 { + delay = 100 + } + orderDelayQueue <- &orderDelayQueueParam{ First: true, Expires: expires, @@ -83,7 +87,7 @@ func loopUnifiedOrder() { // 首次进入不延迟 if !param.First { - time.Sleep(time.Duration(param.Delay) * time.Second) + time.Sleep(time.Duration(param.Delay) * time.Millisecond) } param.First = false @@ -192,6 +196,37 @@ func UnifiedOrder(body, openid string, fee, goodType, userId, activityId int64) }, nil } +func ReOrder(outTradeNo string) (map[string]interface{}, error) { + userOrder := new(models.UserOrder) + exist, err := userOrder.GetByOutTradeNo(outTradeNo) + if err != nil { + return nil, err + } + if !exist { + return nil, errors.New("订单不存在") + } + + timestamp := strconv.FormatInt(time.Now().Unix(), 10) + nonceStr := utils.RandomStr(32) + //获取H5支付需要的paySign + pac := "prepay_id=" + userOrder.PrepayId + paySign := core2.JsapiSign(Appid, timestamp, nonceStr, pac, core2.SignType_MD5, ApiKey) + if userOrder.TimeExpire <= core2.FormatTime(time.Now()) { + go PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 0, 0) + } + return map[string]interface{}{ + "appid": Appid, + "timestamp": timestamp, + "nonce_str": nonceStr, + "package": pac, + "sign_type": core2.SignType_MD5, + "pay_sign": paySign, + "out_trade_no": outTradeNo, + "user_order_id": userOrder.Id, + }, nil + +} + // Notify type NotifyRequest struct { ReturnCode string `xml:"return_code,omitempty" json:"return_code,omitempty"`