|
|
@ -1,16 +1,125 @@ |
|
|
|
package client |
|
|
|
|
|
|
|
import ( |
|
|
|
"go.uber.org/zap" |
|
|
|
"hudongzhuanjia/controllers" |
|
|
|
"hudongzhuanjia/libs/filter" |
|
|
|
"hudongzhuanjia/logger" |
|
|
|
"hudongzhuanjia/models" |
|
|
|
pay_service "hudongzhuanjia/services/pay" |
|
|
|
red_envelope_service "hudongzhuanjia/services/red_envelope" |
|
|
|
"hudongzhuanjia/utils" |
|
|
|
"hudongzhuanjia/utils/code" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
var MaxQueueSize = 10000 |
|
|
|
var RequestLimit = 60 * time.Second |
|
|
|
var GetRedPackQueue = make(chan int64, MaxQueueSize) |
|
|
|
|
|
|
|
func loopGetRedPack() { |
|
|
|
redPacks, err := models.GetRedPacksByStatus(1) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
|
|
|
|
// 保证容量足够
|
|
|
|
GetRedPackQueue = make(chan int64, MaxQueueSize+len(redPacks)) |
|
|
|
|
|
|
|
// 初始化
|
|
|
|
for _, redPack := range redPacks { |
|
|
|
GetRedPackQueue <- redPack.Id |
|
|
|
} |
|
|
|
|
|
|
|
defer func() { |
|
|
|
if errRec := recover(); errRec != nil { |
|
|
|
logger.Error("用户转账轮询发生错误", |
|
|
|
zap.String("函数", "loopGetRedPack"), zap.Any("错误原因", errRec)) |
|
|
|
} |
|
|
|
time.Sleep(5 * time.Second) |
|
|
|
loopGetRedPack() |
|
|
|
}() |
|
|
|
for { |
|
|
|
select { |
|
|
|
case redPackId, ok := <-GetRedPackQueue: |
|
|
|
if !ok { |
|
|
|
panic("GetRedPackQueue通道关闭") |
|
|
|
} |
|
|
|
time.Sleep(RequestLimit) // 请求频率
|
|
|
|
|
|
|
|
redPack := new(models.LiveRedPack) |
|
|
|
exist, err := models.GetById(redPack, redPackId) |
|
|
|
if err != nil { |
|
|
|
GetRedPackQueue <- redPackId |
|
|
|
logger.Error("获取LiveRedPack表数据出现错误", |
|
|
|
zap.Any("错误原因", err), zap.Int64("LiveRedPack表的id", redPack.Id)) |
|
|
|
continue |
|
|
|
} |
|
|
|
if !exist { |
|
|
|
logger.Error("不存在LiveRedPack表数据", zap.Int64("LiveRedPack表的id", redPack.Id)) |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
// todo: 增加一个查询机制 --查询此次付款是否成功
|
|
|
|
// todo: 是否需要发放系统通知
|
|
|
|
resp, err := pay_service.TransferInfo(redPack.TransferNo) |
|
|
|
if err != nil { |
|
|
|
GetRedPackQueue <- redPackId |
|
|
|
logger.Error("微信企业转账查询API", |
|
|
|
zap.Any("错误原因", err), zap.Int64("LiveRedPack表的id", redPack.Id)) |
|
|
|
continue |
|
|
|
} |
|
|
|
if resp.Status == pay_service.CODE_FAIL { // 转账失败
|
|
|
|
_, err = pay_service.Transfer("欧轩互动-红包活动", redPack.OpenId, redPack.TransferNo, redPack.Amount) |
|
|
|
if err != nil { |
|
|
|
GetRedPackQueue <- redPackId |
|
|
|
logger.Error("微信企业转账API", |
|
|
|
zap.Any("错误原因", err), zap.Int64("LiveRedPack表的id", redPack.Id)) |
|
|
|
continue |
|
|
|
} |
|
|
|
_, err = redPack.UpdateStatusById(redPack.Id, 2) |
|
|
|
if err != nil { |
|
|
|
GetRedPackQueue <- redPackId |
|
|
|
logger.Error("微信企业转账,LiveRedPack表更新错误", |
|
|
|
zap.Any("错误原因", err), zap.Int64("LiveRedPack表id", redPack.Id)) |
|
|
|
continue |
|
|
|
} |
|
|
|
} else if resp.Status == pay_service.CODE_SUCCESS { |
|
|
|
_, err = redPack.UpdateStatusById(redPack.Id, 2) |
|
|
|
if err != nil { |
|
|
|
logger.Error("微信企业查询,转账成功", |
|
|
|
zap.Any("错误原因", err), zap.Int64("LiveRedPack表id", redPack.Id)) |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var SendRedPackQueue = make(chan int64, MaxQueueSize) |
|
|
|
|
|
|
|
func loopSendRedPack() { |
|
|
|
redPackInfo := |
|
|
|
defer func() { |
|
|
|
if err := recover(); err != nil { |
|
|
|
logger.Error("用户发送红包轮询出现错误", |
|
|
|
zap.String("函数", "loopSendRedPack"), zap.Any("错误", err)) |
|
|
|
} |
|
|
|
time.Sleep(5 * time.Second) |
|
|
|
loopSendRedPack() |
|
|
|
}() |
|
|
|
for { |
|
|
|
select { |
|
|
|
case redPackInfoId, ok := <-SendRedPackQueue: |
|
|
|
if !ok { |
|
|
|
panic("SendRedPackQueue通道异常关闭") |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
type LiveCtl struct { |
|
|
|
controllers.AuthorCtl |
|
|
|
//controllers.BaseCtl
|
|
|
@ -76,7 +185,7 @@ func (t *LiveCtl) SendLiveRedPack() { |
|
|
|
userId := t.MustGetUID() // 用户 uid
|
|
|
|
activityId := t.MustGetInt64("activity_id") // activity_id
|
|
|
|
num := t.MustGetInt("num") // 红包数量
|
|
|
|
amount := t.MustGetDouble("amount") // 金额
|
|
|
|
amount := t.MustGetInt64("amount") // 金额
|
|
|
|
prompt := t.MustGet("prompt") // 提示
|
|
|
|
|
|
|
|
user := models.User{} |
|
|
@ -85,11 +194,10 @@ func (t *LiveCtl) SendLiveRedPack() { |
|
|
|
t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") |
|
|
|
|
|
|
|
ip := strings.Split(t.Request.OriginRequest.RemoteAddr, ":") |
|
|
|
res, err := pay_service.Order("欧轩互动-直播红包", ip[0], user.Openid, int(amount*100), 3, userId, activityId) |
|
|
|
res, err := pay_service.UnifiedOrder("欧轩互动-直播红包", ip[0], user.Openid, amount) |
|
|
|
t.CheckErr(err) |
|
|
|
|
|
|
|
info := models.LiveRedPackInfo{} |
|
|
|
info.UserOrderId = res["user_order_id"].(int64) |
|
|
|
info.Amount = amount |
|
|
|
info.UserId = userId |
|
|
|
info.ActivityId = activityId |
|
|
@ -136,18 +244,17 @@ func (t *LiveCtl) GetRedPack() { |
|
|
|
} |
|
|
|
|
|
|
|
// 乐观锁 ==> 防止并发
|
|
|
|
row, err := redPack.Receive(userId) |
|
|
|
redPack.OpenId = user.Openid |
|
|
|
redPack.Receiver = user.Id |
|
|
|
redPack.TransferType = 1 |
|
|
|
redPack.TransferNo = utils.RandomStr(32) |
|
|
|
row, err := redPack.UpdateStatusById(redPack.Id, 1) |
|
|
|
t.CheckErr(err) |
|
|
|
if row != 1 { |
|
|
|
t.ERROR("红包被领完了", code.MSG_LIVE_RED_PACK_NOT_EXIST) |
|
|
|
return |
|
|
|
} |
|
|
|
ip := strings.Split(t.Request.OriginRequest.RemoteAddr, ":") |
|
|
|
res, err := pay_service.Transfer("欧轩互动-红包活动", ip[0], user.Openid, redPack.Amount) |
|
|
|
redPack.TransferType = 1 |
|
|
|
redPack.TransferNo = res.PartnerTradeNo |
|
|
|
_, err = redPack.UpdateStatusById(redPack, 2) |
|
|
|
t.CheckErr(err) |
|
|
|
GetRedPackQueue <- redPack.Id // 进入队列
|
|
|
|
|
|
|
|
t.JSON(redPack) |
|
|
|
} |