refactor: reorganize modules and add Linux build tooling
This commit is contained in:
62
module/social/group/internal/config/config.go
Normal file
62
module/social/group/internal/config/config.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/conf"
|
||||
"git.apinb.com/bsm-sdk/core/crypto/encipher"
|
||||
"git.apinb.com/bsm-sdk/core/env"
|
||||
)
|
||||
|
||||
var (
|
||||
Spec SrvConfig
|
||||
)
|
||||
|
||||
type SrvConfig struct {
|
||||
conf.Base `yaml:",inline"`
|
||||
Databases *conf.DBConf `yaml:"Databases"`
|
||||
MicroService *conf.MicroServiceConf `yaml:"MicroService"`
|
||||
Rpc map[string]conf.RpcConf `yaml:"Rpc"`
|
||||
Gateway *conf.GatewayConf `yaml:"Gateway"`
|
||||
Apm *conf.ApmConf `yaml:"APM"`
|
||||
Etcd *conf.EtcdConf `yaml:"Etcd"`
|
||||
WeChat *WeChatConf `yaml:"WeChatConf"`
|
||||
Token *TokenConf `yaml:"Token"`
|
||||
Kyc *KycConf `yaml:"Kyc"`
|
||||
}
|
||||
|
||||
type KycConf struct {
|
||||
Provider string `yaml:"Provider"`
|
||||
BaseUrl string `yaml:"BaseUrl"`
|
||||
ApiSecret string `yaml:"ApiSecret"`
|
||||
ApiToken string `yaml:"ApiToken"`
|
||||
ApiArgs string `yaml:"ApiArgs"`
|
||||
}
|
||||
|
||||
type TokenConf struct {
|
||||
Prefix string `yaml:"Prefix"`
|
||||
Expire int `yaml:"Expire"`
|
||||
}
|
||||
|
||||
type WeChatConf struct {
|
||||
AppID string `json:"app_id"`
|
||||
AppSecret string `json:"app_secret"`
|
||||
}
|
||||
|
||||
func New(srvKey string) {
|
||||
// 初始化配置 创建一个新的配置实例,用于服务配置
|
||||
conf.New(srvKey, &Spec)
|
||||
|
||||
// 配置校验 服务IP,端口; 端口如果不合规,则随机分配端口
|
||||
Spec.Port = conf.CheckPort(Spec.Port)
|
||||
Spec.BindIP = conf.CheckIP(Spec.BindIP)
|
||||
Spec.Addr = net.JoinHostPort(Spec.BindIP, Spec.Port)
|
||||
|
||||
// 配置校验 服务名称地址及监听地址不能为空
|
||||
conf.NotNil(Spec.Service, Spec.Cache)
|
||||
|
||||
// 初始化加密SecretKey
|
||||
encipher.New(env.Runtime.JwtSecretKey)
|
||||
|
||||
conf.PrintInfo(Spec.Addr)
|
||||
}
|
||||
25
module/social/group/internal/impl/impl.go
Normal file
25
module/social/group/internal/impl/impl.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/cache/redis"
|
||||
"git.apinb.com/bsm-sdk/core/with"
|
||||
"bsm/full/module/social/group/internal/config"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
RedisService *redis.RedisClient
|
||||
EtcdService *clientv3.Client
|
||||
DBService *gorm.DB
|
||||
MemorySerice *cache.Cache
|
||||
)
|
||||
|
||||
func NewImpl() {
|
||||
// with activating
|
||||
MemorySerice = with.Memory(nil)
|
||||
RedisService = with.RedisCache(config.Spec.Cache) // redis cache
|
||||
DBService = with.Databases(config.Spec.Databases, nil) // model
|
||||
EtcdService = with.Etcd(config.Spec.Etcd) // etcd
|
||||
}
|
||||
15
module/social/group/internal/logic/basic/const.go
Normal file
15
module/social/group/internal/logic/basic/const.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package basic
|
||||
|
||||
const (
|
||||
ROLE_MEMBER = 0 // 普通会员
|
||||
ROLE_MASTER = 1 // 管理员
|
||||
ROLE_SUPER_MASTER = 2 // 超级管理员
|
||||
ROLE_CREATEOR = 10 // 创建者
|
||||
|
||||
DefaultMemberLimit = 500 // 群组最大成员数
|
||||
DefaultEnableAnyJoin bool = true // 是否开启无需验证即可加入群组
|
||||
)
|
||||
|
||||
var (
|
||||
filed string = "passport_identity as identity,nickname,avatar,sex,province,city,area,sign"
|
||||
)
|
||||
62
module/social/group/internal/logic/basic/create.go
Normal file
62
module/social/group/internal/logic/basic/create.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"bsm/full/module/social/group/internal/impl"
|
||||
"bsm/full/module/social/group/internal/models"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
// 创建群组
|
||||
func Create(ctx context.Context, in *pb.GroupItem) (reply *pb.StatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
record := &models.GroupBasic{
|
||||
Name: in.GetName(),
|
||||
Introduce: in.GetIntroduce(),
|
||||
Avatar: in.GetAvatar(),
|
||||
Notice: in.GetNotice(),
|
||||
MemberLimit: DefaultMemberLimit,
|
||||
EnableAnyJoin: DefaultEnableAnyJoin,
|
||||
MemberTotal: 1,
|
||||
}
|
||||
record.Identity = utils.UUID()
|
||||
record.PassportID = auth.ID
|
||||
record.PassportIdentity = auth.Identity
|
||||
|
||||
err = impl.DBService.Create(&record).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
member := &models.GroupMember{
|
||||
GroupID: record.ID,
|
||||
GroupIdentity: record.Identity,
|
||||
Role: ROLE_CREATEOR,
|
||||
}
|
||||
|
||||
member.PassportID = auth.ID
|
||||
member.PassportIdentity = auth.Identity
|
||||
|
||||
err = impl.DBService.Create(&member).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Data: record.Identity,
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
33
module/social/group/internal/logic/basic/disband.go
Normal file
33
module/social/group/internal/logic/basic/disband.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 解散群组
|
||||
func Disband(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// valildate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// TODO: add your logic code & delete this line.
|
||||
|
||||
return &pb.StatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
30
module/social/group/internal/logic/basic/fetch.go
Normal file
30
module/social/group/internal/logic/basic/fetch.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"bsm/full/module/social/group/internal/models"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
// 获取群组列表
|
||||
func Fetch(ctx context.Context, in *pb.Empty) (reply *pb.GroupsReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
groups, err := models.GetMyGroup(auth.ID)
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.GroupsReply{
|
||||
Total: int32(len(groups)),
|
||||
}, nil
|
||||
}
|
||||
26
module/social/group/internal/logic/basic/get.go
Normal file
26
module/social/group/internal/logic/basic/get.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"bsm/full/module/social/group/internal/impl"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
// 获取群组信息
|
||||
func Get(ctx context.Context, in *pb.IdentRequest) (reply *pb.GroupItem, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = impl.DBService.Where("identity=?", in.Identity).First(&reply).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.GroupItem{}, nil
|
||||
}
|
||||
46
module/social/group/internal/logic/basic/modify.go
Normal file
46
module/social/group/internal/logic/basic/modify.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"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"
|
||||
"bsm/full/module/social/group/internal/impl"
|
||||
"bsm/full/module/social/group/internal/models"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
// 修改群组信息
|
||||
func Modify(ctx context.Context, in *pb.GroupItem) (reply *pb.StatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
record := &models.GroupBasic{
|
||||
Name: in.GetName(),
|
||||
Introduce: in.GetIntroduce(),
|
||||
Avatar: in.GetAvatar(),
|
||||
Background: in.GetBackground(),
|
||||
Notice: in.GetNotice(),
|
||||
EnableSearchByNumber: in.GetEnableSearchByNumber(),
|
||||
EnableSearchByName: in.GetEnableSearchByName(),
|
||||
}
|
||||
record.Identity = utils.UUID()
|
||||
|
||||
err = impl.DBService.Where("identity=? and creator_id=?", in.Identity, auth.ID).UpdateColumns(record).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
22
module/social/group/internal/logic/basic/search.go
Normal file
22
module/social/group/internal/logic/basic/search.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
// 搜索
|
||||
func Search(ctx context.Context, in *pb.SearchRequest) (reply *pb.GroupsReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if in.Keyword == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
return &pb.GroupsReply{}, nil
|
||||
}
|
||||
15
module/social/group/internal/logic/member/const.go
Normal file
15
module/social/group/internal/logic/member/const.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package member
|
||||
|
||||
const (
|
||||
ROLE_MEMBER = 0 // 普通会员
|
||||
ROLE_MASTER = 1 // 管理员
|
||||
ROLE_SUPER_MASTER = 2 // 超级管理员
|
||||
ROLE_CREATEOR = 10 // 创建者
|
||||
|
||||
DefaultMemberLimit = 500 // 群组最大成员数
|
||||
DefaultEnableAnyJoin bool = true // 是否开启无需验证即可加入群组
|
||||
)
|
||||
|
||||
var (
|
||||
filed string = "passport_identity as identity,nickname,avatar,sex,province,city,area,sign"
|
||||
)
|
||||
62
module/social/group/internal/logic/member/do_join.go
Normal file
62
module/social/group/internal/logic/member/do_join.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package member
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"bsm/full/module/social/group/internal/impl"
|
||||
"bsm/full/module/social/group/internal/models"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
// 申请加群
|
||||
func DoJoin(ctx context.Context, in *pb.DoJoinRequest) (reply *pb.StatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//生成唯一标识
|
||||
identity := utils.UUID()
|
||||
|
||||
//判断是否需要加群验证
|
||||
if models.GetGroupEnableAnyJoin(auth.ID, in.Identity) {
|
||||
// 直接加入群成员
|
||||
apply := &models.GroupMember{
|
||||
GroupID: uint(in.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
|
||||
} else {
|
||||
// 写入加群申请表
|
||||
apply := &models.GroupApply{
|
||||
FromID: uint(auth.ID),
|
||||
FromIdentity: auth.Identity,
|
||||
GroupID: uint(in.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
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Data: identity,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
44
module/social/group/internal/logic/member/do_kick.go
Normal file
44
module/social/group/internal/logic/member/do_kick.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package member
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"bsm/full/module/social/group/internal/impl"
|
||||
"bsm/full/module/social/group/internal/models"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
// 踢人
|
||||
func DoKick(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
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 {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if tc == 0 {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
err = impl.DBService.Model(&models.GroupMember{}).Delete("group_identity=? and identity=?", in.GroupIdentity, in.Identity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
} else {
|
||||
//更新群组表数据统计
|
||||
models.UpsetGroupMemberTotal(in.GroupIdentity, "-")
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
47
module/social/group/internal/logic/member/do_quit.go
Normal file
47
module/social/group/internal/logic/member/do_quit.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package member
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"bsm/full/module/social/group/internal/impl"
|
||||
"bsm/full/module/social/group/internal/models"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
// 退群
|
||||
func DoQuit(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
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 {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if auth.ID == creator_id {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
err = impl.DBService.Model(&models.GroupMember{}).Delete("group_identity=? and passport_id=?", in.GroupIdentity, auth.ID).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
} else {
|
||||
//更新群组表数据统计
|
||||
models.UpsetGroupMemberTotal(in.GroupIdentity, "-")
|
||||
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
47
module/social/group/internal/logic/member/do_set_manager.go
Normal file
47
module/social/group/internal/logic/member/do_set_manager.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package member
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"bsm/full/module/social/group/internal/impl"
|
||||
"bsm/full/module/social/group/internal/models"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
// 设置或取消管理员
|
||||
func DoSetManager(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
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 {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if auth.ID != creator_id {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
switch strings.ToUpper(in.Direction) {
|
||||
case "SET":
|
||||
//设置管理员
|
||||
impl.DBService.Model(&models.GroupMember{}).Where("identity=?", in.Identity).UpdateColumn("role", ROLE_MASTER)
|
||||
case "CANCEL":
|
||||
//取消管理员
|
||||
impl.DBService.Model(&models.GroupMember{}).Where("identity=?", in.Identity).UpdateColumn("role", ROLE_MEMBER)
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
34
module/social/group/internal/logic/member/fetch.go
Normal file
34
module/social/group/internal/logic/member/fetch.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package member
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"bsm/full/module/social/group/internal/models"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
// 获取群组成员列表
|
||||
func Fetch(ctx context.Context, in *pb.IdentRequest) (reply *pb.GroupMemberReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
members, err := models.GetGroupMember(in.Identity)
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.GroupMemberReply{
|
||||
Total: int32(len(members)),
|
||||
}, nil
|
||||
}
|
||||
68
module/social/group/internal/logic/member/join_do_handle.go
Normal file
68
module/social/group/internal/logic/member/join_do_handle.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package member
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"bsm/full/module/social/group/internal/impl"
|
||||
"bsm/full/module/social/group/internal/models"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
// 申请加群处理
|
||||
func JoinDoHandle(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusReply, err error) {
|
||||
_, 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)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Data: identity,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
44
module/social/group/internal/logic/member/join_fetch.go
Normal file
44
module/social/group/internal/logic/member/join_fetch.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package member
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"bsm/full/module/social/group/internal/impl"
|
||||
"bsm/full/module/social/group/internal/models"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
// 申请加群列表
|
||||
func JoinFetch(ctx context.Context, in *pb.Empty) (reply *pb.JoinFetchReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fetch := 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 {
|
||||
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
|
||||
}
|
||||
fetch[idx].From = card
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.JoinFetchReply{
|
||||
Total: int32(len(fetch)),
|
||||
Applys: fetch,
|
||||
}, nil
|
||||
}
|
||||
21
module/social/group/internal/models/group_apply.go
Normal file
21
module/social/group/internal/models/group_apply.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
)
|
||||
|
||||
// GroupApply 入群申请
|
||||
type GroupApply struct {
|
||||
types.Std_IICUDS
|
||||
types.Std_Passport
|
||||
FromID uint `gorm:"column:from_id;not null;" json:"from_id"` //申请人id
|
||||
FromIdentity string `gorm:"column:from_identity;type:varchar(36);not null;" json:"from_identity"` //申请人identity
|
||||
GroupID uint `gorm:"column:group_id;not null;" json:"group_id"`
|
||||
GroupIdentity string `gorm:"column:group_identity;type:varchar(36);not null;" json:"group_identity"`
|
||||
Message string `gorm:"column:message;type:varchar(255);default:'';" json:"message"`
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (ga *GroupApply) TableName() string {
|
||||
return "relation_group_apply"
|
||||
}
|
||||
30
module/social/group/internal/models/group_basic.go
Normal file
30
module/social/group/internal/models/group_basic.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
)
|
||||
|
||||
// Group .
|
||||
type GroupBasic struct {
|
||||
types.Std_IICUDS
|
||||
types.Std_Passport
|
||||
Number int64 `gorm:"column:number;not null;" json:"number"` //群号
|
||||
Avatar string `gorm:"column:avatar;type:varchar(255);default:'';" json:"avatar"`
|
||||
Name string `gorm:"column:name;type:varchar(255);not null;" json:"name"`
|
||||
Introduce string `gorm:"column:introduce;type:text;default:'';" json:"introduce"` //简介
|
||||
MemberLimit int32 `gorm:"column:member_limit;type:smallint;default:100;" json:"member_limit"` // 最大成员数,0代表不限制
|
||||
Province int32 `gorm:"column:province;default:0;" json:"province"` // 省
|
||||
City int32 `gorm:"column:city;default:0;" json:"city"` // 市
|
||||
Area int32 `gorm:"column:area;default:0;" json:"area"` // 区
|
||||
Notice string `gorm:"column:notice;type:text;default:'';" json:"notice"` // 群公告
|
||||
Background string `gorm:"column:background;type:varchar(255);default:'';" json:"background"` // 背景
|
||||
EnableAnyJoin bool `gorm:"column:enable_any_join;" json:"enable_any_join"` // 是否开启无需验证即可加入群组
|
||||
EnableSearchByNumber bool `gorm:"column:enable_search_by_number;" json:"enable_search_by_number"` // 是否开启群号搜索
|
||||
EnableSearchByName bool `gorm:"column:enable_search_by_name;" json:"enable_search_by_name"` // 是否开启群名搜索
|
||||
MemberTotal int32 `gorm:"column:member_total;default:0;" json:"member_total"` // 群组成员数
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (group *GroupBasic) TableName() string {
|
||||
return "group_basic"
|
||||
}
|
||||
20
module/social/group/internal/models/group_member.go
Normal file
20
module/social/group/internal/models/group_member.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
)
|
||||
|
||||
// GroupMember 群成员
|
||||
type GroupMember struct {
|
||||
types.Std_IICUDS
|
||||
types.Std_Passport
|
||||
GroupID uint `gorm:"group_id;not null;" json:"group_id"`
|
||||
GroupIdentity string `gorm:"column:group_identity;type:varchar(36);not null;" json:"group_identity"` //group_identity
|
||||
RemarkName string `gorm:"column:remark_name;type:varchar(255);default:'';" json:"remark_name"` //昵称备注
|
||||
Role int32 `gorm:"role;type:smallint;default:0" json:"role"` //0:普通成员,1:管理员,2:超管,10:创建者
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (gm *GroupMember) TableName() string {
|
||||
return "relation_group_member"
|
||||
}
|
||||
21
module/social/group/internal/models/group_res.go
Normal file
21
module/social/group/internal/models/group_res.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
)
|
||||
|
||||
// GroupFiles 群文件
|
||||
type GroupRes struct {
|
||||
types.Std_IICUDS
|
||||
types.Std_Passport
|
||||
GroupID string `gorm:"column:group_id;type:varchar(50);not null;" json:"group_id"`
|
||||
Name string `gorm:"column:name;type:varchar(255);not null;" json:"name"`
|
||||
Introduce string `gorm:"column:introduce;type:varchar(255);default:'';" json:"introduce"` //简介
|
||||
URL string `gorm:"column:url;type:varchar(255);default:'';" json:"url"`
|
||||
DownloadNum int64 `gorm:"column:download_num;default:0;" json:"download_num"`
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (gf *GroupRes) TableName() string {
|
||||
return "relation_group_res"
|
||||
}
|
||||
47
module/social/group/internal/models/query.go
Normal file
47
module/social/group/internal/models/query.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"bsm/full/module/social/group/internal/impl"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func InitData() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetMyGroup(passport_id uint) (groups []*pb.GroupItem, err error) {
|
||||
|
||||
sql := `Select group.*,gm.nickname,gm.remark_name,gm.role
|
||||
From group_member as gm
|
||||
Left join group on group.id=gm.group_id
|
||||
Where passport_id=? and deleted_at is null`
|
||||
err = impl.DBService.Raw(sql, passport_id).Find(&groups).Error
|
||||
return
|
||||
}
|
||||
|
||||
func GetMasterGroup(passport_id uint) []uint {
|
||||
var ids []uint
|
||||
impl.DBService.Model(GroupMember{}).Select("group_id").Where("passport_id=? and role!=0", passport_id).Pluck("group_id", &ids)
|
||||
return ids
|
||||
}
|
||||
|
||||
func GetGroupMember(group_identity string) (members []*pb.PassportInfoSimpleCard, err error) {
|
||||
sql := `Select pe.identity,pe.nickname,pe.avatar,pe.sex,gm.remark_name,gm.role
|
||||
From group_member as gm
|
||||
Left join passport_extend as pe on pe.passport_id=gm.passport_id
|
||||
Where group_identity=? and deleted_at is null`
|
||||
err = impl.DBService.Raw(sql, group_identity).Find(&members).Error
|
||||
return
|
||||
}
|
||||
|
||||
func GetGroupEnableAnyJoin(passport_id uint, group_identity string) bool {
|
||||
var is bool = false
|
||||
impl.DBService.Model(&GroupBasic{}).Select("enable_any_join").Where("createor_id=? and identity=?", passport_id, group_identity).Scan(&is)
|
||||
return is
|
||||
}
|
||||
|
||||
func UpsetGroupMemberTotal(group_identity string, op string) {
|
||||
impl.DBService.Model(&GroupBasic{}).Where("identity=?", group_identity).UpdateColumn("member_total", gorm.Expr("member_total ? ?", op, 1))
|
||||
}
|
||||
46
module/social/group/internal/server/basic_server.go
Normal file
46
module/social/group/internal/server/basic_server.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/social/group/internal/logic/basic"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
type BasicServer struct {
|
||||
pb.UnimplementedBasicServer
|
||||
}
|
||||
|
||||
func NewBasicServer() *BasicServer {
|
||||
return &BasicServer{}
|
||||
}
|
||||
|
||||
// 搜索
|
||||
func (s *BasicServer) Search(ctx context.Context, in *pb.SearchRequest) (*pb.GroupsReply, error) {
|
||||
return basic.Search(ctx, in)
|
||||
}
|
||||
|
||||
// 获取群组列表
|
||||
func (s *BasicServer) Fetch(ctx context.Context, in *pb.Empty) (*pb.GroupsReply, error) {
|
||||
return basic.Fetch(ctx, in)
|
||||
}
|
||||
|
||||
// 获取群组信息
|
||||
func (s *BasicServer) Get(ctx context.Context, in *pb.IdentRequest) (*pb.GroupItem, error) {
|
||||
return basic.Get(ctx, in)
|
||||
}
|
||||
|
||||
// 创建群组
|
||||
func (s *BasicServer) Create(ctx context.Context, in *pb.GroupItem) (*pb.StatusReply, error) {
|
||||
return basic.Create(ctx, in)
|
||||
}
|
||||
|
||||
// 修改群组信息
|
||||
func (s *BasicServer) Modify(ctx context.Context, in *pb.GroupItem) (*pb.StatusReply, error) {
|
||||
return basic.Modify(ctx, in)
|
||||
}
|
||||
|
||||
// 解散群组
|
||||
func (s *BasicServer) Disband(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
return basic.Disband(ctx, in)
|
||||
}
|
||||
51
module/social/group/internal/server/member_server.go
Normal file
51
module/social/group/internal/server/member_server.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/social/group/internal/logic/member"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
)
|
||||
|
||||
type MemberServer struct {
|
||||
pb.UnimplementedMemberServer
|
||||
}
|
||||
|
||||
func NewMemberServer() *MemberServer {
|
||||
return &MemberServer{}
|
||||
}
|
||||
|
||||
// 获取群组成员列表
|
||||
func (s *MemberServer) Fetch(ctx context.Context, in *pb.IdentRequest) (*pb.GroupMemberReply, error) {
|
||||
return member.Fetch(ctx, in)
|
||||
}
|
||||
|
||||
// 申请加群
|
||||
func (s *MemberServer) DoJoin(ctx context.Context, in *pb.DoJoinRequest) (*pb.StatusReply, error) {
|
||||
return member.DoJoin(ctx, in)
|
||||
}
|
||||
|
||||
// 申请加群列表
|
||||
func (s *MemberServer) JoinFetch(ctx context.Context, in *pb.Empty) (*pb.JoinFetchReply, error) {
|
||||
return member.JoinFetch(ctx, in)
|
||||
}
|
||||
|
||||
// 申请加群处理
|
||||
func (s *MemberServer) JoinDoHandle(ctx context.Context, in *pb.GroupOPRequest) (*pb.StatusReply, error) {
|
||||
return member.JoinDoHandle(ctx, in)
|
||||
}
|
||||
|
||||
// 设置或取消管理员
|
||||
func (s *MemberServer) DoSetManager(ctx context.Context, in *pb.GroupOPRequest) (*pb.StatusReply, error) {
|
||||
return member.DoSetManager(ctx, in)
|
||||
}
|
||||
|
||||
// 踢人
|
||||
func (s *MemberServer) DoKick(ctx context.Context, in *pb.GroupOPRequest) (*pb.StatusReply, error) {
|
||||
return member.DoKick(ctx, in)
|
||||
}
|
||||
|
||||
// 退群
|
||||
func (s *MemberServer) DoQuit(ctx context.Context, in *pb.GroupOPRequest) (*pb.StatusReply, error) {
|
||||
return member.DoQuit(ctx, in)
|
||||
}
|
||||
73
module/social/group/internal/server/new.go
Normal file
73
module/social/group/internal/server/new.go
Normal file
@@ -0,0 +1,73 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Grpc *grpc.Server
|
||||
Ctx context.Context
|
||||
Mux *gwRuntime.ServeMux
|
||||
}
|
||||
|
||||
func New(addr string) *Server {
|
||||
srv := &Server{Ctx: context.Background(), Grpc: grpc.NewServer(), Mux: gwRuntime.NewServeMux(
|
||||
gwRuntime.WithForwardResponseRewriter(responseEnvelope),
|
||||
)}
|
||||
|
||||
// register service to grpc.Server
|
||||
pb.RegisterBasicServer(srv.Grpc, NewBasicServer())
|
||||
pb.RegisterMemberServer(srv.Grpc, NewMemberServer())
|
||||
|
||||
reflection.Register(srv.Grpc)
|
||||
|
||||
// 将服务注册到Gateway
|
||||
opts := []grpc.DialOption{grpc.WithInsecure()}
|
||||
pb.RegisterBasicHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterMemberHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
|
||||
// Register services swagger
|
||||
srv.RegisterSwagger()
|
||||
|
||||
return srv
|
||||
}
|
||||
|
||||
// RegisterSwagger 注册swagger
|
||||
func (s *Server) RegisterSwagger() {
|
||||
srvKey := strings.ToLower(vars.ServiceKey)
|
||||
s.Mux.HandlePath("GET", "/"+srvKey+".swagger.json", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
bytes, err := os.ReadFile("./swagger/" + srvKey + ".swagger.json")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
w.Write(bytes)
|
||||
return
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// response envelope
|
||||
func responseEnvelope(_ context.Context, response proto.Message) (interface{}, error) {
|
||||
name := string(response.ProtoReflect().Descriptor().Name())
|
||||
if name == "Status" || name == "Error" || name == "StatusReply" {
|
||||
return response, nil
|
||||
}
|
||||
return map[string]any{
|
||||
"code": 0,
|
||||
"message": "OK",
|
||||
"result": response,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user