diff --git a/go.mod b/go.mod index b6d1586f..238a5b58 100644 --- a/go.mod +++ b/go.mod @@ -24,8 +24,8 @@ require ( github.com/robertkrimen/otto v0.0.0-20210927222213-f9375a256948 // indirect github.com/spf13/cobra v1.2.1 // indirect github.com/stretchr/testify v1.7.0 - github.com/yaoapp/gou v0.0.0-20210929112923-09baf3629b95 // indirect - github.com/yaoapp/kun v0.6.4 + github.com/yaoapp/gou v0.0.0-20210929115559-b1b0614e8d88 // indirect + github.com/yaoapp/kun v0.6.5 github.com/yaoapp/xun v0.5.2 // indirect golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect diff --git a/go.sum b/go.sum index 047cf8f0..af0bdb08 100644 --- a/go.sum +++ b/go.sum @@ -371,6 +371,8 @@ github.com/yaoapp/gou v0.0.0-20210929100518-8de252e7bab8 h1:MPVIqqqF5PV3SEy9a4XN github.com/yaoapp/gou v0.0.0-20210929100518-8de252e7bab8/go.mod h1:mJj2wa3oXVlk/WOHazW8eEZTyva98zDVjIur9lj8aiA= github.com/yaoapp/gou v0.0.0-20210929112923-09baf3629b95 h1:KOMi0RR1ynNgLyaN7YIXVcS0Aj5YUhkad1IQ7Px8SL4= github.com/yaoapp/gou v0.0.0-20210929112923-09baf3629b95/go.mod h1:EKDxzk74c04dULx5+lJfTpMkR+z4QswAl0NC7DXDiWU= +github.com/yaoapp/gou v0.0.0-20210929115559-b1b0614e8d88 h1:rAjfDVt6CBjcQGE6ie4U7tqmvmU355lIf+Ww0cL1WjQ= +github.com/yaoapp/gou v0.0.0-20210929115559-b1b0614e8d88/go.mod h1:vcNZXFRgGuZWzjtYH65ck1o6nzWYDpToQ09zHC9xNDE= github.com/yaoapp/kun v0.6.1 h1:gbjP5wmAuJLe1RGZ2evH4rPCVWykf0iqbksaHikF/GI= github.com/yaoapp/kun v0.6.1/go.mod h1:igsTcWDnzpp0HtRN7sBP+XOEN2tzoC5hk2MyoPFs3xA= github.com/yaoapp/kun v0.6.2 h1:QbtdZpVIklRDGEvL5YYRf/eR05YFZVFk/8rX2vy2K0Q= @@ -379,6 +381,8 @@ github.com/yaoapp/kun v0.6.3 h1:0wD0lo0Cl78O+CoKi+DRVp6AeQFSrFInF+ghu+mww8M= github.com/yaoapp/kun v0.6.3/go.mod h1:igsTcWDnzpp0HtRN7sBP+XOEN2tzoC5hk2MyoPFs3xA= github.com/yaoapp/kun v0.6.4 h1:5iEoZwaE0GZCLzB5uGmEr8X547CGvy0L61Ochmp47nY= github.com/yaoapp/kun v0.6.4/go.mod h1:igsTcWDnzpp0HtRN7sBP+XOEN2tzoC5hk2MyoPFs3xA= +github.com/yaoapp/kun v0.6.5 h1:RNEQp8EIiyGKyX64QfyoP8OreQBg0cCtF15xwUEUCNI= +github.com/yaoapp/kun v0.6.5/go.mod h1:igsTcWDnzpp0HtRN7sBP+XOEN2tzoC5hk2MyoPFs3xA= github.com/yaoapp/xun v0.5.2 h1:DedZ26FpcXfkLnPKXzJsrbVmpWDRx9adUo8AvOcNT+g= github.com/yaoapp/xun v0.5.2/go.mod h1:y107NMHO635nhqJxMt582i1iqLjnNxAoeYr5/500UBw= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/user/captcha.go b/user/captcha.go index 65f7ef7b..ef97cfac 100644 --- a/user/captcha.go +++ b/user/captcha.go @@ -6,6 +6,8 @@ import ( "github.com/mojocn/base64Captcha" "github.com/yaoapp/kun/exception" + "github.com/yaoapp/xiang/config" + "github.com/yaoapp/xiang/xlog" ) var captchaStore = base64Captcha.DefaultMemStore @@ -76,6 +78,12 @@ func MakeCaptcha(option CaptchaOption) (string, string) { if err != nil { exception.New("生成验证码出错 %s", 500, err).Throw() } + + // 打印日志 + if config.Conf.Mode == "debug" { + xlog.Println("图形/音频验证码:", captchaStore.Get(id, false)) + } + return id, content } diff --git a/user/captcha_test.go b/user/captcha_test.go new file mode 100644 index 00000000..7c5f604c --- /dev/null +++ b/user/captcha_test.go @@ -0,0 +1,45 @@ +package user + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCaptcha(t *testing.T) { + id, content := MakeCaptcha(CaptchaOption{ + Type: "audio", + Width: 240, + Height: 80, + Length: 4, + Lang: "zh", + }) + assert.IsType(t, "string", id) + assert.IsType(t, "string", content) + captchaStore.Get(id, false) + assert.True(t, ValidateCaptcha(id, captchaStore.Get(id, false))) + + id, content = MakeCaptcha(CaptchaOption{ + Type: "math", + Width: 240, + Height: 80, + Length: 4, + Lang: "zh", + }) + assert.IsType(t, "string", id) + assert.IsType(t, "string", content) + captchaStore.Get(id, false) + assert.True(t, ValidateCaptcha(id, captchaStore.Get(id, false))) + + id, content = MakeCaptcha(CaptchaOption{ + Type: "digit", + Width: 240, + Height: 80, + Length: 4, + Lang: "zh", + }) + assert.IsType(t, "string", id) + assert.IsType(t, "string", content) + captchaStore.Get(id, false) + assert.True(t, ValidateCaptcha(id, captchaStore.Get(id, false))) +} diff --git a/xlog/xlog.go b/xlog/xlog.go new file mode 100644 index 00000000..a0054960 --- /dev/null +++ b/xlog/xlog.go @@ -0,0 +1,29 @@ +package xlog + +import ( + "fmt" + "log" +) + +// XLog 日志接口 +type XLog interface{} + +// These functions write to the standard logger. + +// Print calls Output to print to the standard logger. +// Arguments are handled in the manner of fmt.Print. +func Print(v ...interface{}) { + log.Output(2, fmt.Sprint(v...)) +} + +// Printf calls Output to print to the standard logger. +// Arguments are handled in the manner of fmt.Printf. +func Printf(format string, v ...interface{}) { + log.Output(2, fmt.Sprintf(format, v...)) +} + +// Println calls Output to print to the standard logger. +// Arguments are handled in the manner of fmt.Println. +func Println(v ...interface{}) { + log.Output(2, fmt.Sprintln(v...)) +}