Browse Source

fix: query user info when create ImSendRedPackageOrd

herolist
shenjiachi 3 weeks ago
parent
commit
62d53fc878
3 changed files with 66 additions and 49 deletions
  1. +2
    -20
      app/hdl/hdl_im.go
  2. +62
    -28
      app/svc/svc_im.go
  3. +2
    -1
      go.mod

+ 2
- 20
app/hdl/hdl_im.go View File

@@ -76,16 +76,7 @@ func GrabRedPackage(c *gin.Context) {
return
}

val, exists := c.Get("user")
if !exists {
e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
return
}
user, ok := val.(*model.User)
if !ok {
e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
return
}
user := svc.GetUser(c)

redPackageContent, grabAmount, err := svc.DelGrabRedPackage(req, user)
if err != nil {
@@ -190,16 +181,7 @@ func SendRedPackage(c *gin.Context) {
return
}

val, exists := c.Get("user")
if !exists {
e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
return
}
user, ok := val.(*model.User)
if !ok {
e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
return
}
user := svc.GetUser(c)

redPackageContent, redPackageId, err := svc.BalancePayForRedPackage(user, args.Amount, args)
if err != nil {


+ 62
- 28
app/svc/svc_im.go View File

@@ -38,6 +38,7 @@ func DelGrabRedPackage(req *md.GrabRedPackageReq, user *model.User) (resp []byte
if imSendRedPackageOrd == nil {
return nil, grabAmount, errors.New("未查询到对应的红包记录")
}
// 剩余个数和已过期无法领取
if imSendRedPackageOrd.RedPacketBalanceNums == 0 || imSendRedPackageOrd.State == int(pb.RedPacketStatusType_RPS_EXPIRE) {
return nil, "-1", nil
}
@@ -141,7 +142,8 @@ func DelGrabRedPackage(req *md.GrabRedPackageReq, user *model.User) (resp []byte
imSendRedPackageOrd.RedPacketBalanceAmount = redPacketBalanceAmount
imSendRedPackageOrd.ReceivedUserIds = redPacketReceivedUserIds
imSendRedPackageOrd.ReceivedImUserIds = redPacketReceivedImUserIds
_, err = redPackageOrdDb.ImSendRedPackageOrdUpdateBySession(session, imSendRedPackageOrd.Id, imSendRedPackageOrd, "red_packet_balance_nums", "state", "received_user_ids", "received_im_user_ids", "received_times",
_, err = redPackageOrdDb.ImSendRedPackageOrdUpdateBySession(session, imSendRedPackageOrd.Id, imSendRedPackageOrd,
"red_packet_balance_nums", "state", "received_user_ids", "received_im_user_ids", "received_times",
"received_user_amount", "create_time", "update_time", "red_packet_balance_amount")
if err != nil {
session.Rollback()
@@ -161,6 +163,7 @@ func DelGrabRedPackage(req *md.GrabRedPackageReq, user *model.User) (resp []byte
session.Rollback()
return nil, grabAmount, err
}
session.Commit()

//4、组装消息参数
msg := &pb.RED_PACKAGE{
@@ -260,18 +263,45 @@ func BalancePayForRedPackage(user *model.User, money string, req md.SendRedPacka

// 新增红包记录
var imUid int
if req.UserId != "" {
imUid = utils.StrToInt(req.UserId)
var imUser model2.User
has, err := db.DbIm.Where("phone_number = ?", user.Phone).Get(&imUser)
if err != nil {
session.Rollback()
return nil, 0, err
}
if has {
imUid = int(imUser.Id)
} else {
//TODO::请求rpc接口,同步用户数据
resp, err := utils.GetBusinessExtClient(cfg.ImBusinessRpc.URL, cfg.ImBusinessRpc.PORT).GetUser(utils.GetCtx("", "", ""), &pb.GetUserReq{
UserId: 0,
Phone: user.Phone,
})
if err != nil {
return nil, redPackageId, err
if req.UserId != "" {
imUid = utils.StrToInt(req.UserId)
} else {
//TODO::请求rpc接口,同步用户数据
resp, err := utils.GetBusinessExtClient(cfg.ImBusinessRpc.URL, cfg.ImBusinessRpc.PORT).GetUser(utils.GetCtx("", "", ""), &pb.GetUserReq{
UserId: 0,
Phone: user.Phone,
})
if err != nil {
return nil, redPackageId, err
}
imUid = int(resp.User.UserId)
now := time.Now()
tempImUser := model2.User{
Id: int64(imUid),
PhoneNumber: user.Phone,
Nickname: user.Nickname,
Sex: 0,
AvatarUrl: user.Avatar,
Extra: "",
CreateTime: now,
UpdateTime: now,
IsAutoAddedFriends: 0,
}
_, err = db.DbIm.Insert(&tempImUser)
if err != nil {
session.Rollback()
return nil, 0, err
}
}
imUid = int(resp.User.UserId)
}

now := time.Now()
@@ -279,33 +309,37 @@ func BalancePayForRedPackage(user *model.User, money string, req md.SendRedPacka
var waitDrawUserIds string
var waitDrawImUserIds string
if req.RedPacketType == int(pb.RedPacketType_RPT_GROUP_SPECIALLY) {
var toUserIds []string
// 构造 im 用户 id 列表
var toImUserIds []string
for _, v := range req.ToUserIds {
toUserIds = append(toUserIds, utils.AnyToString(v))
toImUserIds = append(toImUserIds, utils.AnyToString(v))
}
waitDrawImUserIds = strings.Join(toImUserIds, ",")
// 查询 im 用户信息
var imUsers []model2.User
err = db.DbIm.In("id", req.ToUserIds).Find(&imUsers)
if err != nil {
return nil, 0, err
}
// 获取手机号列表
var phones []string
for _, v := range imUsers {
phones = append(phones, v.PhoneNumber)
}
waitDrawUserIds = strings.Join(toUserIds, ",")
// 查询用户信息
userDb := implement.NewUserDb(db.Db)
waitDrawUsers, err1 := userDb.UserFindByParams(map[string]interface{}{
"key": "id",
"value": toUserIds,
"key": "phone",
"value": phones,
})
if err1 != nil {
return nil, 0, err1
}
var waitDrawUserPhones []string
var toUserIDs []string
for _, v := range waitDrawUsers {
waitDrawUserPhones = append(waitDrawUserPhones, v.Phone)
}
var waitDrawImUsers []model2.User
err = db.DbIm.Table("user").In("phone_number", waitDrawUserPhones).Find(&waitDrawUsers)
if err != nil {
return nil, 0, err
}
var toUserIMIDs []string
for _, v := range waitDrawImUsers {
toUserIMIDs = append(toUserIMIDs, utils.AnyToString(v.Id))
toUserIDs = append(toUserIDs, utils.AnyToString(v.Id))
}
waitDrawImUserIds = strings.Join(toUserIMIDs, ",")
waitDrawUserIds = strings.Join(toUserIDs, ",")
}

ordDb := implement.NewImSendRedPackageOrdDb(db.Db)


+ 2
- 1
go.mod View File

@@ -16,7 +16,7 @@ require (
github.com/go-playground/universal-translator v0.18.1
github.com/go-playground/validator/v10 v10.20.0
github.com/go-redis/redis v6.15.9+incompatible
github.com/gomodule/redigo v1.9.2
github.com/gomodule/redigo v2.0.0+incompatible
github.com/jinzhu/copier v0.4.0
github.com/makiuchi-d/gozxing v0.0.0-20210324052758-57132e828831
github.com/qiniu/api.v7/v7 v7.8.2
@@ -38,6 +38,7 @@ require (
code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be
code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5
github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible
github.com/gin-contrib/sessions v1.0.1
github.com/go-pay/crypto v0.0.1
github.com/go-pay/gopay v1.5.101
github.com/go-pay/xlog v0.0.2


Loading…
Cancel
Save