yxh 6 лет назад
Родитель
Сommit
5b7f965a04
7 измененных файлов с 133 добавлено и 20 удалено
  1. 2 1
      .gitignore
  2. 0 10
      app/api/hello/hello.go
  3. 66 0
      app/controller/admin/public.go
  4. 2 2
      boot/boot.go
  5. 2 1
      go.mod
  6. 6 5
      router/router.go
  7. 55 1
      test/demo_test.go

+ 2 - 1
.gitignore

@@ -7,7 +7,8 @@
 .settings/
 .vscode/
 vender/
-log/
+data/log/
+data/session/
 composer.lock
 gitpush.sh
 pkg/

+ 0 - 10
app/api/hello/hello.go

@@ -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!")
-}

+ 66 - 0
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", "<img src=\""+base64stringC+"\">")
+}
+
+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("验证失败")
+	}
+}

+ 2 - 2
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))
 }
-

+ 2 - 1
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
 )
 

+ 6 - 5
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))
 }

+ 55 - 1
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)