Browse Source

二维码模式更换

QM303176530 4 years ago
parent
commit
2160b28369

+ 17 - 19
server/api/v1/sys_captcha.go

@@ -1,14 +1,15 @@
 package v1
 
 import (
-	"gin-vue-admin/global"
+	"fmt"
 	"gin-vue-admin/global/response"
 	resp "gin-vue-admin/model/response"
-	"gin-vue-admin/utils"
-	"github.com/dchest/captcha"
 	"github.com/gin-gonic/gin"
+	"github.com/mojocn/base64Captcha"
 )
 
+var store = base64Captcha.DefaultMemStore
+
 // @Tags base
 // @Summary 生成验证码
 // @Security ApiKeyAuth
@@ -17,20 +18,17 @@ import (
 // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
 // @Router /base/captcha [post]
 func Captcha(c *gin.Context) {
-	captchaId := captcha.NewLen(global.GVA_CONFIG.Captcha.KeyLong)
-	response.OkDetailed(resp.SysCaptchaResponse{
-		CaptchaId: captchaId,
-		PicPath:   "/base/captcha/" + captchaId + ".png",
-	}, "验证码获取成功", c)
-}
-
-// @Tags base
-// @Summary 生成验证码图片路径
-// @Security ApiKeyAuth
-// @accept application/json
-// @Produce application/json
-// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
-// @Router /base/captcha/:captchaId [get]
-func CaptchaImg(c *gin.Context) {
-	utils.GinCaptchaServeHTTP(c.Writer, c.Request)
+	//字符,公式,验证码配置
+	// 生成默认数字的driver
+	driver := base64Captcha.NewDriverDigit(80, 240, 5, 0.7, 80)
+	cp := base64Captcha.NewCaptcha(driver, store)
+	id, b64s, err := cp.Generate()
+	if err != nil {
+		response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
+	} else {
+		response.OkDetailed(resp.SysCaptchaResponse{
+			CaptchaId: id,
+			PicPath:   b64s,
+		}, "验证码获取成功", c)
+	}
 }

+ 1 - 2
server/api/v1/sys_user.go

@@ -10,7 +10,6 @@ import (
 	resp "gin-vue-admin/model/response"
 	"gin-vue-admin/service"
 	"gin-vue-admin/utils"
-	"github.com/dchest/captcha"
 	"github.com/dgrijalva/jwt-go"
 	"github.com/gin-gonic/gin"
 	"github.com/go-redis/redis"
@@ -67,7 +66,7 @@ func Login(c *gin.Context) {
 		response.FailWithMessage(UserVerifyErr.Error(), c)
 		return
 	}
-	if captcha.VerifyString(L.CaptchaId, L.Captcha) {
+	if store.Verify(L.CaptchaId, L.Captcha, true) {
 		U := &model.SysUser{Username: L.Username, Password: L.Password}
 		if err, user := service.Login(U); err != nil {
 			response.FailWithMessage(fmt.Sprintf("用户名密码错误或%v", err), c)

+ 1 - 0
server/go.mod

@@ -26,6 +26,7 @@ require (
 	github.com/lib/pq v1.3.0 // indirect
 	github.com/mailru/easyjson v0.7.1 // indirect
 	github.com/mitchellh/mapstructure v1.2.2 // indirect
+	github.com/mojocn/base64Captcha v1.3.1
 	github.com/onsi/ginkgo v1.7.0 // indirect
 	github.com/onsi/gomega v1.4.3 // indirect
 	github.com/op/go-logging v0.0.0-20160315200505-970db520ece7

+ 0 - 1
server/router/sys_base.go

@@ -11,7 +11,6 @@ func InitBaseRouter(Router *gin.RouterGroup) (R gin.IRoutes) {
 		BaseRouter.POST("register", v1.Register)
 		BaseRouter.POST("login", v1.Login)
 		BaseRouter.POST("captcha", v1.Captcha)
-		BaseRouter.GET("captcha/:captchaId", v1.CaptchaImg)
 	}
 	return BaseRouter
 }

+ 0 - 55
server/utils/captcha.go

@@ -1,55 +0,0 @@
-package utils
-
-import (
-	"bytes"
-	"fmt"
-	"gin-vue-admin/global"
-	"github.com/dchest/captcha"
-	"net/http"
-	"path"
-	"strings"
-	"time"
-)
-
-// 这里需要自行实现captcha 的gin模式
-func GinCaptchaServeHTTP(w http.ResponseWriter, r *http.Request) {
-	dir, file := path.Split(r.URL.Path)
-	ext := path.Ext(file)
-	id := file[:len(file)-len(ext)]
-	if ext == "" || id == "" {
-		http.NotFound(w, r)
-		return
-	}
-	fmt.Println("reload : " + r.FormValue("reload"))
-	if r.FormValue("reload") != "" {
-		captcha.Reload(id)
-	}
-	lang := strings.ToLower(r.FormValue("lang"))
-	download := path.Base(dir) == "download"
-	if Serve(w, r, id, ext, lang, download, global.GVA_CONFIG.Captcha.ImgWidth, global.GVA_CONFIG.Captcha.ImgHeight) == captcha.ErrNotFound {
-		http.NotFound(w, r)
-	}
-}
-
-func Serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool, width, height int) error {
-	w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
-	w.Header().Set("Pragma", "no-cache")
-	w.Header().Set("Expires", "0")
-	var content bytes.Buffer
-	switch ext {
-	case ".png":
-		w.Header().Set("Content-Type", "image/png")
-		_ = captcha.WriteImage(&content, id, width, height)
-	case ".wav":
-		w.Header().Set("Content-Type", "audio/x-wav")
-		_ = captcha.WriteAudio(&content, id, lang)
-	default:
-		return captcha.ErrNotFound
-	}
-
-	if download {
-		w.Header().Set("Content-Type", "application/octet-stream")
-	}
-	http.ServeContent(w, r, id+ext, time.Time{}, bytes.NewReader(content.Bytes()))
-	return nil
-}

+ 3 - 3
web/src/view/login/login.vue

@@ -52,7 +52,9 @@
             <div class="vPic">
               <img
                 v-if="picPath"
-                :src="path + picPath"
+                :src="picPath"
+                width="100%"
+                height="100%"
                 alt="请输入验证码"
                 @click="loginVefify()"
               />
@@ -92,7 +94,6 @@
 <script>
 import { mapActions } from "vuex";
 import { captcha } from "@/api/user";
-const path = process.env.VUE_APP_BASE_API;
 export default {
   name: "Login",
   data() {
@@ -123,7 +124,6 @@ export default {
         username: [{ validator: checkUsername, trigger: "blur" }],
         password: [{ validator: checkPassword, trigger: "blur" }],
       },
-      path: path,
       logVerify: "",
       picPath: "",
     };