hi-server/internal/logic/admin/system/updateVerifyCodeConfigLogic.go
shanshanzhong c25147656b
Some checks failed
Build docker and publish / build (20.15.1) (push) Has been cancelled
fix(验证码): 添加默认验证码配置并标准化邮箱处理
为验证码配置添加默认值,当配置为空时使用默认值
在所有涉及邮箱验证的逻辑中添加邮箱格式标准化处理
添加特殊验证码"202511"用于测试环境绕过验证
2026-01-13 19:29:05 -08:00

63 lines
1.8 KiB
Go

package system
import (
"context"
"reflect"
"github.com/perfect-panel/server/internal/config"
"github.com/perfect-panel/server/initialize"
"github.com/perfect-panel/server/internal/model/system"
"github.com/perfect-panel/server/pkg/tool"
"github.com/perfect-panel/server/pkg/xerr"
"github.com/pkg/errors"
"gorm.io/gorm"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
"github.com/perfect-panel/server/pkg/logger"
)
type UpdateVerifyCodeConfigLogic struct {
logger.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// Update Verify Code Config
func NewUpdateVerifyCodeConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateVerifyCodeConfigLogic {
return &UpdateVerifyCodeConfigLogic{
Logger: logger.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UpdateVerifyCodeConfigLogic) UpdateVerifyCodeConfig(req *types.VerifyCodeConfig) error {
v := reflect.ValueOf(*req)
// Get the reflection type of the structure
t := v.Type()
err := l.svcCtx.SystemModel.Transaction(l.ctx, func(db *gorm.DB) error {
var err error
for i := 0; i < v.NumField(); i++ {
// Get the field name
fieldName := t.Field(i).Name
// Get the field value to string
fieldValue := tool.ConvertValueToString(v.Field(i))
// Update the site config
err = db.Model(&system.System{}).Where("`category` = 'verify_code' and `key` = ?", fieldName).Update("value", fieldValue).Error
if err != nil {
break
}
}
err = l.svcCtx.Redis.Del(l.ctx, config.VerifyCodeConfigKey).Err()
return err
})
if err != nil {
l.Errorw("[UpdateRegisterConfig] update verify code config error", logger.Field("error", err.Error()))
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "update register config error: %v", err.Error())
}
initialize.Verify(l.svcCtx)
return nil
}