117 lines
3.4 KiB
Go
117 lines
3.4 KiB
Go
package sys
|
|
|
|
import (
|
|
"context"
|
|
"hotgo/internal/dao"
|
|
"hotgo/internal/library/contexts"
|
|
"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"
|
|
)
|
|
|
|
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,
|
|
})
|
|
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 !service.AdminMember().VerifySuperId(ctx, contexts.GetUserId(ctx)) {
|
|
return gerror.New("仅平台超级管理员可操作IM注册配置")
|
|
}
|
|
return nil
|
|
}
|