feat: add im kafka admin integration
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
package imconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
api "hotgo/api/admin/imconfiginternal"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/library/imtenant"
|
||||
"hotgo/internal/model/entity"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var Config = cConfig{}
|
||||
|
||||
type cConfig struct{}
|
||||
|
||||
func (c *cConfig) Tenant(ctx context.Context, req *api.TenantReq) (res *api.TenantRes, err error) {
|
||||
tenant, err := getTenant(ctx, dao.ImTenant.Columns().Code, req.Code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &api.TenantRes{TenantModel: tenantToModel(tenant)}, nil
|
||||
}
|
||||
|
||||
func (c *cConfig) TenantByID(ctx context.Context, req *api.TenantByIDReq) (res *api.TenantRes, err error) {
|
||||
tenant, err := getTenant(ctx, dao.ImTenant.Columns().Id, req.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &api.TenantRes{TenantModel: tenantToModel(tenant)}, nil
|
||||
}
|
||||
|
||||
func (c *cConfig) TenantList(ctx context.Context, req *api.TenantListReq) (res *api.TenantListRes, err error) {
|
||||
limit := req.Limit
|
||||
if limit <= 0 || limit > 1000 {
|
||||
limit = 1000
|
||||
}
|
||||
mod := imtenant.RegistryDB().Model(dao.ImTenant.Table()).Safe().Ctx(ctx).Limit(limit)
|
||||
if req.State != "" {
|
||||
if req.State == "active" {
|
||||
mod = mod.Where(dao.ImTenant.Columns().State, 1)
|
||||
} else {
|
||||
mod = mod.Where(dao.ImTenant.Columns().State, req.State)
|
||||
}
|
||||
}
|
||||
var tenants []*entity.ImTenant
|
||||
if err = mod.OrderAsc(dao.ImTenant.Columns().Id).Scan(&tenants); err != nil {
|
||||
return nil, gerror.Wrap(err, "获取IM租户配置失败")
|
||||
}
|
||||
res = &api.TenantListRes{Items: make([]*api.TenantModel, 0, len(tenants))}
|
||||
for _, tenant := range tenants {
|
||||
res.Items = append(res.Items, tenantToModel(tenant))
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *cConfig) RegistrationSms(ctx context.Context, req *api.RegistrationSmsReq) (res *api.RegistrationSmsRes, err error) {
|
||||
var record *entity.SysConfig
|
||||
if err = dao.SysConfig.Ctx(ctx).
|
||||
Where(dao.SysConfig.Columns().Group, "im").
|
||||
Where(dao.SysConfig.Columns().Key, "register_sms_enabled").
|
||||
Where(dao.SysConfig.Columns().Status, 1).
|
||||
Scan(&record); err != nil {
|
||||
return nil, gerror.Wrap(err, "获取IM注册短信配置失败")
|
||||
}
|
||||
enabled := true
|
||||
if record != nil {
|
||||
enabled = gconv.String(record.Value) != "0"
|
||||
}
|
||||
return &api.RegistrationSmsRes{Enabled: enabled}, nil
|
||||
}
|
||||
|
||||
func (c *cConfig) ThirdParty(ctx context.Context, req *api.ThirdPartyReq) (res *api.ThirdPartyRes, err error) {
|
||||
values := map[string]string{}
|
||||
keys := splitKeys(req.Keys)
|
||||
mod := dao.SysConfig.Ctx(ctx).Where(dao.SysConfig.Columns().Group, req.Group).Where(dao.SysConfig.Columns().Status, 1)
|
||||
if len(keys) > 0 {
|
||||
mod = mod.WhereIn(dao.SysConfig.Columns().Key, keys)
|
||||
}
|
||||
var rows []*entity.SysConfig
|
||||
if err = mod.Scan(&rows); err != nil {
|
||||
return nil, gerror.Wrap(err, "获取IM第三方配置失败")
|
||||
}
|
||||
for _, row := range rows {
|
||||
values[row.Key] = row.Value
|
||||
}
|
||||
return &api.ThirdPartyRes{Values: values}, nil
|
||||
}
|
||||
|
||||
func getTenant(ctx context.Context, field string, value any) (*entity.ImTenant, error) {
|
||||
var tenant *entity.ImTenant
|
||||
if err := imtenant.RegistryDB().Model(dao.ImTenant.Table()).Safe().Ctx(ctx).Where(field, value).Scan(&tenant); err != nil {
|
||||
return nil, gerror.Wrap(err, "获取IM租户配置失败")
|
||||
}
|
||||
if tenant == nil || tenant.Id <= 0 {
|
||||
return nil, gerror.New("IM租户不存在")
|
||||
}
|
||||
return tenant, nil
|
||||
}
|
||||
|
||||
func tenantToModel(tenant *entity.ImTenant) *api.TenantModel {
|
||||
if tenant == nil {
|
||||
return nil
|
||||
}
|
||||
desc := tenant.TenantDesc
|
||||
return &api.TenantModel{
|
||||
ID: tenant.Id,
|
||||
Code: tenant.Code,
|
||||
Name: tenant.Name,
|
||||
TenantDesc: &desc,
|
||||
DBName: tenant.DbName,
|
||||
DBDSN: tenant.DbDsn,
|
||||
State: tenant.State,
|
||||
CreatedAt: gtimeToRFC3339(tenant.CreatedAt),
|
||||
CreatedBy: tenant.CreatedBy,
|
||||
UpdatedAt: gtimeToRFC3339(tenant.UpdatedAt),
|
||||
UpdatedBy: tenant.UpdatedBy,
|
||||
}
|
||||
}
|
||||
|
||||
func gtimeToRFC3339(t *gtime.Time) string {
|
||||
if t == nil {
|
||||
return ""
|
||||
}
|
||||
return t.Time.Format("2006-01-02T15:04:05Z07:00")
|
||||
}
|
||||
|
||||
func splitKeys(raw string) []string {
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
parts := strings.Split(raw, ",")
|
||||
keys := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
if part = strings.TrimSpace(part); part != "" {
|
||||
keys = append(keys, part)
|
||||
}
|
||||
}
|
||||
return keys
|
||||
}
|
||||
Reference in New Issue
Block a user