fix version 1
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 申请加群
|
||||
@@ -19,39 +20,84 @@ func DoJoin(ctx context.Context, in *pb.DoJoinRequest) (reply *pb.DataStatusRepl
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//生成唯一标识
|
||||
identity := utils.UUID()
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
//判断是否需要加群验证
|
||||
if models.GetGroupEnableAnyJoin(auth.ID, in.Identity) {
|
||||
// 直接加入群成员
|
||||
apply := &models.GroupMember{
|
||||
GroupID: uint(in.Id),
|
||||
// 校验群组是否存在
|
||||
var group models.GroupBasic
|
||||
if err = impl.DBService.Where("identity=?", in.Identity).First(&group).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
|
||||
// 重复入群防护:已是成员直接返回成功
|
||||
var memberCnt int64
|
||||
if err = impl.DBService.Model(&models.GroupMember{}).
|
||||
Where("group_identity=? and passport_id=?", in.Identity, auth.ID).Count(&memberCnt).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if memberCnt > 0 {
|
||||
return &pb.DataStatusReply{Data: vars.OK, Timeseq: time.Now().UnixMilli()}, nil
|
||||
}
|
||||
|
||||
// 成员上限校验(MemberLimit 为 0 表示不限)
|
||||
if group.MemberLimit > 0 && group.MemberTotal >= group.MemberLimit {
|
||||
return nil, errcode.ErrResourceExhausted
|
||||
}
|
||||
|
||||
// 是否免验证入群
|
||||
enableAnyJoin, err := models.GetGroupEnableAnyJoin(in.Identity)
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
var identity string
|
||||
if enableAnyJoin {
|
||||
// 免验证直接加入群成员
|
||||
member := &models.GroupMember{
|
||||
GroupID: group.ID,
|
||||
GroupIdentity: in.Identity,
|
||||
Role: ROLE_MEMBER,
|
||||
}
|
||||
apply.Identity = utils.UUID()
|
||||
apply.PassportID = auth.ID
|
||||
apply.PassportIdentity = auth.Identity
|
||||
|
||||
err = impl.DBService.Create(&apply).Error
|
||||
member.Identity = utils.UUID()
|
||||
member.PassportID = auth.ID
|
||||
member.PassportIdentity = auth.Identity
|
||||
if err = impl.DBService.Create(member).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
identity = member.Identity
|
||||
// 更新群组表数据统计
|
||||
models.UpsetGroupMemberTotal(in.Identity, "+")
|
||||
} else {
|
||||
// 避免重复提交待处理申请
|
||||
var applyCnt int64
|
||||
if err = impl.DBService.Model(&models.GroupApply{}).
|
||||
Where("group_identity=? and from_id=? and status=?", in.Identity, auth.ID, 0).Count(&applyCnt).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if applyCnt > 0 {
|
||||
return &pb.DataStatusReply{Data: vars.OK, Timeseq: time.Now().UnixMilli()}, nil
|
||||
}
|
||||
|
||||
// 写入加群申请表
|
||||
apply := &models.GroupApply{
|
||||
FromID: uint(auth.ID),
|
||||
FromID: auth.ID,
|
||||
FromIdentity: auth.Identity,
|
||||
GroupID: uint(in.Id),
|
||||
GroupID: group.ID,
|
||||
GroupIdentity: in.Identity,
|
||||
Message: in.Message,
|
||||
}
|
||||
apply.Identity = utils.UUID()
|
||||
|
||||
err = impl.DBService.Create(&apply).Error
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
if err = impl.DBService.Create(apply).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
identity = apply.Identity
|
||||
}
|
||||
|
||||
return &pb.DataStatusReply{
|
||||
|
||||
@@ -19,22 +19,40 @@ func DoKick(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.DataStatusRep
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 判断是不是群管理或是创建者
|
||||
var tc int64
|
||||
if err = impl.DBService.Model(&models.GroupMember{}).Where("identity=? and role !=0 and passport_id=?", in.GroupIdentity, auth.Identity).Count(&tc).Error; err != nil {
|
||||
if in.GetGroupIdentity() == "" || in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 判断操作者是不是该群的管理员或创建者
|
||||
var opRole int32
|
||||
if err = impl.DBService.Model(&models.GroupMember{}).
|
||||
Where("group_identity=? and passport_id=?", in.GroupIdentity, auth.ID).
|
||||
Pluck("role", &opRole).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if tc == 0 {
|
||||
if opRole <= ROLE_MEMBER {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
err = impl.DBService.Model(&models.GroupMember{}).Delete("group_identity=? and identity=?", in.GroupIdentity, in.Identity).Error
|
||||
if err != nil {
|
||||
// 目标成员必须属于该群组
|
||||
var target models.GroupMember
|
||||
if err = impl.DBService.Where("group_identity=? and identity=?", in.GroupIdentity, in.Identity).First(&target).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
// 禁止踢创建者,以及权限不低于操作者的成员
|
||||
if target.Role >= ROLE_CREATEOR || target.Role >= opRole {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
res := impl.DBService.Where("group_identity=? and identity=?", in.GroupIdentity, in.Identity).Delete(&models.GroupMember{})
|
||||
if res.Error != nil {
|
||||
printer.Error(res.Error.Error())
|
||||
return nil, errcode.ErrDB
|
||||
} else {
|
||||
//更新群组表数据统计
|
||||
}
|
||||
if res.RowsAffected > 0 {
|
||||
// 仅在确实删除成员后更新群组数据统计
|
||||
models.UpsetGroupMemberTotal(in.GroupIdentity, "-")
|
||||
}
|
||||
return &pb.DataStatusReply{
|
||||
|
||||
@@ -20,13 +20,13 @@ func DoQuit(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.DataStatusRep
|
||||
return nil, err
|
||||
}
|
||||
// 判断是不是创建者
|
||||
var creator_id uint
|
||||
if err = impl.DBService.Model(&models.GroupBasic{}).Select("creator_id").Where("identity=?", in.GroupIdentity).Scan(&creator_id).Error; err != nil {
|
||||
var creatorID uint
|
||||
if err = impl.DBService.Model(&models.GroupBasic{}).Select("passport_id").Where("identity=?", in.GroupIdentity).Scan(&creatorID).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if auth.ID == creator_id {
|
||||
if auth.ID == creatorID {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
|
||||
@@ -21,13 +21,13 @@ func DoSetManager(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.DataSta
|
||||
return nil, err
|
||||
}
|
||||
// 判断是不是创建者
|
||||
var creator_id uint
|
||||
if err = impl.DBService.Model(&models.GroupBasic{}).Select("creator_id").Where("identity=?", in.GroupIdentity).Scan(&creator_id).Error; err != nil {
|
||||
var creatorID uint
|
||||
if err = impl.DBService.Model(&models.GroupBasic{}).Select("passport_id").Where("identity=?", in.GroupIdentity).Scan(&creatorID).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if auth.ID != creator_id {
|
||||
if auth.ID != creatorID {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ func Fetch(ctx context.Context, in *pb.IdentRequest) (reply *pb.GroupMemberReply
|
||||
}
|
||||
|
||||
return &pb.GroupMemberReply{
|
||||
Total: int32(len(members)),
|
||||
Total: int32(len(members)),
|
||||
Members: members,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -12,53 +12,86 @@ import (
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 申请加群处理
|
||||
func JoinDoHandle(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.DataStatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var identity string
|
||||
switch strings.ToUpper(in.Direction) {
|
||||
case "PASS":
|
||||
var apply models.GroupApply
|
||||
if err = impl.DBService.Where("identity=?", in.Identity).First(&apply).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 直接加入群成员
|
||||
identity = utils.UUID()
|
||||
member := &models.GroupMember{
|
||||
GroupID: apply.GroupID,
|
||||
GroupIdentity: apply.GroupIdentity,
|
||||
Role: 0,
|
||||
}
|
||||
member.Identity = utils.UUID()
|
||||
member.PassportID = apply.FromID
|
||||
member.PassportIdentity = apply.FromIdentity
|
||||
|
||||
if err = impl.DBService.Create(&member).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
//更新申请表处理结果
|
||||
impl.DBService.Model(&models.GroupApply{}).Where("identity=?", in.Identity).UpdateColumn("status", 1)
|
||||
|
||||
//更新群组表数据统计
|
||||
models.UpsetGroupMemberTotal(apply.GroupIdentity, "+")
|
||||
case "REJECT":
|
||||
//更新申请表处理结果
|
||||
impl.DBService.Model(&models.GroupApply{}).Where("identity=?", in.Identity).UpdateColumn("status", -1)
|
||||
// 未识别的操作方向直接报错,不再默认返回成功
|
||||
direction := strings.ToUpper(in.Direction)
|
||||
if direction != "PASS" && direction != "REJECT" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
// 查询申请记录
|
||||
var apply models.GroupApply
|
||||
if err = impl.DBService.Where("identity=?", in.Identity).First(&apply).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
|
||||
// 幂等:申请已处理则直接返回成功
|
||||
if apply.Status != 0 {
|
||||
return &pb.DataStatusReply{Data: vars.OK, Timeseq: time.Now().UnixMilli()}, nil
|
||||
}
|
||||
|
||||
// 角色校验:仅该群管理员/创建者可处理申请
|
||||
var opRole int32
|
||||
if err = impl.DBService.Model(&models.GroupMember{}).
|
||||
Where("group_identity=? and passport_id=?", apply.GroupIdentity, auth.ID).
|
||||
Pluck("role", &opRole).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if opRole <= ROLE_MEMBER {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
var identity string
|
||||
switch direction {
|
||||
case "PASS":
|
||||
// 幂等:申请人已是成员则不再重复创建
|
||||
var memberCnt int64
|
||||
if err = impl.DBService.Model(&models.GroupMember{}).
|
||||
Where("group_identity=? and passport_id=?", apply.GroupIdentity, apply.FromID).Count(&memberCnt).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if memberCnt == 0 {
|
||||
member := &models.GroupMember{
|
||||
GroupID: apply.GroupID,
|
||||
GroupIdentity: apply.GroupIdentity,
|
||||
Role: ROLE_MEMBER,
|
||||
}
|
||||
member.Identity = utils.UUID()
|
||||
member.PassportID = apply.FromID
|
||||
member.PassportIdentity = apply.FromIdentity
|
||||
if err = impl.DBService.Create(member).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
identity = member.Identity
|
||||
// 更新群组表数据统计
|
||||
models.UpsetGroupMemberTotal(apply.GroupIdentity, "+")
|
||||
}
|
||||
|
||||
// 更新申请表处理结果
|
||||
if err = impl.DBService.Model(&models.GroupApply{}).Where("identity=?", in.Identity).UpdateColumn("status", 1).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
case "REJECT":
|
||||
// 更新申请表处理结果
|
||||
if err = impl.DBService.Model(&models.GroupApply{}).Where("identity=?", in.Identity).UpdateColumn("status", -1).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.DataStatusReply{
|
||||
Data: identity,
|
||||
|
||||
@@ -2,6 +2,7 @@ package member
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/social/group/internal/impl"
|
||||
"bsm/full/module/social/group/internal/models"
|
||||
@@ -18,27 +19,37 @@ func JoinFetch(ctx context.Context, in *pb.Empty) (reply *pb.JoinFetchReply, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fetch := make([]*pb.ApplyJoinGroupItem, 0)
|
||||
applys := make([]*pb.ApplyJoinGroupItem, 0)
|
||||
group_ids := models.GetMasterGroup(auth.ID)
|
||||
if len(group_ids) > 0 {
|
||||
err = impl.DBService.Model(&models.GroupApply{}).Where("group_id in ", group_ids).Find(&fetch).Error
|
||||
if err != nil {
|
||||
var records []models.GroupApply
|
||||
if err = impl.DBService.Model(&models.GroupApply{}).Where("group_id in ?", group_ids).Find(&records).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
var card = &pb.PassportInfoDetailCard{}
|
||||
for idx, item := range fetch {
|
||||
err = impl.DBService.Table("passport_extend").Select(filed).Where("passport_identity = ?", item.From.Identity).First(card).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
for i := range records {
|
||||
item := &pb.ApplyJoinGroupItem{
|
||||
Identity: records[i].Identity,
|
||||
GroupId: int64(records[i].GroupID),
|
||||
Message: records[i].Message,
|
||||
CreatedAt: records[i].CreatedAt.Format(time.DateTime),
|
||||
Status: int32(records[i].Status),
|
||||
}
|
||||
fetch[idx].From = card
|
||||
// 每条申请单独分配资料卡,避免所有元素共用同一指针
|
||||
card := &pb.PassportInfoDetailCard{}
|
||||
if e := impl.DBService.Table("passport_extend").Select(filed).
|
||||
Where("passport_identity = ?", records[i].FromIdentity).First(card).Error; e != nil {
|
||||
printer.Error(e.Error())
|
||||
// 申请人资料查不到时跳过该条,避免整份列表不可用
|
||||
continue
|
||||
}
|
||||
item.From = card
|
||||
applys = append(applys, item)
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.JoinFetchReply{
|
||||
Total: int32(len(fetch)),
|
||||
Applys: fetch,
|
||||
Total: int32(len(applys)),
|
||||
Applys: applys,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user