diff --git a/api/init.go b/api/init.go index dfa877a..b87e6e4 100644 --- a/api/init.go +++ b/api/init.go @@ -12,6 +12,7 @@ import ( "github.com/veypi/OneAuth/api/sms" "github.com/veypi/OneAuth/api/token" "github.com/veypi/OneAuth/api/user" + "github.com/veypi/OneAuth/cfg" "github.com/veypi/OneAuth/libs/auth" "github.com/vyes-ai/vigo" "github.com/vyes-ai/vigo/contrib/common" @@ -24,8 +25,15 @@ var ( _ = Router.Extend("token", token.Router) _ = Router.Extend("app", app.Router) _ = Router.Extend("sms", sms.Router) + _ = Router.Get("cfg", vigo.SkipBefore, getCfg) ) var _ = Router.Any("*", vigo.SkipBefore, func(x *vigo.X) error { return vigo.ErrNotFound }) + +func getCfg(x *vigo.X) any { + return map[string]any{ + "sms": cfg.Config.SMS.Enable, + } +} diff --git a/api/user/create.go b/api/user/create.go index f523352..c91f132 100644 --- a/api/user/create.go +++ b/api/user/create.go @@ -10,11 +10,10 @@ package user import ( "encoding/base64" "fmt" + "math/rand" "strings" "time" - "math/rand" - "github.com/google/uuid" "github.com/veypi/OneAuth/api/sms" "github.com/veypi/OneAuth/cfg" @@ -47,11 +46,13 @@ func userPost(x *vigo.X) (any, error) { data := &models.User{} data.ID = strings.ReplaceAll(uuid.New().String(), "-", "") - data.Phone = opts.Region + opts.Phone - data.Region = opts.Region - err = sms.VerifyCode(opts.Phone, opts.VerifyCode, opts.Region, "signup") - if err != nil { - return nil, vigo.ErrArgInvalid.WithArgs("verify code").WithString(err.Error()) + if cfg.Config.SMS.Enable { + data.Phone = opts.Region + opts.Phone + data.Region = opts.Region + err = sms.VerifyCode(opts.Phone, opts.VerifyCode, opts.Region, "signup") + if err != nil { + return nil, vigo.ErrArgInvalid.WithArgs("verify code").WithString(err.Error()) + } } data.Username = opts.Username data.Code = opts.Code diff --git a/cfg/sms.go b/cfg/sms.go index 5abecf3..293ba46 100644 --- a/cfg/sms.go +++ b/cfg/sms.go @@ -4,15 +4,15 @@ import ( "time" ) -// Config 短信验证码配置 +// SMSConfig 短信验证码配置 type SMSConfig struct { - // 区域配置 +86/.../global Regions map[string]RegionConfig `json:"regions"` Default RegionConfig `json:"default"` // 默认区域配置 // 全局配置 Global GlobalConfig `json:"global"` + Enable bool `json:"enable"` } // RegionConfig 区域配置 @@ -37,6 +37,7 @@ type GlobalConfig struct { // DefaultConfig 默认配置 func defaultSMS() *SMSConfig { return &SMSConfig{ + Enable: true, Global: GlobalConfig{ CodeLength: 6, CodeExpiry: 5 * time.Minute, diff --git a/cli/main.go b/cli/main.go index ef69660..d376b27 100644 --- a/cli/main.go +++ b/cli/main.go @@ -16,9 +16,11 @@ import ( "github.com/vyes-ai/vigo/logv" ) -var cmdMain = flags.New("app", "the backend server of app") -var cmdCfg = cmdMain.SubCommand("cfg", "generate cfg file") -var cmdDB = cmdMain.SubCommand("db", "database operations") +var ( + cmdMain = flags.New("app", "the backend server of app") + cmdCfg = cmdMain.SubCommand("cfg", "generate cfg file") + cmdDB = cmdMain.SubCommand("db", "database operations") +) var configFile = cmdMain.String("f", "./dev.yaml", "the config file") @@ -36,7 +38,7 @@ func init() { cmdMain.StringVar(&cliOpts.Host, "host", "0.0.0.0", "host") cmdMain.IntVar(&cliOpts.Port, "p", 4000, "port") cmdMain.StringVar(&cliOpts.LoggerLevel, "l", "info", "log level") - cmdMain.AutoRegister(cliOpts) + cmdMain.AutoRegister(cfg.Config) cmdMain.Before = func() error { flags.LoadCfg(*configFile, cfg.Config) diff --git a/go.mod b/go.mod index bf09970..6af958c 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/golang-jwt/jwt/v5 v5.2.3 github.com/google/uuid v1.6.0 github.com/veypi/vyes-ui v0.0.0-00010101000000-000000000000 - github.com/vyes-ai/vigo v0.5.0 + github.com/vyes-ai/vigo v0.5.2 golang.org/x/crypto v0.40.0 gorm.io/driver/mysql v1.6.0 gorm.io/driver/postgres v1.6.0 diff --git a/ui/env.js b/ui/env.js index 5c8eae1..c07c3de 100644 --- a/ui/env.js +++ b/ui/env.js @@ -26,10 +26,12 @@ export default ($env) => { } }; $env.$axios.interceptors.response.use(function(response) { - console.log(response) return response?.data || response; }, function(error) { error = error?.response?.data || error?.response || error return Promise.reject(error); }); + $env.$axios.get('/api/cfg').then(res => { + $env.$G.cfg = res + }) } diff --git a/ui/page/login.html b/ui/page/login.html index 945d6f3..3a1b1c5 100644 --- a/ui/page/login.html +++ b/ui/page/login.html @@ -403,7 +403,7 @@ -