package sys import ( "context" "hotgo/internal/consts" "hotgo/internal/dao" "hotgo/internal/library/contexts" "hotgo/internal/library/imkafka" "hotgo/internal/model/input/sysin" "hotgo/internal/service" "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/util/gconv" ) const ( imRegistrationConfigGroup = "im" imRegistrationSmsKey = "register_sms_enabled" ) type sSysImRegistration struct{} func NewSysImRegistration() *sSysImRegistration { return &sSysImRegistration{} } func init() { service.RegisterSysImRegistration(NewSysImRegistration()) } func (s *sSysImRegistration) GetConfig(ctx context.Context) (res *sysin.ImRegistrationConfigModel, err error) { if err = s.verifySuper(ctx); err != nil { return } return s.getConfig(ctx) } func (s *sSysImRegistration) UpdateConfig(ctx context.Context, in *sysin.ImRegistrationConfigUpdateInp) (err error) { if err = s.verifySuper(ctx); err != nil { return } old, err := s.getConfig(ctx) if err != nil { return } value := "0" if in.RegisterSmsEnabled { value = "1" } _, err = dao.SysConfig.Ctx(ctx). Where(dao.SysConfig.Columns().Group, imRegistrationConfigGroup). Where(dao.SysConfig.Columns().Key, imRegistrationSmsKey). Data(g.Map{ dao.SysConfig.Columns().Value: value, dao.SysConfig.Columns().UpdatedAt: gtime.Now(), }). Update() if err != nil { return gerror.Wrap(err, "更新IM注册短信开关失败") } _ = writeImAdminAudit(ctx, 0, "registration_sms_config_update", "config", 0, "success", g.Map{ "old": old.RegisterSmsEnabled, "new": in.RegisterSmsEnabled, }) actorName := "" if user := contexts.GetUser(ctx); user != nil { actorName = user.RealName } if eventErr := imkafka.PublishConfigEvent(ctx, imkafka.ConfigEventInput{ EventType: consts.ImConfigEventRegistrationUpdated, BizID: "registration", Group: imRegistrationConfigGroup, Keys: []string{imRegistrationSmsKey}, Actor: &imkafka.Actor{ Type: "admin", ID: gconv.String(contexts.GetUserId(ctx)), DisplayName: actorName, }, CreatedBy: contexts.GetUserId(ctx), }); eventErr != nil { g.Log().Warningf(ctx, "发送IM注册配置Kafka事件失败 err:%+v", eventErr) } return } func (s *sSysImRegistration) getConfig(ctx context.Context) (res *sysin.ImRegistrationConfigModel, err error) { var record gdb.Record record, err = dao.SysConfig.Ctx(ctx). Where(dao.SysConfig.Columns().Group, imRegistrationConfigGroup). Where(dao.SysConfig.Columns().Key, imRegistrationSmsKey). Where(dao.SysConfig.Columns().Status, 1). One() if err != nil { return nil, gerror.Wrap(err, "获取IM注册短信开关失败") } if record.IsEmpty() { if err = s.seedConfig(ctx); err != nil { return nil, err } return s.getConfig(ctx) } return &sysin.ImRegistrationConfigModel{ RegisterSmsEnabled: record[dao.SysConfig.Columns().Value].String() == "1", UpdatedAt: record[dao.SysConfig.Columns().UpdatedAt].GTime(), }, nil } func (s *sSysImRegistration) seedConfig(ctx context.Context) (err error) { _, err = dao.SysConfig.Ctx(ctx).Data(g.Map{ dao.SysConfig.Columns().Group: imRegistrationConfigGroup, dao.SysConfig.Columns().Name: "注册短信发送", dao.SysConfig.Columns().Type: "bool", dao.SysConfig.Columns().Key: imRegistrationSmsKey, dao.SysConfig.Columns().Value: "1", dao.SysConfig.Columns().DefaultValue: "1", dao.SysConfig.Columns().Sort: 10, dao.SysConfig.Columns().Tip: "控制 IM 注册验证码是否真实发送", dao.SysConfig.Columns().IsDefault: 1, dao.SysConfig.Columns().Status: 1, dao.SysConfig.Columns().CreatedAt: gtime.Now(), dao.SysConfig.Columns().UpdatedAt: gtime.Now(), }).Insert() if err != nil { return gerror.Wrap(err, "初始化IM注册短信开关失败") } return } func (s *sSysImRegistration) verifySuper(ctx context.Context) error { if !isImPlatformAdmin(ctx, contexts.GetUserId(ctx)) { return gerror.New("仅平台超级管理员可操作IM注册配置") } return nil }