diff --git a/controllers/client/bully_screen.go b/controllers/client/bully_screen.go index 7f01faf..555ad9e 100644 --- a/controllers/client/bully_screen.go +++ b/controllers/client/bully_screen.go @@ -7,7 +7,6 @@ import ( bully_screen_service "hudongzhuanjia/services/bully_reward" "hudongzhuanjia/services/pay" "hudongzhuanjia/utils/code" - "strings" "time" "github.com/ouxuanserver/osmanthuswine/src/core" @@ -53,8 +52,7 @@ func (t *BullyScreenCtl) PaScreen() { // todo:微信直接付款 // 调用微信统一下单接口 amount := bullyScreenServer.Price * second - ip := strings.Split(t.Request.OriginRequest.RemoteAddr, ":") - res, err := pay_service.UnifiedOrder("欧轩互动-霸屏支付", ip[0], user.Openid, int64(amount*100), 1, user.Id, activityId) + res, err := pay_service.UnifiedOrder("欧轩互动-霸屏支付", user.Openid, int64(amount*100), 1, user.Id, activityId) t.CheckErr(err) history := &models.BullyScreenHistory{ diff --git a/controllers/client/live.go b/controllers/client/live.go index f886ab6..66d58ff 100644 --- a/controllers/client/live.go +++ b/controllers/client/live.go @@ -8,7 +8,6 @@ import ( red_envelope_service "hudongzhuanjia/services/red_envelope" "hudongzhuanjia/utils" "hudongzhuanjia/utils/code" - "strings" "time" ) @@ -104,8 +103,7 @@ func (t *LiveCtl) SendLiveRedPack() { t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") - ip := strings.Split(t.Request.OriginRequest.RemoteAddr, ":") - res, err := pay_service.UnifiedOrder("欧轩互动-直播红包", ip[0], user.Openid, int64(amount*100), 3, activityId, userId) + res, err := pay_service.UnifiedOrder("欧轩互动-直播红包", user.Openid, int64(amount*100), 3, activityId, userId) t.CheckErr(err) rule := models.LiveRedEnvelopeRule{} @@ -125,6 +123,7 @@ func (t *LiveCtl) SendLiveRedPack() { record := new(models.ShakeRedEnvelopeRecord) record.AreaId = area.Id record.AreaName = area.Name + record.RedEnvelopeType = 1 record.LiveRedEnvelopeRuleId = rule.Id record.ActivityId = activityId record.Name = activity.Name @@ -136,47 +135,27 @@ func (t *LiveCtl) SendLiveRedPack() { t.CheckErr(err) } - res["red_pack_info_id"] = rule.Id + res["live_red_envelope_rule_id"] = rule.Id t.JSON(res) } -// 支付之后可以遍历查询是否成功 -- 前端发送消息 -// 不建议, 通过im 系统进行通知 -func (t *LiveCtl) QueryLiveRedPack() { - outTradeNo := t.MustGet("out_trade_no") - res, err := pay_service.OrderQuery(outTradeNo) - t.CheckErr(err) - info := new(models.LiveRedEnvelopeRule) - exist, err := info.GetByOutTradeNo(outTradeNo) - t.CheckErr(err) - t.Assert(exist, code.MSG_LIVE_RED_PACK_INFO_NOT_EXIST, "直播红包信息不存在") - if res.TradeState == pay_service.CODE_TRADE_SUCCESS { - info.Status = 1 - info.UpdateStatusById(info.Id, info.Status) - t.JSON(map[string]interface{}{ - "red_pack_info_id": info.Id, - "status": info.Status, - }) - } else { - t.JSON(map[string]interface{}{ - "red_pack_info_id": info.Id, - "status": info.Status, - }) - } -} - // 领取红包 func (t *LiveCtl) GetLiveRedPack() { liveRedPackInfoId := t.MustGetInt64("live_red_pack_info_id") userId := t.MustGetUID() + if liveRedPackInfoId == 0 { + t.ERROR("live_red_pack_info_id不为0", code.MSG_ERR_Param) + return + } + user := models.User{} exist, err := models.GetById(&user, userId) t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "不存在用户") redPack := new(models.ShakeRedEnvelopeRecord) - exist, err = redPack.GetByInfoId(liveRedPackInfoId) + exist, err = redPack.GetLiveRedPackRecord(liveRedPackInfoId) t.CheckErr(err) if !exist { // 通知其他的人 @@ -185,17 +164,16 @@ func (t *LiveCtl) GetLiveRedPack() { } // 乐观锁 ==> 防止并发 - redPack.OpenId = user.Openid - redPack.Receiver = user.Id + redPack.UserId = user.Id redPack.TransferType = 1 redPack.PartnerTradeNo = utils.RandomStr(32) - row, err := redPack.UpdateStatusById(redPack.Id, 1) + row, err := redPack.UpdateAllColsById(redPack.Id) t.CheckErr(err) if row != 1 { t.ERROR("红包被领完了", code.MSG_LIVE_RED_PACK_NOT_EXIST) return } - pay_service.PutTransferDelayQueue("欧轩互动-红包活动", user.Openid, redPack.PartnerTradeNo, redPack.Amount, 5, 5*60) + pay_service.PutTransferDelayQueue("欧轩互动-红包活动", user.Openid, redPack.PartnerTradeNo, int(redPack.Amount*100), 5, 5*60) t.JSON(redPack) } diff --git a/controllers/client/reward.go b/controllers/client/reward.go index 3d1d992..54f039d 100644 --- a/controllers/client/reward.go +++ b/controllers/client/reward.go @@ -8,7 +8,6 @@ import ( pay_service "hudongzhuanjia/services/pay" "hudongzhuanjia/utils" "hudongzhuanjia/utils/code" - "strings" "time" "github.com/ouxuanserver/osmanthuswine/src/core" @@ -53,8 +52,7 @@ func (t *RewardCtl) Reward() { t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") - ip := strings.Split(t.Request.OriginRequest.RemoteAddr, ":") - res, err := pay_service.UnifiedOrder("欧轩互动-打赏支付", ip[0], user.Openid, int64(amount*100), 2, user.Id, activityId) + res, err := pay_service.UnifiedOrder("欧轩互动-打赏支付", user.Openid, int64(amount*100), 2, user.Id, activityId) t.CheckErr(err) _, err = core.GetXormAuto().InsertOne(&models.RewardHistory{ diff --git a/controllers/pc/shake_red_envelope.go b/controllers/pc/shake_red_envelope.go index e29baa9..5a7cd31 100644 --- a/controllers/pc/shake_red_envelope.go +++ b/controllers/pc/shake_red_envelope.go @@ -4,11 +4,14 @@ import ( "fmt" "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" + "hudongzhuanjia/libs/im" "hudongzhuanjia/models" + im_service "hudongzhuanjia/services/im" red_envelope_service "hudongzhuanjia/services/red_envelope" "hudongzhuanjia/utils" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" + "time" ) type ShakeRedEnvelopeCtl struct { @@ -65,12 +68,25 @@ func (t *ShakeRedEnvelopeCtl) Start() { t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "摇红包不存在") + envelope := new(models.ShakeRedEnvelopeActivity) + exist, err = envelope.GetPayedById(rule.ShakeRedEnvelopeActivityId) + t.CheckErr(err) + t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在") + if rule.ShakeRedEnvelopeStatus != define.StatusReady { t.ERROR(fmt.Sprintf("该活动%s", rule.ShakeRedEnvelopeStatus), code.MSG_ERR) + return } _, err = rule.UpdateStatus(ruleId, define.StatusRunning) t.CheckErr(err) + + go im_service.SendNoticeByActivityId(envelope.ActivityId, im.NoticeShakeRedPackStart, + map[string]interface{}{ + "shake_red_envelope_rule_id": rule.Id, + "timestamp": time.Now().Unix(), + }) + t.SUCCESS("success") } @@ -83,9 +99,21 @@ func (t *ShakeRedEnvelopeCtl) Stop() { t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河不存在") + envelope := new(models.ShakeRedEnvelopeActivity) + exist, err = envelope.GetPayedById(rule.ShakeRedEnvelopeActivityId) + t.CheckErr(err) + t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在") + rule.ShakeRedEnvelopeStatus = define.StatusEnding _, err = rule.UpdateStatus(ruleId, rule.ShakeRedEnvelopeStatus) t.CheckErr(err) + + go im_service.SendNoticeByActivityId(envelope.ActivityId, im.NoticeShakeRedPackEnd, + map[string]interface{}{ + "shake_red_envelope_rule_id": rule.Id, + "timestamp": time.Now().Unix(), + }) + t.SUCCESS("success") } diff --git a/models/bully_screen_history.go b/models/bully_screen_history.go index d7caac0..1e2989b 100644 --- a/models/bully_screen_history.go +++ b/models/bully_screen_history.go @@ -9,24 +9,25 @@ const BullyScreenHistoryTableName = TableNamePrefix + "bully_screen_history" //霸屏历史表 type BullyScreenHistory struct { - Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"` - BullyScreenServerId int64 `json:"bully_screen_server_id" xorm:"not null comment('霸屏服务得id') INT(11)"` - OutTradeNo string `json:"out_trade_no" xorm:"not null default '' comment('微信订单号') VARCHAR(128)"` - CustomerId int64 `json:"customer_id" xorm:"not null comment('客户id') INT(11)"` - ActivityId int64 `json:"activity_id" xorm:"not null comment('互动id') INT(11)"` - UserId int64 `json:"user_id" xorm:"not null comment('用户得id') INT(11)"` - Nickname string `json:"nickname" xorm:"not null default('') comment('发送者昵称') VARCHAR(128)"` - RehearsalId int64 `json:"rehearsal_id" xorm:"not null default(0) comment('彩排id/ 0是正式') INT(11)"` - Style int `json:"style" xorm:"not null comment('服务样式') INT(11)"` - Second int `json:"second" xorm:"not null comment('霸屏时间(秒)') INT(11)"` - Content string `json:"content" xorm:"not null comment('内容') TEXT"` - Status int `json:"status" xorm:"not null default(0) comment('[-1未支付,0未审核,1未通过,2已通过,3已推送,4已作废]') INT(11)"` - Amount float64 `json:"amount" xorm:"not null default 0.00 comment('霸屏金额') DECIMAL(10)"` - ReviewTime int64 `json:"review_time" xorm:"not null comment('审核的时间') INT(11)"` - Version int64 `json:"version" xorm:"not null version comment('乐观锁') INT(11)"` - IsDelete bool `json:"is_delete" xorm:"not null default(0) comment('删除') TINYINT(1)"` - CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"` - UpdatedAt time.Time `json:"updated_at" xorm:"not null updated comment('更新时间') DATETIME"` + Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"` + IsDelete bool `json:"is_delete" xorm:"not null default(0) comment('删除') TINYINT(1)"` + CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"` + UpdatedAt time.Time `json:"updated_at" xorm:"not null updated comment('更新时间') DATETIME"` + + BullyScreenServerId int64 `json:"bully_screen_server_id" xorm:"not null default 0 comment('霸屏服务得id') INT(11)"` + OutTradeNo string `json:"out_trade_no" xorm:"not null default '' comment('微信订单号') VARCHAR(128)"` + CustomerId int64 `json:"customer_id" xorm:"not null default 0 comment('客户id') INT(11)"` + ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('互动id') INT(11)"` + UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户得id') INT(11)"` + Nickname string `json:"nickname" xorm:"not null default '' comment('发送者昵称') VARCHAR(128)"` + RehearsalId int64 `json:"rehearsal_id" xorm:"not null default 0 comment('彩排id/ 0是正式') INT(11)"` + Style int `json:"style" xorm:"not null default 0 comment('服务样式') INT(11)"` + Second int `json:"second" xorm:"not null default 0 comment('霸屏时间(秒)') INT(11)"` + Content string `json:"content" xorm:"not null comment('内容') TEXT"` + Status int `json:"status" xorm:"not null default(0) comment('[-1未支付,0未审核,1未通过,2已通过,3已推送,4已作废]') INT(11)"` + Amount float64 `json:"amount" xorm:"not null default 0.00 comment('霸屏金额') DECIMAL(18)"` + ReviewTime int64 `json:"review_time" xorm:"not null default 0 comment('审核的时间') INT(11)"` + Version int64 `json:"version" xorm:"not null version comment('乐观锁') INT(11)"` } func (t *BullyScreenHistory) TableName() string { diff --git a/models/customer_goods.go b/models/customer_goods.go index 36adcb0..4d34ce6 100644 --- a/models/customer_goods.go +++ b/models/customer_goods.go @@ -18,7 +18,7 @@ type CustomerGoods struct { FixedField string `json:"fixed_field" xorm:"not null default '' comment('固定不可改字段,|分隔') VARCHAR(255)"` Stock int `json:"stock" xorm:"not null default 0 comment('库存-1的时候无上限') INT(18)"` Name string `json:"name" xorm:"not null default '' comment('商品名称') VARCHAR(255)"` - Price float64 `json:"price" xorm:"not null default '' comment('商品单价') DECIMAL(18,2)"` + Price float64 `json:"price" xorm:"not null default '' comment('商品单价') DECIMAL(18)"` Desc string `json:"desc" xorm:"not null default '' comment('商品介绍') VARCHAR(255)"` // 无关变量 diff --git a/models/customer_order.go b/models/customer_order.go index 2c2f9d4..861aa6e 100644 --- a/models/customer_order.go +++ b/models/customer_order.go @@ -1,8 +1,9 @@ package models import ( - "github.com/ouxuanserver/osmanthuswine/src/core" "time" + + "github.com/ouxuanserver/osmanthuswine/src/core" ) const CustomerOrderTableName = TableNamePrefix + "customer_order" @@ -24,8 +25,8 @@ type CustomerOrder struct { GoodsId int64 `json:"goods_id" xorm:"not null default 0 comment('customer_goods表id') BIGINT(20)"` 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,2)"` - Postage float64 `json:"postage" xorm:"not null default 0.00 comment('邮费[0免邮]') DECIMAL(18,2)"` + TotalAmount float64 `json:"total_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已退款]')"` // 快递信息 Receiver string `json:"receiver" xorm:"not null default '' comment('收件人') VARCHAR(128)"` diff --git a/models/init_models.go b/models/init_models.go index f712ccf..c77b2c4 100644 --- a/models/init_models.go +++ b/models/init_models.go @@ -87,7 +87,6 @@ func init() { new(LiveViewer), new(LiveConfig), new(LiveRedEnvelopeRule), - new(LiveRedPack), new(UserTransfer), ) fmt.Printf("error=======>%v\n\n", err) diff --git a/models/live_config.go b/models/live_config.go index 3d96af8..eb32b2f 100644 --- a/models/live_config.go +++ b/models/live_config.go @@ -8,35 +8,37 @@ import ( const LiveConfigTN = TableNamePrefix + "live_config" type LiveConfig struct { - Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"` - IsDelete bool `json:"-" xorm:"not null default 0 comment('是否删除') TINYINT(1)"` - CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"` - UpdatedAt time.Time `json:"updated_at" xorm:"not null updated default CURRENT_TIMESTAMP comment('更新时间') TIMESTAMP"` - LiveSwitch int `json:"live_switch" xorm:"not null default 0 comment('直播开关0关1开') INT(11)"` - ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('互动id') INT(11)"` - LiveRoomId string `json:"live_room_id" xorm:"not null default '' comment('直播间id') VARCHAR(128)"` - AdaptationFunc []interface{} `json:"adaptation_func" xorm:"json default '' comment('选中的适配功能及互动已买的服务id,json格式') VARCHAR(255)"` - StartTime time.Time `json:"start_time" xorm:"not null default '1970-01-01 08:00:00' comment('开播时间') DATETIME"` - EndTime time.Time `json:"end_time" xorm:"not null default '1970-01-01 08:00:00' comment('直播结束时间') DATETIME"` - LiveH5Url string `json:"live_h5_url" xorm:"not null default '' comment('h5直播地址') VARCHAR(255)"` - AdminLiveUrl string `json:"admin_live_url" xorm:"not null default '' comment('管理员直播地址,即直播页面中管理员登录页面') VARCHAR(255)"` - LiveH5Qrcode string `json:"live_h5_qrcode" xorm:"not null default '' comment('h5直播地址二维码') VARCHAR(255)"` - UnpayOrderTime string `json:"unpay_order_time" xorm:"not null default '' comment('待支付订单直播结束延长的时间戳') VARCHAR(128)"` - LiveType int `json:"live_type" xorm:"not null default 1 comment('直播方式1推流方式2app直播') INT(1)"` - VideoPushUrl string `json:"video_push_url" xorm:"not null default '' comment('视频推流地址') VARCHAR(255)"` - VideoPullUrlRtmp string `json:"video_pull_url_rtmp" xorm:"not null default '' comment('视频拉流地址即播放地址') VARCHAR(255)"` - VideoPullUrlFlv string `json:"video_pull_url_flv" xorm:"not null default '' comment('视频拉流地址即播放地址') VARCHAR(255)"` - VideoPullUrlM3u8 string `json:"video_pull_url_m3u8" xorm:"not null default '' comment('视频拉流地址即播放地址') VARCHAR(255)"` - LiveStartPage string `json:"live_start_page" xorm:"not null default '' comment('直播启动页图片') VARCHAR(255)"` - BeforeStartPage string `json:"before_start_page" xorm:"not null default '' comment('开播前图片') VARCHAR(255)"` - ActivityDesc string `json:"activity_desc" xorm:"not null default '' comment('活动介绍页图片') VARCHAR(255)"` - SharePosterImg string `json:"share_poster_img" xorm:"not null default '' comment('分享的海报图片') VARCHAR(255)"` - Status int `json:"status" xorm:"not null default 1 comment('直播的状态1未开始2进行中3已结束') VARCHAR(255)"` - Announcement string `json:"announcement" xorm:"not null default '' comment('公告内容') VARCHAR(255)"` - WatchNum int `json:"watch_num" xorm:"not null default 0 comment('观看人数') BIGINT(11)"` - LikeNum int `json:"like_num" xorm:"not null default 0 comment('点赞数') BIGINT(11)"` - ImGroupId string `json:"im_group_id" xorm:"not null default '' comment('腾讯im聊天群id') VARCHAR(255)"` - ImGroupName string `json:"im_group_name" xorm:"not null default '' comment('聊天群名称') VARCHAR(255)"` + Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"` + IsDelete bool `json:"-" xorm:"not null default 0 comment('是否删除') TINYINT(1)"` + CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"` + UpdatedAt time.Time `json:"updated_at" xorm:"not null updated default CURRENT_TIMESTAMP comment('更新时间') TIMESTAMP"` + + LiveSwitch int `json:"live_switch" xorm:"not null default 0 comment('直播开关0关1开') INT(11)"` + ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('互动id') INT(11)"` + LiveRoomId string `json:"live_room_id" xorm:"not null default '' comment('直播间id') VARCHAR(128)"` + AdaptationFunc []interface{} `json:"adaptation_func" xorm:"json not null default '' comment('选中的适配功能及互动已买的服务id,json格式') VARCHAR(255)"` + StartTime time.Time `json:"start_time" xorm:"not null default '1970-01-01 08:00:00' comment('开播时间') DATETIME"` + EndTime time.Time `json:"end_time" xorm:"not null default '1970-01-01 08:00:00' comment('直播结束时间') DATETIME"` + LiveH5Url string `json:"live_h5_url" xorm:"not null default '' comment('h5直播地址') VARCHAR(255)"` + AdminLiveUrl string `json:"admin_live_url" xorm:"not null default '' comment('管理员直播地址,即直播页面中管理员登录页面') VARCHAR(255)"` + LiveH5Qrcode string `json:"live_h5_qrcode" xorm:"not null default '' comment('h5直播地址二维码') VARCHAR(255)"` + LiveH5AdminQrcode string `json:"live_h_5_admin_qrcode" xorm:"not null default '' comment('管理员直播地址二维码') VARCHAR(255)"` + UnpayOrderTime string `json:"unpay_order_time" xorm:"not null default '' comment('待支付订单直播结束延长的时间戳') VARCHAR(128)"` + LiveType int `json:"live_type" xorm:"not null default 1 comment('直播方式1推流方式2app直播') INT(1)"` + VideoPushUrl string `json:"video_push_url" xorm:"not null default '' comment('视频推流地址') VARCHAR(255)"` + VideoPullUrlRtmp string `json:"video_pull_url_rtmp" xorm:"not null default '' comment('视频拉流地址即播放地址') VARCHAR(255)"` + VideoPullUrlFlv string `json:"video_pull_url_flv" xorm:"not null default '' comment('视频拉流地址即播放地址') VARCHAR(255)"` + VideoPullUrlM3u8 string `json:"video_pull_url_m3u8" xorm:"not null default '' comment('视频拉流地址即播放地址') VARCHAR(255)"` + LiveStartPage string `json:"live_start_page" xorm:"not null default '' comment('直播启动页图片') VARCHAR(255)"` + BeforeStartPage string `json:"before_start_page" xorm:"not null default '' comment('开播前图片') VARCHAR(255)"` + ActivityDesc string `json:"activity_desc" xorm:"not null default '' comment('活动介绍页图片') VARCHAR(255)"` + SharePosterImg string `json:"share_poster_img" xorm:"not null default '' comment('分享的海报图片') VARCHAR(255)"` + Status int `json:"status" xorm:"not null default 1 comment('直播的状态1未开始2进行中3已结束') TINYINT(1)"` + Announcement string `json:"announcement" xorm:"not null default '' comment('公告内容') VARCHAR(255)"` + WatchNum int `json:"watch_num" xorm:"not null default 0 comment('观看人数') INT(11)"` + LikeNum int `json:"like_num" xorm:"not null default 0 comment('点赞数') INT(11)"` + ImGroupId string `json:"im_group_id" xorm:"not null default '' comment('腾讯im聊天群id') VARCHAR(255)"` + ImGroupName string `json:"im_group_name" xorm:"not null default '' comment('聊天群名称') VARCHAR(255)"` } func (t *LiveConfig) TableName() string { diff --git a/models/live_red_pack_info.go b/models/live_red_envelope_rule.go similarity index 84% rename from models/live_red_pack_info.go rename to models/live_red_envelope_rule.go index 47bd9a7..550e8f1 100644 --- a/models/live_red_pack_info.go +++ b/models/live_red_envelope_rule.go @@ -1,14 +1,15 @@ package models import ( - "github.com/ouxuanserver/osmanthuswine/src/core" "time" + + "github.com/ouxuanserver/osmanthuswine/src/core" ) -const LiveRedPackInfoTN = TableNamePrefix + "live_red_pack_info" +const LiveRedPackInfoTN = TableNamePrefix + "live_red_envelope_rule" type LiveRedEnvelopeRule struct { - Id int64 `json:"live_red_pack_info_id" xorm:"not null pk autoincr INT(11)"` + Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"` IsDelete bool `json:"-" xorm:"not null default 0 comment('是否删除') TINYINT(1)"` CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"` UpdatedAt time.Time `json:"updated_at" xorm:"not null updated default CURRENT_TIMESTAMP comment('更新时间') TIMESTAMP"` @@ -17,7 +18,7 @@ type LiveRedEnvelopeRule struct { ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('互动id') INT(11)"` GroupId string `json:"group_id" xorm:"not null default '' comment('聊天室地址') VARCHAR(128)"` Prompt string `json:"prompt" xorm:"not null default 0 comment('祝福语') VARCHAR(255)"` - Amount float64 `json:"amount" xorm:"not null default 0 comment('红包金额') DECIMAL(18,2)"` + Amount float64 `json:"amount" xorm:"not null default 0 comment('红包金额') DECIMAL(18)"` OutTradeNo string `json:"out_trade_no" xorm:"not null default '' comment('订单号') VARCHAR(128)"` Error string `json:"error" xorm:"not null default '' comment('出现错误') VARCHAR(255)"` Status int `json:"status" xorm:"not null default 0 comment('-1尚未支付0支付成功1已推送2已作废')"` @@ -41,10 +42,6 @@ func (t *LiveRedEnvelopeRule) GetByOutTradeNo(outTradeNo string) (bool, error) { return core.GetXormAuto().Where("is_delete=0 and out_trade_no=?", outTradeNo).Get(t) } -func (t *LiveRedEnvelopeRule) UpdateStatusById(id interface{}, status int) (int64, error) { - return core.GetXormAuto().Where("id=?", id).Cols("status").Update(&LiveRedPack{Status: status}) -} - func (t *LiveRedEnvelopeRule) UpdateStatusByOutTradeNo(outTradeNo string, status int) (int64, error) { t.Status = status return core.GetXormAuto().Where("is_delete=0 and status=0 and out_trade_no=?", outTradeNo).Update(t) diff --git a/models/live_red_pack.go b/models/live_red_pack.go deleted file mode 100644 index 7860929..0000000 --- a/models/live_red_pack.go +++ /dev/null @@ -1,53 +0,0 @@ -package models - -import ( - "github.com/ouxuanserver/osmanthuswine/src/core" - "time" -) - -const LiveRedPackTN = TableNamePrefix + "live_red_pack" - -type LiveRedPack struct { - Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"` - IsDelete bool `json:"-" xorm:"not null default 0 comment('是否删除') TINYINT(1)"` - CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"` - UpdatedAt time.Time `json:"updated_at" xorm:"not null updated default CURRENT_TIMESTAMP comment('更新时间') TIMESTAMP"` - - LiveRedPackInfoId int64 `json:"live_red_pack_info_id" xorm:"not null default 0 comment('红包信息id') INT(11)"` - ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('互动id') INT(11)"` - Receiver int64 `json:"receiver" xorm:"not null default 0 comment('[0未领取/非0领取用户id]') INT(11)"` - OpenId string `json:"open_id" xorm:"not null default '' comment('用户openid') VARCHAR(128)"` - Amount int `json:"amount" xorm:"not null default 0 comment('红包金额, 分') INT(18)"` - TransferType int `json:"transfer_type" xorm:"not null default 0 comment('转账方式[0微信红包1微信零钱]') TINYINT(1)"` - PartnerTradeNo string `json:"partner_trade_no" xorm:"not null default '' comment('转账单号') VARCHAR(128) "` - Status int `json:"status" xorm:"not null default 0 comment('0 未被领取 1 已被领取 2 已发送 3 出现错误') TINYINT(1)"` - Version int `json:"version" xorm:"not null version comment('乐观锁') INT(11)"` -} - -func (t LiveRedPack) TableName() string { - return LiveRedPackTN -} - -func (t *LiveRedPack) Add() (int64, error) { - return core.GetXormAuto().InsertOne(t) -} - -func (t *LiveRedPack) GetByInfoId(infoId int64) (bool, error) { - return core.GetXormAuto().Where("live_red_package_info_id=? and is_delete=0", infoId).Get(t) -} - -func (t *LiveRedPack) UpdateStatusById(id interface{}, status int) (int64, error) { - t.Status = status - return core.GetXormAuto().Where("id=?", id). - Cols("receiver, open_id, transfer_type, transfer_no, status").Update(t) -} - -func GetRedPacksByStatus(status interface{}) ([]*LiveRedPack, error) { - redPacks := make([]*LiveRedPack, 0) - err := core.GetXormAuto().Where("is_delete=0 and status=?", status).Find(&redPacks) - return redPacks, err -} - -func (t *LiveRedPack) GetByTransferNo(transferNo string) (bool, error) { - return core.GetXormAuto().Where("is_delete=0 and transfer_no=?", transferNo).Get(t) -} diff --git a/models/real_sign_list.go b/models/real_sign_list.go index 3260ec0..998f481 100644 --- a/models/real_sign_list.go +++ b/models/real_sign_list.go @@ -9,12 +9,13 @@ import ( const RealSignListTN = TableNamePrefix + "real_sign_list" type RealSignList struct { - Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"` - ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('主活动id') INT(11)"` - JsonList string `json:"json_list" xorm:"not null comment('名单记录') TEXT"` - IsDelete bool `json:"is_delete" xorm:"default(0)" description:"是否删除"` - CreatedAt time.Time `json:"created_at" xorm:"created" description:"创建时间"` - UpdatedAt time.Time `json:"updated_at" xorm:"updated" description:"更新时间"` + Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"` + IsDelete bool `json:"is_delete" xorm:"not null default(0) comment('是否删除') TINYINT(1)"` + CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') TIMESTAMP"` + UpdatedAt time.Time `json:"updated_at" xorm:"not null default 'CURRENT_TIMESTAMP' updated comment('更新时间') TIMESTAMP"` + + ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('主活动id') INT(11)"` + JsonList string `json:"json_list" xorm:"not null comment('名单记录') TEXT"` } func (t *RealSignList) TableName() string { diff --git a/models/reward_history.go b/models/reward_history.go index 409e542..ce1d07c 100644 --- a/models/reward_history.go +++ b/models/reward_history.go @@ -1,38 +1,42 @@ package models import ( - "github.com/ouxuanserver/osmanthuswine/src/core" "time" + + "github.com/ouxuanserver/osmanthuswine/src/core" ) const ReWardHistoryTableName = TableNamePrefix + "reward_history" //打赏历史 type RewardHistory struct { - Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"` - OutTradeNo string `json:"out_trade_no" xorm:"not null default '' comment('微信订单号') VARCHAR(128)"` - CustomerId int64 `json:"customer_id" xorm:"not null comment('客户id') INT(11)"` - RewardServerId int64 `json:"reward_server_id" xorm:"not null comment('打赏服务id') INT(11)"` - RehearsalId int64 `json:"rehearsal_id" xorm:"not null default(0) comment('彩排id/0正式') INT(11)"` - UserId int64 `json:"user_id" xorm:"not null comment('用户得id') INT(11)"` - User *User `json:"user" xorm:"-" description:"用户信息"` - Content string `json:"content" xorm:"not null comment('内容') text"` - Amount float64 `json:"amount" xorm:"not null default(0.0) comment('金额') DECIMAL"` - RewardAmount string `json:"reward_amount" xorm:"-" description:"同上, 字符串"` - Status int `json:"status" xorm:"not null default(0) comment('-1未支付,0未审核,1未通过,2已通过,3已推送,4已作废') INT(11)"` - ReviewTime int64 `json:"review_time" xorm:"not null default(0) comment('审核时间') INT(11)"` - Version int64 `json:"version" xorm:"not null version comment('乐观锁') INT(11)"` - IsDelete bool `json:"is_delete" xorm:"not null default(0)"` - CreatedAt time.Time `json:"create_at" xorm:"not null default(CURRENT_TIMESTAMP) created comment('创建时间') TIMESTAMP"` - UpdatedAt time.Time `json:"update_at" xorm:"not null default(CURRENT_TIMESTAMP) updated comment('更新时间') TIMESTAMP"` + Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"` + IsDelete bool `json:"is_delete" xorm:"not null default(0) comment('是否删除')"` + CreatedAt time.Time `json:"create_at" xorm:"not null created comment('创建时间') TIMESTAMP"` + UpdatedAt time.Time `json:"update_at" xorm:"not null default(CURRENT_TIMESTAMP) updated comment('更新时间') TIMESTAMP"` + + OutTradeNo string `json:"out_trade_no" xorm:"not null default '' comment('微信订单号') VARCHAR(128)"` + CustomerId int64 `json:"customer_id" xorm:"not null default 0 comment('客户id') INT(11)"` + RewardServerId int64 `json:"reward_server_id" xorm:"not null default 0 comment('打赏服务id') INT(11)"` + RehearsalId int64 `json:"rehearsal_id" xorm:"not null default 0 comment('彩排id/0正式') INT(11)"` + UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户得id') INT(11)"` + Content string `json:"content" xorm:"not null comment('内容') text"` + Amount float64 `json:"amount" xorm:"not null default(0.00) comment('金额') DECIMAL(18)"` + Status int `json:"status" xorm:"not null default(0) comment('-1未支付,0未审核,1未通过,2已通过,3已推送,4已作废') INT(11)"` + ReviewTime int64 `json:"review_time" xorm:"not null default(0) comment('审核时间') INT(11)"` + Version int64 `json:"version" xorm:"not null version comment('乐观锁') INT(11)"` + + // 额外字段 + RewardAmount string `json:"reward_amount" xorm:"-" description:"同上, 字符串"` + User *User `json:"user" xorm:"-" description:"用户信息"` } func (t *RewardHistory) TableName() string { return ReWardHistoryTableName } -func (t *RewardHistory) GetByUserOrderId(userOrderId int64) (bool, error) { - return core.GetXormAuto().Where("is_delete=0 and user_order_id=?", userOrderId).Get(t) +func (t *RewardHistory) GetByOutTradeNo(outTradeNo string) (bool, error) { + return core.GetXormAuto().Where("is_delete=0 and out_trade_no=?", outTradeNo).Get(t) } func (t *RewardHistory) UpdateStatus(id int64, status int) (int64, error) { diff --git a/models/shake_red_envelope_record.go b/models/shake_red_envelope_record.go index ad8777f..5636d1a 100644 --- a/models/shake_red_envelope_record.go +++ b/models/shake_red_envelope_record.go @@ -1,8 +1,9 @@ package models import ( - "github.com/ouxuanserver/osmanthuswine/src/core" "time" + + "github.com/ouxuanserver/osmanthuswine/src/core" ) const ShakeRedEnvelopeRecordTableName = TableNamePrefix + "shake_red_envelope_record" @@ -23,7 +24,7 @@ type ShakeRedEnvelopeRecord struct { AreaName string `json:"area_name" xorm:"not null default '' comment('地区名字') VARCHAR(18)"` Name string `json:"name" xorm:"not null default '' comment('红包名字') VARCHAR(255)"` UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户id') INT(11)"` - Amount float64 `json:"amount" xorm:"not null default '0' comment('金额') DECIMAL(18,2)"` + Amount float64 `json:"amount" xorm:"not null default '0' comment('金额') DECIMAL(18)"` IsDraw int `json:"is_draw" xorm:"not null default(0) comment('-1未被摇中,0已被摇中,1已被提现,2提现失败')"` TransferType int `json:"transfer_type" xorm:"not null default 0 comment('0微信红包 1 微信转账') TINYINT(1)"` PartnerTradeNo string `json:"partner_trade_no" xorm:"not null default '' comment('转账账单no') VARCHAR(128)"` @@ -83,3 +84,7 @@ func GetRedEnvelopesByUserId(userId int64) ([]*ShakeRedEnvelopeRecord, error) { } return records, err } + +func (t *ShakeRedEnvelopeRecord) GetLiveRedPackRecord(id interface{}) (bool, error) { + return core.GetXormAuto().Where("is_delete=0 and red_envelope_type = 1 and live_red_envelope_rule_id=?", id).Get(t) +} diff --git a/models/shake_red_envelope_rule.go b/models/shake_red_envelope_rule.go index 7b131ce..4dd63ef 100644 --- a/models/shake_red_envelope_rule.go +++ b/models/shake_red_envelope_rule.go @@ -10,9 +10,12 @@ const ShakeRedEnvelopeRuleTableName = TableNamePrefix + "shake_red_envelope_rule //摇红包活动规则 type ShakeRedEnvelopeRule struct { - Id int64 `json:"id" xorm:"pk autoincr"` + Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"` + IsDelete bool `json:"-" xorm:"not null default(0) comment('是否删除')"` + CreatedAt time.Time `json:"-" xorm:"not null created comment('创建时间')"` + UpdatedAt time.Time `json:"-" xorm:"not null default 'CURRENT_TIMESTAMP' updated comment('更新时间')"` + ShakeRedEnvelopeActivityId int64 `json:"shake_red_envelope_activity_id" xorm:"not null comment('主活动id')" description:"主活动的id"` - ShakeRedEnvelopeRuleLadders []*ShakeRedEnvelopeRuleLadder `json:"shake_red_envelope_rule_ladders" xorm:"-" description:"阶梯红包"` AreaId int64 `json:"area_id" xorm:"not null comment('地区id')" description:"地区id"` Model string `json:"model" description:"[普通红包,随机红包,阶梯红包]"` RedEnvelopeNum int64 `json:"red_envelope_num" xorm:"not null comment('红包个数')" description:"红包个数"` @@ -21,9 +24,7 @@ type ShakeRedEnvelopeRule struct { Probability float64 `json:"probability" description:"概率[0-100)" xorm:"not null comment('概率')"` Des string `json:"des" xorm:"not null default('根据对应得概率获得红包') comment('规则描述')"` ShakeRedEnvelopeStatus string `json:"shake_red_envelope_status" xorm:"not null default('未开始') comment('未开始|进行中|已结束')"` - IsDelete bool `json:"-" xorm:"default(0)" description:"是否删除"` - CreatedAt time.Time `json:"-" xorm:"created" description:"创建时间"` - UpdatedAt time.Time `json:"-" xorm:"updated" description:"更新时间"` + ShakeRedEnvelopeRuleLadders []*ShakeRedEnvelopeRuleLadder `json:"shake_red_envelope_rule_ladders" xorm:"-" description:"阶梯红包"` } func (t *ShakeRedEnvelopeRule) TableName() string { diff --git a/models/user_order.go b/models/user_order.go index 2afb5e2..5b102fa 100644 --- a/models/user_order.go +++ b/models/user_order.go @@ -66,11 +66,13 @@ func (t *UserOrder) UpdateStatusByOutTradeNo(outTradeNo interface{}, status int) } func (t *UserOrder) UpdateErrByOutTradeNo(outTradeNo interface{}) (int64, error) { - return core.GetXormAuto().Where("out_trade_no=? and is_delete=0", outTradeNo).Cols("err_msg").Update(t) + return core.GetXormAuto().Where("out_trade_no=? and is_delete=0", outTradeNo). + Cols("err_msg").Update(t) } func (t *UserOrder) UpdateRefundByOutTradeNo(outTradeNo interface{}) (int64, error) { - return core.GetXormAuto().Where("out_trade_no=?", outTradeNo).Cols("success_time", "refund_recv_account", "refund_account").Update(t) + return core.GetXormAuto().Where("out_trade_no=?", outTradeNo). + Cols("success_time", "status", "refund_recv_account", "refund_account").Update(t) } func GetUserOrdersByStatus(status ...int) ([]*UserOrder, error) { diff --git a/services/im/im.go b/services/im/im.go index 0b91fe8..f0f2ede 100644 --- a/services/im/im.go +++ b/services/im/im.go @@ -6,7 +6,7 @@ import ( "hudongzhuanjia/models" ) -func SendNoticeByActivityId(activityId int64, data map[string]interface{}, members ...string) error { +func SendNoticeByActivityId(activityId int64, _type im.NoticeType, data map[string]interface{}, members ...string) error { live := new(models.LiveConfig) exist, err := live.GetByActivityId(activityId) if err != nil { @@ -15,5 +15,5 @@ func SendNoticeByActivityId(activityId int64, data map[string]interface{}, membe if !exist { return errors.New("直播信息不存在") } - return im.SendGroupSystemNotification(live.ImGroupId, im.NoticeLiveRedPackStart, data, members...) + return im.SendGroupSystemNotification(live.ImGroupId, _type, data, members...) } diff --git a/services/pay/handle.go b/services/pay/handle.go new file mode 100644 index 0000000..31b4200 --- /dev/null +++ b/services/pay/handle.go @@ -0,0 +1,40 @@ +package pay_service + +import ( + "errors" + "hudongzhuanjia/libs/im" + "hudongzhuanjia/models" + im_service "hudongzhuanjia/services/im" + "time" +) + +//处理支付成功之后的回调问题 + +// 直播红包 +func Handle(order *models.UserOrder) error { + if order == nil { + return errors.New("订单信息不存在") + } + if order.GoodType == 3 { // 直播红包 + info := new(models.LiveRedEnvelopeRule) + exist, err := info.GetByOutTradeNo(order.OutTradeNo) + if err != nil || !exist { + return errors.New("直播红包信息异常") + } + + return im_service.SendNoticeByActivityId(order.ActivityId, im.NoticeLiveRedPackStart, + map[string]interface{}{ + "live_red_envelope_rule_id": info.Id, + "timestamp": time.Now().Unix(), + }) + } else if order.GoodType == 2 { // 打赏 + history := new(models.RewardHistory) + _, err := history.UpdateStatusByOutTradeNo(order.OutTradeNo, 0) + return err + } else if order.GoodType == 1 { // 霸屏 + history := new(models.BullyScreenHistory) + _, err := history.UpdateStatusByOutTradeNo(order.OutTradeNo, 0) + return err + } + return nil +} diff --git a/services/pay/order.go b/services/pay/order.go index dd20416..ba426ad 100644 --- a/services/pay/order.go +++ b/services/pay/order.go @@ -1,6 +1,8 @@ package pay_service import ( + "encoding/xml" + "fmt" core2 "github.com/chanxuehong/wechat/mch/core" "github.com/chanxuehong/wechat/mch/pay" "github.com/pkg/errors" @@ -8,6 +10,7 @@ import ( "hudongzhuanjia/logger" "hudongzhuanjia/models" "hudongzhuanjia/utils" + "io" "math" "strconv" "time" @@ -71,16 +74,11 @@ func loopUnifiedOrder() { if !ok { panic("通道异常关闭") } - //userOrder := new(models.UserOrder) - //exist, err := userOrder.GetByOutTradeNo(outTradeNo) - //if err != nil || !exist { - // logger.Error("通过订单查询UserOrder", zap.String("错误原因", err.Error()), - // zap.Bool("是否存在", exist), zap.String("交易订单号", outTradeNo)) - // continue - //} if param.Expires <= time.Now().Unix() { - Close(param.OutTradeNo) // 超时关闭订单 - continue // 超时 + //if param.Status == 0 { + // Close(param.OutTradeNo) // 超时关闭订单 + //} + continue // 超时 } // 首次进入不延迟 @@ -100,11 +98,13 @@ func loopUnifiedOrder() { continue } - if res.TradeState == CODE_TRADE_REFUND { + if res.Query.TradeState == CODE_TRADE_SUCCESS { + go Handle(res.Order) + } else if res.Query.TradeState == CODE_TRADE_REFUND { param.Status = 3 orderDelayQueue <- param continue - } else if res.TradeState == CODE_TRADE_NOTPAY || res.TradeState == CODE_TRADE_USERPAYING { + } else if res.Query.TradeState == CODE_TRADE_NOTPAY || res.Query.TradeState == CODE_TRADE_USERPAYING { orderDelayQueue <- param continue } @@ -124,7 +124,7 @@ func loopUnifiedOrder() { const CallbackOrderUrl = "https://api.ouxuanhudong.com/PcClient/common/WeChatOauthCtl/callbackOrder" -func UnifiedOrder(body, ip, openid string, fee, goodType, userId, activityId int64) (map[string]interface{}, error) { +func UnifiedOrder(body, openid string, fee, goodType, userId, activityId int64) (map[string]interface{}, error) { client, err := Client() if err != nil { return nil, err @@ -136,17 +136,16 @@ func UnifiedOrder(body, ip, openid string, fee, goodType, userId, activityId int outTradeNo := utils.RandomStr(32) nonceStr := utils.RandomStr(32) resp, err := pay.UnifiedOrder2(client, &pay.UnifiedOrderRequest{ - Body: body, - OutTradeNo: outTradeNo, - TotalFee: fee, - SpbillCreateIP: ip, - NotifyURL: CallbackOrderUrl, - TradeType: "JSAPI", - DeviceInfo: "WEB", - NonceStr: nonceStr, - SignType: core2.SignType_MD5, - TimeStart: now, - OpenId: openid, + Body: body, + OutTradeNo: outTradeNo, + TotalFee: fee, + NotifyURL: CallbackOrderUrl, + TradeType: "JSAPI", + DeviceInfo: "WEB", + NonceStr: nonceStr, + SignType: core2.SignType_MD5, + TimeStart: now, + OpenId: openid, }) if err != nil { return nil, err @@ -178,9 +177,9 @@ func UnifiedOrder(body, ip, openid string, fee, goodType, userId, activityId int timestamp := strconv.FormatInt(time.Now().Unix(), 10) //获取H5支付需要的paySign pac := "prepay_id=" + resp.PrepayId - paySign := core2.JsapiSign(client.AppId(), nonceStr, pac, core2.SignType_MD5, timestamp, ApiKey) + paySign := core2.JsapiSign(client.AppId(), timestamp, nonceStr, pac, core2.SignType_MD5, ApiKey) - go PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 5, 0) + go PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 0, 0) return map[string]interface{}{ "appid": Appid, "timestamp": timestamp, @@ -229,49 +228,36 @@ type NotifyRequest struct { TimeEnd string `xml:"time_end,omitempty" json:"time_end,omitempty"` } -//func NotifyOrder(req *http.Request) error { -// res := new(NotifyRequest) -// if err := xml.NewDecoder(req.Body).Decode(res); err != nil { -// return fmt.Errorf("xml.NewDecoder.Decode:%w", err) -// } - -//if res.ReturnCode != CODE_SUCCESS { -// return fmt.Errorf("network error, retrun_code: %v and return_msg: %v", res.ReturnCode, res.ReturnMsg) -//} -// -//if res.ResultCode != CODE_FAIL && res.ErrCode == CODE_SYSTEMERROR { -// return fmt.Errorf("trade error, err_code: %v and err_code_des: %v", res.ErrCode, res.ErrCodeDes) -//} -// -//if res.ResultCode == CODE_SUCCESS { -// userOrder := new(models.UserOrder) -// userOrder.TimeEnd = res.TimeEnd -// userOrder.TransactionId = res.TransactionId -// if res.TradeType == CODE_TRADE_SUCCESS { -// userOrder.Status = 1 -// } else if _, err := userOrder.UpdateStatusByOutTradeNo(res.OutTradeNo, userOrder.TradeType); err != nil { -// return err -// } -// -//} else { -//} - -//// 设置一下 -//if userOrder.GoodType == 1 { // 霸屏 -// _, err = new(models.BullyScreenHistory).UpdateStatusByUserOrderId(userOrder.Id, 0) -// if err != nil { -// return err -// } -//} else if userOrder.GoodType == 2 { -// _, err = new(models.RewardHistory).UpdateStatusByUserOrderId(userOrder.Id, 0) -// if err != nil { -// return err -// } -//} -// return nil -//} - -func OrderQuery(outTradeNo string) (*pay.OrderQueryResponse, error) { +func NotifyOrder(w io.Reader) (*models.UserOrder, error) { + res := new(NotifyRequest) + if err := xml.NewDecoder(w).Decode(res); err != nil { + return nil, fmt.Errorf("xml.NewDecoder.Decode:%w", err) + } + + if res.ReturnCode != CODE_SUCCESS { + return nil, fmt.Errorf("network error, retrun_code: %v and return_msg: %v", res.ReturnCode, res.ReturnMsg) + } + + if res.ResultCode == CODE_SUCCESS && res.TradeType == CODE_TRADE_SUCCESS { + userOrder := new(models.UserOrder) + userOrder.TimeEnd = res.TimeEnd + userOrder.TransactionId = res.TransactionId + userOrder.Status = 1 + if _, err := userOrder.UpdateStatusByOutTradeNo(res.OutTradeNo, 1); err != nil { + return nil, err + } + return userOrder, nil + } else { + return nil, fmt.Errorf("trade error, err_code: %v and err_code_des: %v", res.ErrCode, res.ErrCodeDes) + } +} + +type OrderQueryResult struct { + Order *models.UserOrder + Query *pay.OrderQueryResponse +} + +func OrderQuery(outTradeNo string) (*OrderQueryResult, error) { client, err := Client() if err != nil { return nil, err @@ -318,7 +304,10 @@ func OrderQuery(outTradeNo string) (*pay.OrderQueryResponse, error) { if _, err = userOrder.UpdateStatusByOutTradeNo(outTradeNo, userOrder.Status); err != nil { return nil, err } - return res, nil + return &OrderQueryResult{ + Order: userOrder, + Query: res, + }, nil } func Close(outTradeNo string) error { @@ -376,19 +365,24 @@ func Refund(reason, outTradeNo string) (*pay.RefundResponse, error) { return nil, err } - PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 5, 0) // 退款查询 + PutOrderDelayQueue(userOrder.Body, userOrder.OutTradeNo, userOrder.OpenId, int(userOrder.TotalFee), userOrder.Status, 0, 0) // 退款查询 return res, nil } -func QueryRefund(outTradeNo string) (*pay.RefundQueryResponse, error) { +type QueryRefundResult struct { + Order *models.UserOrder + Query *pay.RefundQueryResponse +} + +func QueryRefund(outTradeNo string) (*QueryRefundResult, error) { userOrder := new(models.UserOrder) - //exist, err := userOrder.GetByOutTradeNo(outTradeNo) - //if err != nil { - // return nil, err - //} - //if !exist { - // return nil, errors.New("不存在改笔退款") - //} + exist, err := userOrder.GetByOutTradeNo(outTradeNo) + if err != nil { + return nil, err + } + if !exist { + return nil, errors.New("不存在改笔退款") + } client, err := Client() res, err := pay.RefundQuery2(client, &pay.RefundQueryRequest{ @@ -412,5 +406,8 @@ func QueryRefund(outTradeNo string) (*pay.RefundQueryResponse, error) { return nil, err } - return res, nil + return &QueryRefundResult{ + Order: userOrder, + Query: res, + }, nil } diff --git a/services/pay/pay_test.go b/services/pay/pay_test.go deleted file mode 100644 index a3cc5a7..0000000 --- a/services/pay/pay_test.go +++ /dev/null @@ -1,12 +0,0 @@ -package pay_service - -import ( - "fmt" - "testing" -) - -func TestTransfer(t *testing.T) { - res, err := Transfer("欧轩互动-转账测试", "127.0.0.1", "o9XM41s_NN8Y0QK6_MbM-aYMV3TE", 1) - fmt.Println(err) - fmt.Printf("%+v\n", res) -}