From 5b7f965a04c9851b528b77b323616d59bd326daf Mon Sep 17 00:00:00 2001 From: yxh Date: Sat, 4 Jan 2020 22:44:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E5=92=8Cgftoken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- app/api/hello/hello.go | 10 ------ app/controller/admin/public.go | 66 ++++++++++++++++++++++++++++++++++ boot/boot.go | 4 +-- go.mod | 3 +- router/router.go | 11 +++--- test/demo_test.go | 56 ++++++++++++++++++++++++++++- 7 files changed, 133 insertions(+), 20 deletions(-) delete mode 100644 app/api/hello/hello.go create mode 100644 app/controller/admin/public.go diff --git a/.gitignore b/.gitignore index c1db1c7..62624a8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,8 @@ .settings/ .vscode/ vender/ -log/ +data/log/ +data/session/ composer.lock gitpush.sh pkg/ diff --git a/app/api/hello/hello.go b/app/api/hello/hello.go deleted file mode 100644 index bde4596..0000000 --- a/app/api/hello/hello.go +++ /dev/null @@ -1,10 +0,0 @@ -package hello - -import ( - "github.com/gogf/gf/net/ghttp" -) - -// Hello World -func Handler(r *ghttp.Request) { - r.Response.Writeln("Hello World!") -} diff --git a/app/controller/admin/public.go b/app/controller/admin/public.go new file mode 100644 index 0000000..c1dbf3e --- /dev/null +++ b/app/controller/admin/public.go @@ -0,0 +1,66 @@ +package admin + +import ( + "gfast/app/service/user" + "gfast/library/response" + "github.com/gogf/gf/net/ghttp" + "github.com/gogf/gf/util/gvalid" + "github.com/mojocn/base64Captcha" +) + +type Public struct{} + +//Login 用户登陆验证 +func (p *Public) Login(r *ghttp.Request) { + data := r.GetPostMapStrStr() + rules := map[string]string{ + "passport": "required", + "password": "required", + } + msgs := map[string]interface{}{ + "passport": "账号不能为空", + "password": "密码不能为空", + } + if e := gvalid.CheckMap(data, rules, msgs); e != nil { + response.Json(r, 1, e.String()) + } + if err := user.SignIn(data["passport"], data["password"], r.Session); err != nil { + response.Json(r, 1, err.Error()) + } else { + response.Json(r, 0, "ok") + } +} + +func (p *Public) Verify(r *ghttp.Request) { + //字符,公式,验证码配置 + var configC = base64Captcha.ConfigCharacter{ + Height: 60, + Width: 240, + //const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合. + Mode: base64Captcha.CaptchaModeNumberAlphabet, + ComplexOfNoiseText: base64Captcha.CaptchaComplexLower, + ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower, + IsShowHollowLine: false, + IsShowNoiseDot: false, + IsShowNoiseText: false, + IsShowSlimeLine: false, + IsShowSineLine: true, + CaptchaLen: 4, + } + //创建字符公式验证码. + //GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid. + idKeyC, capC := base64Captcha.GenerateCaptcha("8nM77YhE2xOvU6GMQ33A", configC) + //以base64编码 + base64stringC := base64Captcha.CaptchaWriteToBase64Encoding(capC) + r.Response.Header().Set("Content-type", "text/html") + r.Response.Write(idKeyC, "\n", "") +} + +func (p *Public) CheckVerify(r *ghttp.Request) { + data := r.GetQueryMapStrStr() + if base64Captcha.VerifyCaptchaAndIsClear(data["key"], data["value"], false) { + r.Response.Write("验证成功") + } else { + r.Response.Write("验证失败") + } +} diff --git a/boot/boot.go b/boot/boot.go index 1f87801..33ce726 100644 --- a/boot/boot.go +++ b/boot/boot.go @@ -3,6 +3,6 @@ package boot import "github.com/gogf/gf/frame/g" func init() { - g.Server().SetPort(8200) + g.Server().SetPort(8200) + g.Server().AddStaticPath("/public", g.Cfg().Get("server.ServerRoot").(string)) } - diff --git a/go.mod b/go.mod index 85084a1..8e1bae1 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,8 @@ module gfast require ( github.com/casbin/casbin/v2 v2.1.2 - github.com/gogf/gf v1.10.0 + github.com/gogf/gf v1.10.1 + github.com/mojocn/base64Captcha v1.2.2 google.golang.org/appengine v1.6.5 // indirect ) diff --git a/router/router.go b/router/router.go index f5c0f02..5daa027 100644 --- a/router/router.go +++ b/router/router.go @@ -1,13 +1,14 @@ package router import ( - "gfast/app/api/hello" - "gfast/app/api/user" - "github.com/gogf/gf/frame/g" + "gfast/app/api/user" + "gfast/app/controller/admin" + "github.com/gogf/gf/frame/g" ) // 统一路由注册. func init() { - g.Server().BindHandler("/", hello.Handler) - g.Server().BindObject("/user", new(user.Controller)) + s := g.Server() + s.BindObject("/user", new(user.Controller)) + s.BindObject("/system/public", new(admin.Public)) } diff --git a/test/demo_test.go b/test/demo_test.go index 9e33b5f..8397437 100644 --- a/test/demo_test.go +++ b/test/demo_test.go @@ -6,12 +6,15 @@ import ( "github.com/casbin/casbin/v2" "github.com/casbin/casbin/v2/util" "github.com/gogf/gf/os/glog" + "github.com/mojocn/base64Captcha" "testing" ) func TestDemo(t *testing.T) { //t.Run("demo1" ,Demo1) - t.Run("Adapters_test", Adapters) + //t.Run("Adapters_test", Adapters) + t.Run("CaptchaDemo", CaptchaDemo) + t.Run("CaptchaVerify", CaptchaVerify) } func Demo1(t *testing.T) { @@ -35,6 +38,57 @@ func Demo1(t *testing.T) { } } +func demoCodeCaptchaCreate() { + //config struct for digits + //数字验证码配置 + /*var configD = base64Captcha.ConfigDigit{ + Height: 80, + Width: 240, + MaxSkew: 0.7, + DotCount: 80, + CaptchaLen: 5, + }*/ + //config struct for audio + //声音验证码配置 + /*var configA = base64Captcha.ConfigAudio{ + CaptchaLen: 6, + Language: "zh", + }*/ + //config struct for Character + //字符,公式,验证码配置 + var configC = base64Captcha.ConfigCharacter{ + Height: 60, + Width: 240, + //const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合. + Mode: base64Captcha.CaptchaModeNumber, + ComplexOfNoiseText: base64Captcha.CaptchaComplexLower, + ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower, + IsShowHollowLine: false, + IsShowNoiseDot: false, + IsShowNoiseText: false, + IsShowSlimeLine: false, + IsShowSineLine: false, + CaptchaLen: 4, + } + //创建字符公式验证码. + //GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid. + idKeyC, capC := base64Captcha.GenerateCaptcha("8nM77YhE2xOvU6GMQ33A", configC) + //以base64编码 + base64stringC := base64Captcha.CaptchaWriteToBase64Encoding(capC) + fmt.Println(idKeyC, "\n", base64stringC) +} +func CaptchaDemo(t *testing.T) { + demoCodeCaptchaCreate() +} + +func CaptchaVerify(t *testing.T) { + if base64Captcha.VerifyCaptchaAndIsClear("8nM77YhE2xOvU6GMQ33A", "0870", false) { + fmt.Println("验证成功") + } else { + fmt.Println("验证失败") + } +} + func Adapters(t *testing.T) { a := initAdapter(t, "mysql", "root:123456@tcp(127.0.0.1:3306)/test2") testAutoSave(t, a)